# Adding concept intersections

## 1.1 Introduction

Concept sets play an important role when working with data in the format of the OMOP CDM. They can be used to create cohorts after which, as we’ve seen in the previous vignette, we can identify intersections between the cohorts. PatientProfiles adds another option for working with concept sets which is use them for adding associated variables directly without first having to create a cohort.

It is important to note, and is explained more below, that results may differ when generating a cohort and then identifying intersections between two cohorts compared to working directly with concept sets. The creation of cohorts will involve the collapsing of overlapping records as well as imposing certain requirements such as only including records that were observed during an individuals observation period. When adding variables based on concept sets we will be working directly with record-level data in the OMOP CDM clinical tables.

## 1.2 Adding variables from concept sets

For this vignette we’ll use the Eunomia synthetic dataset. First lets create our cohort of interest, individuals with an ankle sprain.

    library(PatientProfiles)
    library(CodelistGenerator)
    library(CohortConstructor)
    library(dplyr)
    library(ggplot2)
    library(omock)

    cdm <- mockCdmFromDataset(datasetName = "GiBleed", source = "duckdb")
    #> ℹ Reading GiBleed tables.
    #> ℹ Adding drug_strength table.

    cdm$ankle_sprain <- conceptCohort(
      cdm = cdm,
      conceptSet = list("ankle_sprain" = 81151),
      name = "ankle_sprain"
    )
    #> Warning: ! `codelist` casted to integers.
    #> ℹ Subsetting table condition_occurrence using 1 concept with domain: condition.
    #> ℹ Combining tables.
    #> ℹ Creating cohort attributes.
    #> ℹ Applying cohort requirements.
    #> ℹ Merging overlapping records.
    #> ✔ Cohort ankle_sprain created.

    cdm$ankle_sprain
    #> # Source:   table<results.test_ankle_sprain> [?? x 4]
    #> # Database: DuckDB 1.4.0 [root@Darwin 24.6.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/Rtmp46ctLr/filee9e8d53c28.duckdb]
    #>    cohort_definition_id subject_id cohort_start_date cohort_end_date
    #>                   <int>      <int> <date>            <date>
    #>  1                    1        126 1985-08-30        1985-10-04
    #>  2                    1        188 2011-10-30        2011-11-20
    #>  3                    1       3839 1970-08-09        1970-09-13
    #>  4                    1       5197 1987-07-27        1987-08-10
    #>  5                    1       3562 1975-06-16        1975-07-14
    #>  6                    1       2619 1966-02-17        1966-03-17
    #>  7                    1       2771 1940-03-08        1940-03-29
    #>  8                    1       4971 1971-07-06        1971-08-03
    #>  9                    1        442 1997-05-28        1997-07-02
    #> 10                    1       4472 1955-09-25        1955-10-16
    #> # ℹ more rows

Now let’s say we’re interested in summarising use of acetaminophen among our ankle sprain cohort. We can start by identifying the relevant concepts.

    acetaminophen_cs <- getDrugIngredientCodes(
      cdm = cdm,
      name = c("acetaminophen")
    )

    acetaminophen_cs
    #>
    #> ── 1 codelist ──────────────────────────────────────────────────────────────────
    #>
    #> - 161_acetaminophen (7 codes)

Once we have our codes for acetaminophen we can create variables based on these. As with cohort intersections, PatientProfiles provides four types of functions for concept intersections.

First, we can add a binary flag variable indicating whether an individual had a record of acetaminophen on the day of their ankle sprain or up to 30 days afterwards.

    cdm$ankle_sprain |>
      addConceptIntersectFlag(
        conceptSet = acetaminophen_cs,
        indexDate = "cohort_start_date",
        window = c(0, 30)
      ) |>
      glimpse()
    #> Rows: ??
    #> Columns: 5
    #> Database: DuckDB 1.4.0 [root@Darwin 24.6.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/Rtmp46ctLr/filee9e8d53c28.duckdb]
    #> $ cohort_definition_id        <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
    #> $ subject_id                  <int> 3839, 5197, 3562, 1602, 3137, 2501, 2392, …
    #> $ cohort_start_date           <date> 1970-08-09, 1987-07-27, 1975-06-16, 2013-…
    #> $ cohort_end_date             <date> 1970-09-13, 1987-08-10, 1975-07-14, 2013-…
    #> $ `161_acetaminophen_0_to_30` <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …

Second, we can count the number of records of acetaminophen in this same window for each individual.

    cdm$ankle_sprain |>
      addConceptIntersectCount(
        conceptSet = acetaminophen_cs,
        indexDate = "cohort_start_date",
        window = c(0, 30)
      ) |>
      glimpse()
    #> Rows: ??
    #> Columns: 5
    #> Database: DuckDB 1.4.0 [root@Darwin 24.6.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/Rtmp46ctLr/filee9e8d53c28.duckdb]
    #> $ cohort_definition_id        <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
    #> $ subject_id                  <int> 3839, 5197, 3562, 1602, 3137, 2501, 2392, …
    #> $ cohort_start_date           <date> 1970-08-09, 1987-07-27, 1975-06-16, 2013-…
    #> $ cohort_end_date             <date> 1970-09-13, 1987-08-10, 1975-07-14, 2013-…
    #> $ `161_acetaminophen_0_to_30` <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …

Third, we could identify the first start date of acetaminophen in this window.

    cdm$ankle_sprain |>
      addConceptIntersectDate(
        conceptSet = acetaminophen_cs,
        indexDate = "cohort_start_date",
        window = c(0, 30),
        order = "first"
      ) |>
      glimpse()
    #> Rows: ??
    #> Columns: 5
    #> Database: DuckDB 1.4.0 [root@Darwin 24.6.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/Rtmp46ctLr/filee9e8d53c28.duckdb]
    #> $ cohort_definition_id        <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
    #> $ subject_id                  <int> 3839, 5197, 3562, 1602, 3137, 2501, 2392, …
    #> $ cohort_start_date           <date> 1970-08-09, 1987-07-27, 1975-06-16, 2013-…
    #> $ cohort_end_date             <date> 1970-09-13, 1987-08-10, 1975-07-14, 2013-…
    #> $ `161_acetaminophen_0_to_30` <date> 1970-08-09, 1987-07-27, 1975-06-16, 2013-…

Or fourth, we can get the number of days to the start date of acetaminophen in the window.

    cdm$ankle_sprain |>
      addConceptIntersectDays(
        conceptSet = acetaminophen_cs,
        indexDate = "cohort_start_date",
        window = c(0, 30),
        order = "first"
      ) |>
      glimpse()
    #> Rows: ??
    #> Columns: 5
    #> Database: DuckDB 1.4.0 [root@Darwin 24.6.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/Rtmp46ctLr/filee9e8d53c28.duckdb]
    #> $ cohort_definition_id        <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
    #> $ subject_id                  <int> 3839, 5197, 3562, 1602, 3137, 2501, 2392, …
    #> $ cohort_start_date           <date> 1970-08-09, 1987-07-27, 1975-06-16, 2013-…
    #> $ cohort_end_date             <date> 1970-09-13, 1987-08-10, 1975-07-14, 2013-…
    #> $ `161_acetaminophen_0_to_30` <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …

## 1.3 Adding multiple concept based variables

We can add more than one variable at a time when using these functions. For example, we might want to add variables for multiple time windows.

    cdm$ankle_sprain |>
      addConceptIntersectFlag(
        conceptSet = acetaminophen_cs,
        indexDate = "cohort_start_date",
        window = list(
          c(-Inf, -1),
          c(0, 0),
          c(1, Inf)
        )
      ) |>
      glimpse()
    #> Rows: ??
    #> Columns: 7
    #> Database: DuckDB 1.4.0 [root@Darwin 24.6.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/Rtmp46ctLr/filee9e8d53c28.duckdb]
    #> $ cohort_definition_id           <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
    #> $ subject_id                     <int> 126, 188, 3839, 5197, 3562, 2619, 2771,…
    #> $ cohort_start_date              <date> 1985-08-30, 2011-10-30, 1970-08-09, 19…
    #> $ cohort_end_date                <date> 1985-10-04, 2011-11-20, 1970-09-13, 19…
    #> $ `161_acetaminophen_1_to_inf`   <dbl> 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, …
    #> $ `161_acetaminophen_minf_to_m1` <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, …
    #> $ `161_acetaminophen_0_to_0`     <dbl> 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, …

Or we might want to get variables for multiple drug ingredients of interest.

    meds_cs <- getDrugIngredientCodes(
      cdm = cdm,
      name = c(
        "acetaminophen",
        "amoxicillin",
        "aspirin",
        "heparin",
        "morphine",
        "oxycodone",
        "warfarin"
      )
    )

    cdm$ankle_sprain |>
      addConceptIntersectFlag(
        conceptSet = meds_cs,
        indexDate = "cohort_start_date",
        window = list(
          c(-Inf, -1),
          c(0, 0)
        )
      ) |>
      glimpse()
    #> Rows: ??
    #> Columns: 18
    #> Database: DuckDB 1.4.0 [root@Darwin 24.6.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/Rtmp46ctLr/filee9e8d53c28.duckdb]
    #> $ cohort_definition_id           <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
    #> $ subject_id                     <int> 1661, 3105, 1478, 1427, 2677, 1808, 100…
    #> $ cohort_start_date              <date> 1963-04-19, 2000-10-13, 2016-04-04, 19…
    #> $ cohort_end_date                <date> 1963-05-24, 2000-11-03, 2016-04-18, 19…
    #> $ `7052_morphine_minf_to_m1`     <dbl> 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
    #> $ `723_amoxicillin_minf_to_m1`   <dbl> 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, …
    #> $ `7804_oxycodone_minf_to_m1`    <dbl> 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, …
    #> $ `5224_heparin_minf_to_m1`      <dbl> 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
    #> $ `723_amoxicillin_0_to_0`       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
    #> $ `1191_aspirin_minf_to_m1`      <dbl> 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, …
    #> $ `1191_aspirin_0_to_0`          <dbl> 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, …
    #> $ `161_acetaminophen_minf_to_m1` <dbl> 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, …
    #> $ `11289_warfarin_minf_to_m1`    <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
    #> $ `161_acetaminophen_0_to_0`     <dbl> 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, …
    #> $ `11289_warfarin_0_to_0`        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
    #> $ `5224_heparin_0_to_0`          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
    #> $ `7052_morphine_0_to_0`         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
    #> $ `7804_oxycodone_0_to_0`        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …

## 1.4 Cohort-based versus concept-based intersections

In the previous vignette we saw that we can add an intersection variable using a cohort we have created. Meanwhile in this vignette we see that we can instead create an intersection variable using a concept set directly. It is important to note that under some circumstances these two approaches can lead to different results.

When creating a cohort we combine overlapping records, as cohort entries cannot overlap. Thus when adding an intersection count, `addCohortIntersectCount()` will return a count of cohort entries in the window of interest while `addConceptIntersectCount()` will return a count of records withing the window. We can see the impact for acetaminophen for our example data below, where we have slightly more records than cohort entries.

    acetaminophen_cs <- getDrugIngredientCodes(
      cdm = cdm,
      name = c("acetaminophen")
    )

    cdm$acetaminophen <- conceptCohort(
      cdm = cdm,
      name = "acetaminophen",
      conceptSet = acetaminophen_cs
    )
    #> ℹ Subsetting table drug_exposure using 7 concepts with domain: drug.
    #> ℹ Combining tables.
    #> ℹ Creating cohort attributes.
    #> ℹ Applying cohort requirements.
    #> ℹ Merging overlapping records.
    #> ✔ Cohort acetaminophen created.

    bind_rows(
      cdm$ankle_sprain |>
        addCohortIntersectCount(
          targetCohortTable = "acetaminophen",
          window = c(-Inf, Inf)
        ) |>
        group_by(`161_acetaminophen_minf_to_inf`) |>
        tally() |>
        collect() |>
        arrange(desc(`161_acetaminophen_minf_to_inf`)) |>
        mutate(type = "cohort"),
      cdm$ankle_sprain |>
        addConceptIntersectCount(
          conceptSet = acetaminophen_cs,
          window = c(-Inf, Inf)
        ) |>
        group_by(`161_acetaminophen_minf_to_inf`) |>
        tally() |>
        collect() |>
        arrange(desc(`161_acetaminophen_minf_to_inf`)) |>
        mutate(type = "concept_set")
    ) |>
      ggplot() +
      geom_col(aes(`161_acetaminophen_minf_to_inf`, n, fill = type),
        position = "dodge"
      ) +
      theme_bw() +
      theme(
        legend.title = element_blank(),
        legend.position = "top"
      )

Additional differences between cohort and concept set intersections may also result from cohort table rules. For example, cohort tables will typically omit any records that occur outside an individual´s observation time (as defined in the observation period window). Such records, however, would not be excluded when adding a concept based intersection.