Skip to contents

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.

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.

#> Error in get(paste0(generic, ".", class), envir = get_method_env()) : 
#>   object 'type_sum.accel' not found
#> 
#> Download completed!
library(CDMConnector)
library(CodelistGenerator)
library(PatientProfiles)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

con <- DBI::dbConnect(duckdb::duckdb(),
  dbdir = CDMConnector::eunomia_dir()
)
#> Creating CDM database /tmp/RtmpuP8HP0/GiBleed_5.3.zip
cdm <- CDMConnector::cdm_from_con(con,
  cdm_schem = "main",
  write_schema = "main"
)
#> Note: method with signature 'DBIConnection#Id' chosen for function 'dbExistsTable',
#>  target signature 'duckdb_connection#Id'.
#>  "duckdb_connection#ANY" would also be valid

cdm <- generateConceptCohortSet(
  cdm = cdm,
  name = "ankle_sprain",
  conceptSet = list("ankle_sprain" = 81151),
  end = "event_end_date",
  limit = "all",
  overwrite = TRUE
)

cdm$ankle_sprain
#> # Source:   table<main.ankle_sprain> [?? x 4]
#> # Database: DuckDB v1.1.3 [unknown@Linux 6.8.0-1017-azure:R 4.4.2//tmp/RtmpuP8HP0/file20b05eabe89a.duckdb]
#>    cohort_definition_id subject_id cohort_start_date cohort_end_date
#>                   <int>      <int> <date>            <date>         
#>  1                    1        246 1981-06-06        1981-06-27     
#>  2                    1        445 1951-08-05        1951-08-19     
#>  3                    1        474 1982-12-02        1982-12-16     
#>  4                    1        579 1994-07-25        1994-08-29     
#>  5                    1        579 2005-10-28        2005-12-02     
#>  6                    1        652 1991-05-18        1991-06-08     
#>  7                    1        655 1999-06-06        1999-07-04     
#>  8                    1        870 2002-11-11        2002-12-02     
#>  9                    1       1000 1995-02-08        1995-03-08     
#> 10                    1       1021 1976-06-29        1976-07-20     
#> # ℹ 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)
  ) |>
  dplyr::glimpse()
#> Rows: ??
#> Columns: 5
#> Database: DuckDB v1.1.3 [unknown@Linux 6.8.0-1017-azure:R 4.4.2//tmp/RtmpuP8HP0/file20b05eabe89a.duckdb]
#> $ cohort_definition_id        <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ subject_id                  <int> 579, 655, 870, 1124, 1283, 1427, 2226, 225…
#> $ cohort_start_date           <date> 1994-07-25, 1999-06-06, 2002-11-11, 1971-…
#> $ cohort_end_date             <date> 1994-08-29, 1999-07-04, 2002-12-02, 1971-…
#> $ `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)
  ) |>
  dplyr::glimpse()
#> Rows: ??
#> Columns: 5
#> Database: DuckDB v1.1.3 [unknown@Linux 6.8.0-1017-azure:R 4.4.2//tmp/RtmpuP8HP0/file20b05eabe89a.duckdb]
#> $ cohort_definition_id        <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ subject_id                  <int> 579, 655, 870, 1124, 1283, 1427, 2226, 225…
#> $ cohort_start_date           <date> 1994-07-25, 1999-06-06, 2002-11-11, 1971-…
#> $ cohort_end_date             <date> 1994-08-29, 1999-07-04, 2002-12-02, 1971-…
#> $ `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"
  ) |>
  dplyr::glimpse()
#> Rows: ??
#> Columns: 5
#> Database: DuckDB v1.1.3 [unknown@Linux 6.8.0-1017-azure:R 4.4.2//tmp/RtmpuP8HP0/file20b05eabe89a.duckdb]
#> $ cohort_definition_id        <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ subject_id                  <int> 579, 655, 870, 1021, 1124, 1283, 1427, 222…
#> $ cohort_start_date           <date> 1994-07-25, 1999-06-06, 2002-11-11, 1976-…
#> $ cohort_end_date             <date> 1994-08-29, 1999-07-04, 2002-12-02, 1976-…
#> $ `161_acetaminophen_0_to_30` <date> 1994-07-25, 1999-06-06, 2002-11-11, 1976-…

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"
  ) |>
  dplyr::glimpse()
#> Rows: ??
#> Columns: 5
#> Database: DuckDB v1.1.3 [unknown@Linux 6.8.0-1017-azure:R 4.4.2//tmp/RtmpuP8HP0/file20b05eabe89a.duckdb]
#> $ cohort_definition_id        <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ subject_id                  <int> 579, 655, 870, 1021, 1124, 1283, 1427, 222…
#> $ cohort_start_date           <date> 1994-07-25, 1999-06-06, 2002-11-11, 1976-…
#> $ cohort_end_date             <date> 1994-08-29, 1999-07-04, 2002-12-02, 1976-…
#> $ `161_acetaminophen_0_to_30` <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …

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)
    )
  ) |>
  dplyr::glimpse()
#> Rows: ??
#> Columns: 7
#> Database: DuckDB v1.1.3 [unknown@Linux 6.8.0-1017-azure:R 4.4.2//tmp/RtmpuP8HP0/file20b05eabe89a.duckdb]
#> $ cohort_definition_id           <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ subject_id                     <int> 246, 579, 579, 652, 655, 870, 1124, 128…
#> $ cohort_start_date              <date> 1981-06-06, 1994-07-25, 2005-10-28, 19…
#> $ cohort_end_date                <date> 1981-06-27, 1994-08-29, 2005-12-02, 19…
#> $ `161_acetaminophen_minf_to_m1` <dbl> 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, …
#> $ `161_acetaminophen_0_to_0`     <dbl> 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, …
#> $ `161_acetaminophen_1_to_inf`   <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …

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)
    )
  ) |>
  dplyr::glimpse()
#> Rows: ??
#> Columns: 18
#> Database: DuckDB v1.1.3 [unknown@Linux 6.8.0-1017-azure:R 4.4.2//tmp/RtmpuP8HP0/file20b05eabe89a.duckdb]
#> $ cohort_definition_id           <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ subject_id                     <int> 3199, 576, 2206, 4349, 1450, 871, 520, …
#> $ cohort_start_date              <date> 1980-08-22, 1999-06-28, 1990-11-23, 19…
#> $ cohort_end_date                <date> 1980-09-19, 1999-07-12, 1990-12-14, 19…
#> $ `5224_heparin_minf_to_m1`      <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
#> $ `7052_morphine_minf_to_m1`     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
#> $ `7804_oxycodone_minf_to_m1`    <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
#> $ `1191_aspirin_minf_to_m1`      <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ `723_amoxicillin_minf_to_m1`   <dbl> 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, …
#> $ `161_acetaminophen_minf_to_m1` <dbl> 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ `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> 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, …
#> $ `1191_aspirin_0_to_0`          <dbl> 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, …
#> $ `723_amoxicillin_0_to_0`       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, …

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 <- generateConceptCohortSet(
  cdm = cdm,
  name = "acetaminophen",
  conceptSet = acetaminophen_cs,
  end = "event_end_date",
  limit = "all"
)

dplyr::bind_rows(
  cdm$ankle_sprain |>
    addCohortIntersectCount(
      targetCohortTable = "acetaminophen",
      window = c(-Inf, Inf)
    ) |>
    dplyr::group_by(`161_acetaminophen_minf_to_inf`) |>
    dplyr::tally() |>
    dplyr::collect() |>
    dplyr::arrange(desc(`161_acetaminophen_minf_to_inf`)) |>
    dplyr::mutate(type = "cohort"),
  cdm$ankle_sprain |>
    addConceptIntersectCount(
      conceptSet = acetaminophen_cs,
      window = c(-Inf, Inf)
    ) |>
    dplyr::group_by(`161_acetaminophen_minf_to_inf`) |>
    dplyr::tally() |>
    dplyr::collect() |>
    dplyr::arrange(desc(`161_acetaminophen_minf_to_inf`)) |>
    dplyr::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.