When creating multiple cohorts we might be interested in the overlap between them. That is, how many individuals appear in multiple cohorts. CohortCharacteristics provides functions to generate such estimates and then summarise these estimates in tables and plots.

To see how this works let’s create a few medication cohorts with the Eunomia synthetic dataset.

library(CDMConnector)
library(CodelistGenerator)
library(CohortCharacteristics)
library(dplyr)
library(ggplot2)

con <- DBI::dbConnect(duckdb::duckdb(),
  dbdir = CDMConnector::eunomia_dir()
)
cdm <- CDMConnector::cdm_from_con(con,
  cdm_schem = "main",
  write_schema = "main",
  cdm_name = "Eunomia"
)

meds_cs <- getDrugIngredientCodes(
  cdm = cdm,
  name = c(
    "acetaminophen",
    "morphine",
    "warfarin"
  )
)

cdm <- generateConceptCohortSet(
  cdm = cdm,
  name = "meds",
  conceptSet = meds_cs,
  end = "event_end_date",
  limit = "all",
  overwrite = TRUE
)

settings(cdm$meds)
#> # A tibble: 3 × 6
#>   cohort_definition_id cohort_name    limit prior_observation future_observation
#>                  <int> <chr>          <chr>             <dbl>              <dbl>
#> 1                    1 11289_warfarin all                   0                  0
#> 2                    2 161_acetamino… all                   0                  0
#> 3                    3 7052_morphine  all                   0                  0
#> # ℹ 1 more variable: end <chr>
cohortCount(cdm$meds)
#> # A tibble: 3 × 3
#>   cohort_definition_id number_records number_subjects
#>                  <int>          <int>           <int>
#> 1                    1            137             137
#> 2                    2          13908            2679
#> 3                    3             35              35

Now we have our cohorts we can summarise the overlap between them.

meds_overlap <- cdm$meds |>
  summariseCohortOverlap()
meds_overlap |>
  glimpse()
#> Rows: 36
#> Columns: 13
#> $ result_id        <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
#> $ cdm_name         <chr> "Eunomia", "Eunomia", "Eunomia", "Eunomia", "Eunomia"…
#> $ group_name       <chr> "cohort_name_reference &&& cohort_name_comparator", "…
#> $ group_level      <chr> "161_acetaminophen &&& 11289_warfarin", "161_acetamin…
#> $ strata_name      <chr> "overall", "overall", "overall", "overall", "overall"…
#> $ strata_level     <chr> "overall", "overall", "overall", "overall", "overall"…
#> $ variable_name    <chr> "overlap", "reference", "comparator", "overlap", "ref…
#> $ variable_level   <chr> "number_subjects", "number_subjects", "number_subject…
#> $ estimate_name    <chr> "count", "count", "count", "count", "count", "count",…
#> $ estimate_type    <chr> "integer", "integer", "integer", "integer", "integer"…
#> $ estimate_value   <chr> "136", "2543", "1", "35", "2644", "0", "6", "131", "2…
#> $ additional_name  <chr> "overall", "overall", "overall", "overall", "overall"…
#> $ additional_level <chr> "overall", "overall", "overall", "overall", "overall"…

We have table and plotting functions to help view our results.

tableCohortOverlap(meds_overlap)
CDM name Cohort name reference Cohort name comparator Estimate name Only in reference cohort In both cohorts Only in comparator cohort
Eunomia 11289 warfarin 161 acetaminophen N (%) 1 (0.04%) 136 (5.07%) 2,543 (94.89%)
7052 morphine N (%) 131 (78.92%) 6 (3.61%) 29 (17.47%)
161 acetaminophen 7052 morphine N (%) 2,644 (98.69%) 35 (1.31%) 0 (0.00%)
plotCohortOverlap(meds_overlap)

As well as generating these estimates for cohorts overall, we can also obtain stratified estimates. In this example we’ll add age groups to our cohort table, and then obtain estimates stratified by these groups.

cdm$meds <- cdm$meds |>
  PatientProfiles::addAge(ageGroup = list(c(0, 49), c(50, 150))) |>
  compute(temporary = FALSE, name = "meds") |>
  newCohortTable()
meds_overlap <- cdm$meds |>
  summariseCohortOverlap(strata = list("age_group"))

As with our overall results, we can quickly create tables and figures to view our stratified results.

tableCohortOverlap(meds_overlap)
Age group
CDM name Cohort name reference Cohort name comparator Estimate name 0 to 49 50 to 150 Overall
Only in reference cohort In both cohorts Only in comparator cohort Only in reference cohort In both cohorts Only in comparator cohort Only in reference cohort In both cohorts Only in comparator cohort
Eunomia 11289 warfarin 161 acetaminophen N (%) 1 (0.04%) 8 (0.30%) 2,653 (99.66%) 26 (2.25%) 102 (8.85%) 1,025 (88.90%) 1 (0.04%) 136 (5.07%) 2,543 (94.89%)
7052 morphine N (%) 0 (0.00%) 0 (0.00%) 0 (0.00%) 122 (92.42%) 6 (4.55%) 4 (3.03%) 131 (78.92%) 6 (3.61%) 29 (17.47%)
161 acetaminophen 7052 morphine N (%) 2,636 (99.06%) 25 (0.94%) 0 (0.00%) 1,117 (99.11%) 10 (0.89%) 0 (0.00%) 2,644 (98.69%) 35 (1.31%) 0 (0.00%)
plotCohortOverlap(meds_overlap,
  facet = "strata_level"
)