So far we’ve seen that we can add variables indicating intersections based on cohorts or concept sets. One additional option we have is to simply add an intersection based on a table.

Let’s again create a cohort containing people with an ankle sprain.

library(CDMConnector)
library(CodelistGenerator)
library(PatientProfiles)
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 <- 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.0.0 [root@Darwin 23.4.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpzA2J8b/file751332d13f7c.duckdb]
#>    cohort_definition_id subject_id cohort_start_date cohort_end_date
#>                   <int>      <int> <date>            <date>         
#>  1                    1        884 2008-03-21        2008-04-18     
#>  2                    1       1117 1937-11-12        1937-12-03     
#>  3                    1       1145 1961-10-22        1961-11-26     
#>  4                    1       1214 1947-10-08        1947-11-05     
#>  5                    1       2210 1958-06-02        1958-06-23     
#>  6                    1       2461 1976-01-13        1976-01-27     
#>  7                    1       3064 1936-08-28        1936-10-02     
#>  8                    1       3141 1985-08-20        1985-09-03     
#>  9                    1       3509 1943-01-11        1943-02-01     
#> 10                    1       3668 1996-04-26        1996-05-24     
#> # ℹ more rows

cdm$ankle_sprain %>%
  addTableIntersectFlag(
    tableName = "condition_occurrence",
    window = c(-30, -1)
  ) |>
  tally()
#> # Source:   SQL [1 x 1]
#> # Database: DuckDB v1.0.0 [root@Darwin 23.4.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpzA2J8b/file751332d13f7c.duckdb]
#>       n
#>   <dbl>
#> 1  1915

We can use table intersection functions to check whether someone had a record in the drug exposure table in the 30 days before their ankle sprain. If we set targetStartDate to “drug_exposure_start_date” and targetEndDate to “drug_exposure_end_date” we are checking whether an individual had an ongoing drug exposure record in the window.

cdm$ankle_sprain |>
  addTableIntersectFlag(
    tableName = "drug_exposure",
    indexDate = "cohort_start_date",
    targetStartDate = "drug_exposure_start_date",
    targetEndDate = "drug_exposure_end_date",
    window = c(-30, -1)
  ) |>
  glimpse()
#> Rows: ??
#> Columns: 5
#> Database: DuckDB v1.0.0 [root@Darwin 23.4.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpzA2J8b/file751332d13f7c.duckdb]
#> $ cohort_definition_id    <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
#> $ subject_id              <int> 3047, 5022, 1335, 1568, 1992, 2651, 2843, 3820…
#> $ cohort_start_date       <date> 1989-04-28, 2010-01-23, 1998-05-28, 2017-12-0…
#> $ cohort_end_date         <date> 1989-05-26, 2010-02-27, 1998-06-18, 2017-12-2…
#> $ drug_exposure_m30_to_m1 <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…

Meanwhile if we set we set targetStartDate to “drug_exposure_start_date” and targetEndDate to “drug_exposure_start_date” we will instead be checking whether they had a drug exposure record that started during the window.

cdm$ankle_sprain |>
  addTableIntersectFlag(
    tableName = "drug_exposure",
    indexDate = "cohort_start_date",
    window = c(-30, -1)
  ) |>
  glimpse()
#> Rows: ??
#> Columns: 5
#> Database: DuckDB v1.0.0 [root@Darwin 23.4.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpzA2J8b/file751332d13f7c.duckdb]
#> $ cohort_definition_id    <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
#> $ subject_id              <int> 3047, 5022, 1335, 1568, 1992, 2651, 2843, 3820…
#> $ cohort_start_date       <date> 1989-04-28, 2010-01-23, 1998-05-28, 2017-12-0…
#> $ cohort_end_date         <date> 1989-05-26, 2010-02-27, 1998-06-18, 2017-12-2…
#> $ drug_exposure_m30_to_m1 <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…

As before, instead of a flag, we could also add count, date, or days variables.

cdm$ankle_sprain |>
  addTableIntersectCount(
    tableName = "drug_exposure",
    indexDate = "cohort_start_date",
    window = c(-180, -1)
  ) |>
  glimpse()
#> Rows: ??
#> Columns: 5
#> Database: DuckDB v1.0.0 [root@Darwin 23.4.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpzA2J8b/file751332d13f7c.duckdb]
#> $ cohort_definition_id     <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ subject_id               <int> 5199, 4222, 3308, 1211, 3330, 408, 3047, 3701…
#> $ cohort_start_date        <date> 1975-01-20, 1998-07-29, 1977-12-18, 1966-12-…
#> $ cohort_end_date          <date> 1975-02-17, 1998-08-26, 1978-01-22, 1966-12-…
#> $ drug_exposure_m180_to_m1 <dbl> 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, …

cdm$ankle_sprain |>
  addTableIntersectDate(
    tableName = "drug_exposure",
    indexDate = "cohort_start_date",
    order = "last",
    window = c(-180, -1)
  ) |>
  glimpse()
#> Rows: ??
#> Columns: 5
#> Database: DuckDB v1.0.0 [root@Darwin 23.4.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpzA2J8b/file751332d13f7c.duckdb]
#> $ cohort_definition_id     <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ subject_id               <int> 5199, 4222, 3308, 4121, 1211, 2846, 3330, 408…
#> $ cohort_start_date        <date> 1975-01-20, 1998-07-29, 1977-12-18, 1925-11-…
#> $ cohort_end_date          <date> 1975-02-17, 1998-08-26, 1978-01-22, 1925-12-…
#> $ drug_exposure_m180_to_m1 <date> 1974-08-21, 1998-05-19, 1977-08-02, 1925-09-…


cdm$ankle_sprain |>
  addTableIntersectDate(
    tableName = "drug_exposure",
    indexDate = "cohort_start_date",
    order = "last",
    window = c(-180, -1)
  ) |>
  glimpse()
#> Rows: ??
#> Columns: 5
#> Database: DuckDB v1.0.0 [root@Darwin 23.4.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpzA2J8b/file751332d13f7c.duckdb]
#> $ cohort_definition_id     <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ subject_id               <int> 5199, 4222, 3308, 4121, 1211, 2846, 3330, 408…
#> $ cohort_start_date        <date> 1975-01-20, 1998-07-29, 1977-12-18, 1925-11-…
#> $ cohort_end_date          <date> 1975-02-17, 1998-08-26, 1978-01-22, 1925-12-…
#> $ drug_exposure_m180_to_m1 <date> 1974-08-21, 1998-05-19, 1977-08-02, 1925-09-…

In these examples we’ve been adding intersections using the entire drug exposure concept table. However, we could have subsetted it before adding our table intersection. For example, let’s say we want to add a variable for acetaminophen use among our ankle sprain cohort. As we’ve seen before we could use a cohort or concept set for this, but now we have another option - subset the drug exposure table down to acetaminophen records and add a table intersection.

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

cdm$acetaminophen_records <- cdm$drug_exposure |>
  filter(drug_concept_id %in% !!acetaminophen_cs[[1]]) |>
  compute()

cdm$ankle_sprain |>
  addTableIntersectFlag(
    tableName = "acetaminophen_records",
    indexDate = "cohort_start_date",
    targetStartDate = "drug_exposure_start_date",
    targetEndDate = "drug_exposure_end_date",
    window = c(-Inf, Inf)
  ) |>
  glimpse()
#> Rows: ??
#> Columns: 5
#> Database: DuckDB v1.0.0 [root@Darwin 23.4.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpzA2J8b/file751332d13f7c.duckdb]
#> $ cohort_definition_id              <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ subject_id                        <int> 1117, 1145, 1214, 2210, 2461, 3064, …
#> $ cohort_start_date                 <date> 1937-11-12, 1961-10-22, 1947-10-08,…
#> $ cohort_end_date                   <date> 1937-12-03, 1961-11-26, 1947-11-05,…
#> $ acetaminophen_records_minf_to_inf <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …

Beyond this table intersection provides a means if implementing a wide range of custom analyses. One more example to show this is provided below, where we check whether individuals have a measurement or procedure record on the date of their ankle sprain.

cdm$proc_or_meas <- union_all(
  cdm$procedure_occurrence |>
    select("person_id",
      "record_date" = "procedure_date"
    ),
  cdm$measurement |>
    select("person_id",
      "record_date" = "measurement_date"
    )
) |>
  compute()

cdm$ankle_sprain |>
  addTableIntersectFlag(
    tableName = "proc_or_meas",
    indexDate = "cohort_start_date",
    targetStartDate = "record_date",
    targetEndDate = "record_date",
    window = c(0, 0)
  ) |>
  glimpse()
#> Rows: ??
#> Columns: 5
#> Database: DuckDB v1.0.0 [root@Darwin 23.4.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpzA2J8b/file751332d13f7c.duckdb]
#> $ cohort_definition_id <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
#> $ subject_id           <int> 1117, 1145, 1214, 2210, 2461, 3064, 3509, 3668, 4…
#> $ cohort_start_date    <date> 1937-11-12, 1961-10-22, 1947-10-08, 1958-06-02, …
#> $ cohort_end_date      <date> 1937-12-03, 1961-11-26, 1947-11-05, 1958-06-23, …
#> $ proc_or_meas_0_to_0  <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…