From 9e42a9d3489d589e62f9e741e41e1d32edc61272 Mon Sep 17 00:00:00 2001 From: edward-burn <9583964+edward-burn@users.noreply.github.com> Date: Wed, 23 Oct 2024 01:30:23 +0100 Subject: [PATCH] some vignette tweaks --- vignettes/a01_building_base_cohorts.Rmd | 61 ++++----- vignettes/a04_require_intersections.Rmd | 157 ++++++------------------ 2 files changed, 60 insertions(+), 158 deletions(-) diff --git a/vignettes/a01_building_base_cohorts.Rmd b/vignettes/a01_building_base_cohorts.Rmd index 93fd1673..a0577598 100644 --- a/vignettes/a01_building_base_cohorts.Rmd +++ b/vignettes/a01_building_base_cohorts.Rmd @@ -41,56 +41,41 @@ cdm <- cdm_from_con(con, cdm_schema = "main", write_schema = c(prefix = "my_study_", schema = "main")) ``` -## Demographic based cohort creation +## Concept based cohort creation -One base cohort we can create is based around patient demographics. Here for example we create a cohort where people enter on their 18th birthday and leave at age 65 or +A way of defining base cohorts is to identify clinical records with codes from some pre-specified list. Here for example we'll first find codes for diclofenac and acetaminophen. ```{r} -cdm$working_age_cohort <- demographicsCohort(cdm = cdm, - ageRange = c(18, 65), - name = "working_age_cohort") +drug_codes <- getDrugIngredientCodes(cdm, + name = c("acetaminophen", + "amoxicillin", + "diclofenac", + "simvastatin", + "warfarin")) -settings(cdm$working_age_cohort) -cohortCount(cdm$working_age_cohort) -attrition(cdm$working_age_cohort) +drug_codes ``` +Now we have our codes of interest, we'll make cohorts for each of these where cohort exit is defined as the event start date (which for these will be their drug exposure end date). ```{r} -cdm$working_age_cohort |> - addAge(indexDate = "cohort_start_date") |> - summarise(min_start_age = min(age), - median_start_age = median(age), - max_start_age = max(age)) - -cdm$working_age_cohort |> - addAge(indexDate = "cohort_end_date") |> - summarise(min_start_age = min(age), - median_start_age = median(age), - max_start_age = max(age)) +cdm$drugs <- conceptCohort(cdm, + conceptSet = drug_codes, + exit = "event_end_date", + name = "drugs") + +settings(cdm$drugs) +cohortCount(cdm$drugs) +attrition(cdm$drugs) ``` -## Concept based cohort creation - -```{r} -drug_codes <- getDrugIngredientCodes(cdm, - name = c("diclofenac", - "acetaminophen")) +## Demographic based cohort creation -drug_codes -``` +One base cohort we can create is based around patient demographics. Here for example we create a cohort where people enter on their 18th birthday and leave at on the day before their 66th birthday. ```{r} -cdm$medications <- conceptCohort(cdm = cdm, - conceptSet = drug_codes, - name = "medications") - -settings(cdm$medications) -cohortCount(cdm$medications) +cdm$working_age_cohort <- demographicsCohort(cdm = cdm, + ageRange = c(18, 65), + name = "working_age_cohort") ``` -## Concept based cohort creation for measurements - -TO DO - - diff --git a/vignettes/a04_require_intersections.Rmd b/vignettes/a04_require_intersections.Rmd index 03dd19e1..ea702c3b 100644 --- a/vignettes/a04_require_intersections.Rmd +++ b/vignettes/a04_require_intersections.Rmd @@ -1,5 +1,5 @@ --- -title: "Requirements on Presence and Absence" +title: "Requirements related to other cohorts, concept sets, or tables" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{a04_require_intersections} @@ -14,8 +14,10 @@ knitr::opts_chunk$set( ``` ```{r setup} +library(CodelistGenerator) library(CohortConstructor) library(CohortCharacteristics) +library(visOmopResults) library(ggplot2) ``` @@ -44,85 +46,71 @@ cdm <- cdm_from_con(con, cdm_schema = "main", write_schema = c(prefix = "my_study_", schema = "main")) ``` -Let's start by creating two drug cohorts, one for users of diclofenac and another for users of acetaminophen. +Let's start by creating a cohort of warfarin users. ```{r} -cdm$medications <- conceptCohort(cdm = cdm, - conceptSet = list("diclofenac" = 1124300, - "acetaminophen" = 1127433), - name = "medications") -cohortCount(cdm$medications) +warfarin_codes <- getDrugIngredientCodes(cdm, "warfarin") +cdm$warfarin <- conceptCohort(cdm = cdm, + conceptSet = warfarin_codes, + name = "warfarin") +cohortCount(cdm$warfarin) ``` -As well as our medication cohorts, let's also make another cohort containing individuals with a record of a GI bleed. Later we'll use this cohort when specifying inclusion/ exclusion criteria. +As well as our warfarin cohort, let's also make another cohort containing individuals with a record of a GI bleed. Later we'll use this cohort when specifying inclusion/ exclusion criteria. ```{r} cdm$gi_bleed <- conceptCohort(cdm = cdm, - conceptSet = list("gi_bleed" = 192671), + conceptSet = list("gi_bleed" = 192671L), name = "gi_bleed") ``` ## Restrictions on cohort presence -We could require that individuals in our medication cohorts are seen (or not seen) in another cohort. To do this we can use the `requireCohortIntersect()` function, requiring that individuals have one or more intersections with the GI bleed cohort. +We could require that individuals in our medication cohorts are seen (or not seen) in another cohort. To do this we can use the `requireCohortIntersect()` function. Here, for example, we require that individuals have one or more intersections with the GI bleed cohort. ```{r} -cdm$medications_gi_bleed <- cdm$medications %>% +cdm$warfarin_gi_bleed <- cdm$warfarin %>% requireCohortIntersect(intersections = c(1,Inf), targetCohortTable = "gi_bleed", targetCohortId = 1, indexDate = "cohort_start_date", window = c(-Inf, 0), - name = "medications_gi_bleed") + name = "warfarin_gi_bleed") -summary_attrition <- summariseCohortAttrition(cdm$medications_gi_bleed) -plotCohortAttrition(summary_attrition, cohortId = 1) +summary_attrition <- summariseCohortAttrition(cdm$warfarin_gi_bleed) +plotCohortAttrition(summary_attrition) ``` -The flow chart above illustrates the changes to cohort 1 (users of acetaminophen) when restricted to only include individuals who intersect with the GI bleed cohort at least once before the cohort start date. 2,296 individuals and 8,765 records were excluded. +The flow chart above illustrates the changes to the cohort of users of acetaminophen when restricted to only include individuals who have at least one record in the GI bleed cohort before their start date for acetaminophen. -Instead of requiring that individuals intersect with the GI bleed cohort, we could instead require that they don't intersect with it. In this case we can again use the `requireCohortIntersect()` function, but this time set the intersections argument to 0 to require individuals' absence in this other cohort rather than their presence in it. +Instead of requiring that individuals have a record in the GI bleed cohort, we could instead require that they don't. In this case we can again use the `requireCohortIntersect()` function, but this time we set the intersections argument to 0 so as to require individuals' absence in this other cohort. ```{r} -cdm$medications_no_gi_bleed <- cdm$medications %>% +cdm$warfarin_no_gi_bleed <- cdm$warfarin %>% requireCohortIntersect(intersections = 0, targetCohortTable = "gi_bleed", targetCohortId = 1, indexDate = "cohort_start_date", window = c(-Inf, 0), - name = "medications_no_gi_bleed") + name = "warfarin_no_gi_bleed") -summary_attrition <- summariseCohortAttrition(cdm$medications_no_gi_bleed) -plotCohortAttrition(summary_attrition, cohortId = 1) +summary_attrition <- summariseCohortAttrition(cdm$warfarin_no_gi_bleed) +plotCohortAttrition(summary_attrition) ``` -The flow chart above illustrates the changes to cohort 1 when restricted to only include individuals with no intersects with the GI bleed cohort before the cohort start date. 36 individuals and 600 records were excluded. - - ## Restrictions on concept presence We could require that individuals in our medication cohorts have been seen (or not seen) to have events related to a concept list. To do this we can use the `requireConceptIntersect()` function, allowing us to filter our cohort based on whether they have or have not had events of GI bleeding before they entered the cohort. -```{r, include = FALSE} -cdm$medications <- conceptCohort(cdm = cdm, - conceptSet = list("diclofenac" = 1124300, - "acetaminophen" = 1127433), - name = "medications") - -cdm$gi_bleed <- conceptCohort(cdm = cdm, - conceptSet = list("gi_bleed" = 192671), - name = "gi_bleed") -``` - ```{r} -cdm$medications_gi_bleed <- cdm$medications %>% +cdm$warfarin_gi_bleed <- cdm$warfarin %>% requireConceptIntersect(conceptSet = list("gi_bleed" = 192671), indexDate = "cohort_start_date", window = c(-Inf, 0), - name = "medications_gi_bleed") + name = "warfarin_gi_bleed") -summary_attrition <- summariseCohortAttrition(cdm$medications_gi_bleed) -plotCohortAttrition(summary_attrition, cohortId = 1) +summary_attrition <- summariseCohortAttrition(cdm$warfarin_gi_bleed) +plotCohortAttrition(summary_attrition) ``` The flow chart above illustrates the changes to cohort 1 when restricted to only include individuals who have had events of GI bleeding at least once before the cohort start date. 2,296 individuals and 8,765 records were excluded. @@ -130,99 +118,28 @@ The flow chart above illustrates the changes to cohort 1 when restricted to onl Instead of requiring that individuals have events of GI bleeding, we could instead require that they don't have any events of it. In this case we can again use the `requireConceptIntersect()` function, but this time set the intersections argument to 0 to require individuals without past events of GI bleeding. ```{r} -cdm$medications_no_gi_bleed <- cdm$medications %>% +cdm$warfarin_no_gi_bleed <- cdm$warfarin %>% requireConceptIntersect(intersections = 0, conceptSet = list("gi_bleed" = 192671), indexDate = "cohort_start_date", window = c(-Inf, 0), - name = "medications_no_gi_bleed") + name = "warfarin_no_gi_bleed") -summary_attrition <- summariseCohortAttrition(cdm$medications_no_gi_bleed) -plotCohortAttrition(summary_attrition, cohortId = 1) +summary_attrition <- summariseCohortAttrition(cdm$warfarin_no_gi_bleed) +plotCohortAttrition(summary_attrition) ``` -The flow chart above illustrates the changes to cohort 1 when restricted to only include individuals who have not had events of GI bleeding before the cohort start date. 36 individuals and 600 records were excluded. - ## Restrictions on presence in clinical tables -A clinical table is a table which contains 'raw' clinical data. We can use clinical tables to filter our cohorts using the `requireTableIntersect()` function. This will allow us to filter individuals in the medications cohort based on whether they have intersections with the GI bleed clinical table or not. - -```{r, include = FALSE} -cdm$medications <- conceptCohort(cdm = cdm, - conceptSet = list("diclofenac" = 1124300, - "acetaminophen" = 1127433), - name = "medications") - -cdm$gi_bleed <- conceptCohort(cdm = cdm, - conceptSet = list("gi_bleed" = 192671), - name = "gi_bleed") -``` - -```{r} -cdm$medications_gi_bleed <- cdm$medications %>% - requireTableIntersect(tableName = "gi_bleed", - indexDate = "cohort_start_date", - window = c(-Inf, 0), - name = "medications_gi_bleed") - -summary_attrition <- summariseCohortAttrition(cdm$medications_gi_bleed) -plotCohortAttrition(summary_attrition, cohortId = 1) -``` - -The flow chart above illustrates the changes to cohort 1 when restricted to only include individuals who intersect with the GI bleeding clinical table at least once before the cohort start date. 2,296 individuals and 8,765 records were excluded. - -Instead of requiring that individuals intersect with the GI bleed clinical table, we could instead require that they don't intersect with it. In this case we can again use the `requireCohortIntersect()` function, but this time set the intersections argument to 0 to require individuals' absence in the GI bleed clinical table. +We can also impose requirements around individuals presence (or absence) in clinical tables in the OMOP CDM using the `requireTableIntersect()` function. Here for example we reuire that individuals in our warfarin cohort have at least one prior record in the visit occurrence table. ```{r} -cdm$medications_no_gi_bleed <- cdm$medications %>% - requireTableIntersect(tableName = "gi_bleed", +cdm$warfarin_visit <- cdm$warfarin %>% + requireTableIntersect(tableName = "visit_occurrence", indexDate = "cohort_start_date", - window = c(-Inf, 0), - name = "medications_no_gi_bleed", - intersections = 0) - -summary_attrition <- summariseCohortAttrition(cdm$medications_no_gi_bleed) -plotCohortAttrition(summary_attrition, cohortId = 1) -``` - -The flow chart above illustrates the changes to cohort 1 when restricted to only include individuals who have no intersects with the GI bleeding clinical table before the cohort start date. 36 individuals and 600 records were excluded. - -## Restrictions on deaths - -We could require that individuals in our medication cohorts are seen to have (or not have) a death. To do this we can use the `requireDeathFlag()` function, requiring that individuals are seen (or not seen) to have died after the cohort start date. - -```{r, include = FALSE} -cdm$medications <- conceptCohort(cdm = cdm, - conceptSet = list("diclofenac" = 1124300, - "acetaminophen" = 1127433), - name = "medications") - -cdm$gi_bleed <- conceptCohort(cdm = cdm, - conceptSet = list("gi_bleed" = 192671), - name = "gi_bleed") -``` - -```{r} -cdm$medications_deaths <- cdm$medications %>% - requireDeathFlag(window = c(0,Inf), - name = "medications_deaths") + window = c(-Inf, -1), + name = "warfarin_visit") -summary_attrition <- summariseCohortAttrition(cdm$medications_deaths) -plotCohortAttrition(summary_attrition, cohortId = 1) +summary_attrition <- summariseCohortAttrition(cdm$warfarin_visit) +plotCohortAttrition(summary_attrition) ``` - -The flow chart above illustrates the changes to cohort 1 when restricted to only include individuals who died after the cohort start date. None of the individuals in cohort 1 died and therefore they are all excluded from this cohort. - -To exclude individuals who died we add the argument 'negate = TRUE' to the function `requireDeathFlag()`. - -```{r} -cdm$medications_no_deaths <- cdm$medications %>% - requireDeathFlag(window = c(0,Inf), - name = "medications_no_deaths", - negate = TRUE) - -summary_attrition <- summariseCohortAttrition(cdm$medications_no_deaths) -plotCohortAttrition(summary_attrition, cohortId = 1) -``` - -The flow chart above illustrates the changes to cohort 1 when restricted to only include individuals who did not die after the cohort start date. None of the individuals in cohort 1 died and therefore no one was excluded.