# Adding table intersections

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)

    CDMConnector::requireEunomia()
    con <- DBI::dbConnect(duckdb::duckdb(),
      dbdir = CDMConnector::eunomiaDir()
    )
    cdm <- CDMConnector::cdmFromCon(con, cdmSchema = "main", writeSchema = "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<ankle_sprain> [?? x 4]
    #> # Database: DuckDB v1.3.1 [root@Darwin 24.5.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpShNcsp/fileb2d93eda3c82.duckdb]
    #>    cohort_definition_id subject_id cohort_start_date cohort_end_date
    #>                   <int>      <int> <date>            <date>
    #>  1                    1          7 1985-05-16        1985-06-13
    #>  2                    1        144 1978-10-30        1978-11-20
    #>  3                    1       1138 1984-02-09        1984-03-15
    #>  4                    1       1211 2009-07-19        2009-08-02
    #>  5                    1       1357 2000-05-19        2000-06-09
    #>  6                    1       1776 1931-08-23        1931-09-13
    #>  7                    1       2071 2005-12-13        2006-01-03
    #>  8                    1       2309 1977-01-08        1977-01-22
    #>  9                    1       2311 2010-11-03        2010-12-01
    #> 10                    1       2503 1986-10-10        1986-11-07
    #> # ℹ more rows

    cdm$ankle_sprain |>
      addTableIntersectFlag(
        tableName = "condition_occurrence",
        window = c(-30, -1)
      ) |>
      tally()
    #> # Source:   SQL [?? x 1]
    #> # Database: DuckDB v1.3.1 [root@Darwin 24.5.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpShNcsp/fileb2d93eda3c82.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.3.1 [root@Darwin 24.5.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpShNcsp/fileb2d93eda3c82.duckdb]
    #> $ cohort_definition_id    <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
    #> $ subject_id              <int> 1992, 2899, 5215, 4523, 1261, 1746, 3929, 1448…
    #> $ cohort_start_date       <date> 1992-10-19, 1967-08-21, 2008-08-06, 1994-07-2…
    #> $ cohort_end_date         <date> 1992-11-23, 1967-09-25, 2008-09-10, 1994-08-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.3.1 [root@Darwin 24.5.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpShNcsp/fileb2d93eda3c82.duckdb]
    #> $ cohort_definition_id    <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
    #> $ subject_id              <int> 1992, 2899, 5215, 4523, 1261, 1746, 3929, 1448…
    #> $ cohort_start_date       <date> 1992-10-19, 1967-08-21, 2008-08-06, 1994-07-2…
    #> $ cohort_end_date         <date> 1992-11-23, 1967-09-25, 2008-09-10, 1994-08-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.3.1 [root@Darwin 24.5.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpShNcsp/fileb2d93eda3c82.duckdb]
    #> $ cohort_definition_id     <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
    #> $ subject_id               <int> 144, 1138, 1992, 3518, 763, 1761, 2845, 2899,…
    #> $ cohort_start_date        <date> 1978-10-30, 1984-02-09, 1992-10-19, 1968-08-…
    #> $ cohort_end_date          <date> 1978-11-20, 1984-03-15, 1992-11-23, 1968-08-…
    #> $ drug_exposure_m180_to_m1 <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …

    cdm$ankle_sprain |>
      addTableIntersectDate(
        tableName = "drug_exposure",
        indexDate = "cohort_start_date",
        order = "last",
        window = c(-180, -1)
      ) |>
      glimpse()
    #> Rows: ??
    #> Columns: 5
    #> Database: DuckDB v1.3.1 [root@Darwin 24.5.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpShNcsp/fileb2d93eda3c82.duckdb]
    #> $ cohort_definition_id     <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
    #> $ subject_id               <int> 144, 1138, 3518, 763, 1761, 2845, 2899, 4160,…
    #> $ cohort_start_date        <date> 1978-10-30, 1984-02-09, 1968-08-03, 1987-06-…
    #> $ cohort_end_date          <date> 1978-11-20, 1984-03-15, 1968-08-17, 1987-07-…
    #> $ drug_exposure_m180_to_m1 <date> 1978-05-26, 1984-01-05, 1968-05-08, 1987-05-…

    cdm$ankle_sprain |>
      addTableIntersectDate(
        tableName = "drug_exposure",
        indexDate = "cohort_start_date",
        order = "last",
        window = c(-180, -1)
      ) |>
      glimpse()
    #> Rows: ??
    #> Columns: 5
    #> Database: DuckDB v1.3.1 [root@Darwin 24.5.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpShNcsp/fileb2d93eda3c82.duckdb]
    #> $ cohort_definition_id     <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
    #> $ subject_id               <int> 144, 1138, 3518, 763, 1761, 2845, 2899, 4160,…
    #> $ cohort_start_date        <date> 1978-10-30, 1984-02-09, 1968-08-03, 1987-06-…
    #> $ cohort_end_date          <date> 1978-11-20, 1984-03-15, 1968-08-17, 1987-07-…
    #> $ drug_exposure_m180_to_m1 <date> 1978-05-26, 1984-01-05, 1968-05-08, 1987-05-…

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.3.1 [root@Darwin 24.5.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpShNcsp/fileb2d93eda3c82.duckdb]
    #> $ cohort_definition_id              <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
    #> $ subject_id                        <int> 7, 144, 1138, 1211, 1357, 1776, 2071…
    #> $ cohort_start_date                 <date> 1985-05-16, 1978-10-30, 1984-02-09,…
    #> $ cohort_end_date                   <date> 1985-06-13, 1978-11-20, 1984-03-15,…
    #> $ 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.3.1 [root@Darwin 24.5.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/RtmpShNcsp/fileb2d93eda3c82.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> 7, 144, 1138, 1211, 1357, 1776, 2071, 2309, 2311,…
    #> $ cohort_start_date    <date> 1985-05-16, 1978-10-30, 1984-02-09, 2009-07-19, …
    #> $ cohort_end_date      <date> 1985-06-13, 1978-11-20, 1984-03-15, 2009-08-02, …
    #> $ proc_or_meas_0_to_0  <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…