9  Working with cohorts

9.1 Cohort intersections

PatientProfiles::addCohortIntersect()

9.2 Intersection between two cohorts

9.3 Set up

library(CDMConnector)
library(dplyr)
library(PatientProfiles)

# For this example we will use GiBleed data set
CDMConnector::downloadEunomiaData(datasetName = "GiBleed")        
db <- DBI::dbConnect(duckdb::duckdb(), eunomia_dir())

cdm <- cdmFromCon(db, cdmSchema = "main", writeSchema = "main")

cdm <- cdm |> 
  generate_concept_cohort_set(concept_set = list("gi_bleed" = 192671), 
                            limit = "all", 
                            end = 30,
                            name = "gi_bleed",
                            overwrite = TRUE) |> 
  generate_concept_cohort_set(concept_set = list("acetaminophen" = c(1125315,
                                                              1127078,
                                                              1127433,
                                                              40229134,
                                                              40231925,
                                                              40162522,
                                                              19133768)), 
                              limit = "all", 
                            # end = "event_end_date",
                            name = "acetaminophen",
                            overwrite = TRUE)

9.3.1 Flag

cdm$gi_bleed <- cdm$gi_bleed |> 
  addCohortIntersectFlag(targetCohortTable = "acetaminophen",
                         window = list(c(-Inf, -1), c(0,0), c(1, Inf)))

cdm$gi_bleed |> 
  summarise(acetaminophen_prior = sum(acetaminophen_minf_to_m1), 
            acetaminophen_index = sum(acetaminophen_0_to_0),
            acetaminophen_post = sum(acetaminophen_1_to_inf)) |> 
  collect()
# A tibble: 1 × 3
  acetaminophen_prior acetaminophen_index acetaminophen_post
                <dbl>               <dbl>              <dbl>
1                 467                 467                476

9.3.2 Count

9.3.3 Date and times

9.4 Intersection between a cohort and tables with patient data

10 Further reading