Applying demographic requirements to a cohort

library(CodelistGenerator)
library(CohortConstructor)
library(CohortCharacteristics)
library(ggplot2)

In this vignette we’ll show how requirements related to patient demographics can be applied to a cohort. Again we’ll use the Eunomia synthetic data.

con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir())
cdm <- CDMConnector::cdmFromCon(con, cdmSchema = "main", 
                    writeSchema = "main", writePrefix = "my_study_")

Let’s start by creating a cohort of people with a fracture. We’ll first look for codes that might represent a fracture and the build a cohort using these codes, setting cohort exit to 180 days after the fracture.

fracture_codes <- getCandidateCodes(cdm, "fracture")
fracture_codes <- list("fracture" = fracture_codes$concept_id)
cdm$fracture <- conceptCohort(cdm = cdm, 
                                 conceptSet = fracture_codes, 
                                 name = "fracture")

summary_attrition <- summariseCohortAttrition(cdm$fracture)
plotCohortAttrition(summary_attrition)
%0 1->2 2->3 3->4 6->5 8->7 10->9 1 Initial events N subjects = 1,596 N records = 2,522 2 N subjects = 1,596 N records = 2,522 3 N subjects = 1,596 N records = 2,522 4 Final events N subjects = 1,596 N records = 2,445 5 N subjects = 0 N records = 0 6 Record start <= record end 7 N subjects = 0 N records = 0 8 Record in observation 9 N subjects = 0 N records = 77 10 Merge overlapping records

Restrict cohort by age

We can choose a specific age range for individuals in our cohort using requireAge() from CohortConstructor.

cdm$fracture <- cdm$fracture |> 
  requireAge(indexDate = "cohort_start_date",
             ageRange = list(c(18, 100)))

summary_attrition <- summariseCohortAttrition(cdm$fracture)
plotCohortAttrition(summary_attrition)
%0 1->2 2->3 3->4 4->5 7->6 9->8 11->10 13->12 1 Initial events N subjects = 1,596 N records = 2,522 2 N subjects = 1,596 N records = 2,522 3 N subjects = 1,596 N records = 2,522 4 N subjects = 1,596 N records = 2,445 5 Final events N subjects = 859 N records = 1,148 6 N subjects = 0 N records = 0 7 Record start <= record end 8 N subjects = 0 N records = 0 9 Record in observation 10 N subjects = 0 N records = 77 11 Merge overlapping records 12 N subjects = 737 N records = 1,297 13 Age requirement: 18 to 100

Note that by default individuals are filtered based on the age they were when they entered the cohort.

Restrict cohort by sex

We can also specify a sex criteria for individuals in our cohort using requireSex() from CohortConstructor.

cdm$fracture <- cdm$fracture |> 
  requireSex(sex = "Female")

summary_attrition <- summariseCohortAttrition(cdm$fracture)
plotCohortAttrition(summary_attrition)
%0 1->2 2->3 3->4 4->5 5->6 8->7 10->9 12->11 14->13 16->15 1 Initial events N subjects = 1,596 N records = 2,522 2 N subjects = 1,596 N records = 2,522 3 N subjects = 1,596 N records = 2,522 4 N subjects = 1,596 N records = 2,445 5 N subjects = 859 N records = 1,148 6 Final events N subjects = 447 N records = 604 7 N subjects = 0 N records = 0 8 Record start <= record end 9 N subjects = 0 N records = 0 10 Record in observation 11 N subjects = 0 N records = 77 12 Merge overlapping records 13 N subjects = 737 N records = 1,297 14 Age requirement: 18 to 100 15 N subjects = 412 N records = 544 16 Sex requirement: Female

Restrict cohort by number of prior observations

We can also specify a minimum number of days of prior observations for each individual using requirePriorObservation() from CohortConstructor.

cdm$fracture <- cdm$fracture |> 
  requirePriorObservation(indexDate = "cohort_start_date",
                          minPriorObservation = 365)

summary_attrition <- summariseCohortAttrition(cdm$fracture)
plotCohortAttrition(summary_attrition)
%0 1->2 2->3 3->4 4->5 5->6 6->7 9->8 11->10 13->12 15->14 17->16 19->18 1 Initial events N subjects = 1,596 N records = 2,522 2 N subjects = 1,596 N records = 2,522 3 N subjects = 1,596 N records = 2,522 4 N subjects = 1,596 N records = 2,445 5 N subjects = 859 N records = 1,148 6 N subjects = 447 N records = 604 7 Final events N subjects = 447 N records = 604 8 N subjects = 0 N records = 0 9 Record start <= record end 10 N subjects = 0 N records = 0 11 Record in observation 12 N subjects = 0 N records = 77 13 Merge overlapping records 14 N subjects = 737 N records = 1,297 15 Age requirement: 18 to 100 16 N subjects = 412 N records = 544 17 Sex requirement: Female 18 N subjects = 0 N records = 0 19 Prior observation requirement: 365 days

As well as specifying a minimum amount of prior observation, we can require some mimimum amount of follow-up by using requireFutureObservation() in a similar way.

Applying multiple demographic requirements to a cohort

We can implement multiple demographic requirements at the same time by using the more general requireDemographics() function.

cdm$fracture <- conceptCohort(cdm = cdm, 
                                 conceptSet = fracture_codes, 
                                 name = "fracture") |> 
  requireDemographics(indexDate = "cohort_start_date",
                      ageRange = c(18,100),
                      sex = "Female",
                      minPriorObservation = 365, 
                      minFutureObservation = 30)

summary_attrition <- summariseCohortAttrition(cdm$fracture)
plotCohortAttrition(summary_attrition)
%0 1->2 2->3 3->4 4->5 5->6 6->7 7->8 10->9 12->11 14->13 16->15 18->17 20->19 22->21 1 Initial events N subjects = 1,596 N records = 2,522 2 N subjects = 1,596 N records = 2,522 3 N subjects = 1,596 N records = 2,522 4 N subjects = 1,596 N records = 2,445 5 N subjects = 859 N records = 1,148 6 N subjects = 447 N records = 604 7 N subjects = 447 N records = 604 8 Final events N subjects = 445 N records = 598 9 N subjects = 0 N records = 0 10 Record start <= record end 11 N subjects = 0 N records = 0 12 Record in observation 13 N subjects = 0 N records = 77 14 Merge overlapping records 15 N subjects = 737 N records = 1,297 16 Age requirement: 18 to 100 17 N subjects = 412 N records = 544 18 Sex requirement: Female 19 N subjects = 0 N records = 0 20 Prior observation requirement: 365 days 21 N subjects = 2 N records = 6 22 Future observation requirement: 30 days