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.
#> 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)
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.1.3 [unknown@Linux 6.8.0-1017-azure:R 4.4.2//tmp/RtmpF9hwFG/file22773ec574d8.duckdb]
#> cohort_definition_id subject_id cohort_start_date cohort_end_date
#> <int> <int> <date> <date>
#> 1 1 445 1951-08-05 1951-08-19
#> 2 1 474 1982-12-02 1982-12-16
#> 3 1 655 1999-06-06 1999-07-04
#> 4 1 870 2002-11-11 2002-12-02
#> 5 1 1000 1995-02-08 1995-03-08
#> 6 1 1021 1993-09-10 1993-10-15
#> 7 1 1124 1971-10-02 1971-10-16
#> 8 1 1427 1982-03-13 1982-04-03
#> 9 1 1520 1955-02-19 1955-03-12
#> 10 1 1569 1973-01-14 1973-02-18
#> # ℹ more rows
cdm$ankle_sprain |>
addTableIntersectFlag(
tableName = "condition_occurrence",
window = c(-30, -1)
) |>
tally()
#> # Source: SQL [?? x 1]
#> # Database: DuckDB v1.1.3 [unknown@Linux 6.8.0-1017-azure:R 4.4.2//tmp/RtmpF9hwFG/file22773ec574d8.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.1.3 [unknown@Linux 6.8.0-1017-azure:R 4.4.2//tmp/RtmpF9hwFG/file22773ec574d8.duckdb]
#> $ cohort_definition_id <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
#> $ subject_id <int> 2287, 608, 719, 1602, 2899, 5045, 2821, 1448, …
#> $ cohort_start_date <date> 1987-12-12, 2004-10-21, 1983-08-31, 2013-04-1…
#> $ cohort_end_date <date> 1988-01-09, 2004-11-04, 1983-09-28, 2013-05-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.1.3 [unknown@Linux 6.8.0-1017-azure:R 4.4.2//tmp/RtmpF9hwFG/file22773ec574d8.duckdb]
#> $ cohort_definition_id <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
#> $ subject_id <int> 2287, 608, 719, 1602, 2899, 5045, 2821, 1448, …
#> $ cohort_start_date <date> 1987-12-12, 2004-10-21, 1983-08-31, 2013-04-1…
#> $ cohort_end_date <date> 1988-01-09, 2004-11-04, 1983-09-28, 2013-05-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.1.3 [unknown@Linux 6.8.0-1017-azure:R 4.4.2//tmp/RtmpF9hwFG/file22773ec574d8.duckdb]
#> $ cohort_definition_id <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ subject_id <int> 1569, 2616, 3703, 3268, 608, 2149, 2149, 2318…
#> $ cohort_start_date <date> 1973-01-14, 2017-04-01, 1969-12-18, 1970-07-…
#> $ cohort_end_date <date> 1973-02-18, 2017-04-29, 1970-01-08, 1970-08-…
#> $ drug_exposure_m180_to_m1 <dbl> 3, 1, 1, 1, 2, 4, 1, 1, 1, 1, 2, 1, 2, 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.1.3 [unknown@Linux 6.8.0-1017-azure:R 4.4.2//tmp/RtmpF9hwFG/file22773ec574d8.duckdb]
#> $ cohort_definition_id <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ subject_id <int> 1569, 1980, 2616, 3703, 3268, 2149, 2149, 305…
#> $ cohort_start_date <date> 1973-01-14, 2014-05-31, 2017-04-01, 1969-12-…
#> $ cohort_end_date <date> 1973-02-18, 2014-06-14, 2017-04-29, 1970-01-…
#> $ drug_exposure_m180_to_m1 <date> 1972-10-08, 2014-02-09, 2017-02-07, 1969-08-…
cdm$ankle_sprain |>
addTableIntersectDate(
tableName = "drug_exposure",
indexDate = "cohort_start_date",
order = "last",
window = c(-180, -1)
) |>
glimpse()
#> Rows: ??
#> Columns: 5
#> Database: DuckDB v1.1.3 [unknown@Linux 6.8.0-1017-azure:R 4.4.2//tmp/RtmpF9hwFG/file22773ec574d8.duckdb]
#> $ cohort_definition_id <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ subject_id <int> 1569, 1980, 2616, 3703, 3268, 2149, 2149, 305…
#> $ cohort_start_date <date> 1973-01-14, 2014-05-31, 2017-04-01, 1969-12-…
#> $ cohort_end_date <date> 1973-02-18, 2014-06-14, 2017-04-29, 1970-01-…
#> $ drug_exposure_m180_to_m1 <date> 1972-10-08, 2014-02-09, 2017-02-07, 1969-08-…
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.1.3 [unknown@Linux 6.8.0-1017-azure:R 4.4.2//tmp/RtmpF9hwFG/file22773ec574d8.duckdb]
#> $ cohort_definition_id <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ subject_id <int> 655, 870, 1124, 1427, 1520, 1569, 22…
#> $ cohort_start_date <date> 1999-06-06, 2002-11-11, 1971-10-02,…
#> $ cohort_end_date <date> 1999-07-04, 2002-12-02, 1971-10-16,…
#> $ 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.1.3 [unknown@Linux 6.8.0-1017-azure:R 4.4.2//tmp/RtmpF9hwFG/file22773ec574d8.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> 655, 870, 1124, 1427, 1520, 1569, 2258, 2287, 228…
#> $ cohort_start_date <date> 1999-06-06, 2002-11-11, 1971-10-02, 1982-03-13, …
#> $ cohort_end_date <date> 1999-07-04, 2002-12-02, 1971-10-16, 1982-04-03, …
#> $ proc_or_meas_0_to_0 <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…