-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
186 lines (136 loc) · 6.19 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE, warn = FALSE, message = FALSE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# CohortConstructor <img src="man/figures/logo.png" align="right" height="180"/>
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/CohortConstructor)](https://CRAN.R-project.org/package=CohortConstructor)
[![R-CMD-check](https://github.com/OHDSI/CohortConstructor/workflows/R-CMD-check/badge.svg)](https://github.com/OHDSI/CohortConstructor/actions)
[![Codecov test coverage](https://codecov.io/gh/OHDSI/CohortConstructor/branch/main/graph/badge.svg)](https://app.codecov.io/gh/OHDSI/CohortConstructor?branch=main)
[![Lifecycle:Experimental](https://img.shields.io/badge/Lifecycle-Experimental-339999)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
<!-- badges: end -->
The goal of CohortConstructor is to support the creation and manipulation of study cohorts in data mapped to the OMOP CDM.
## Installation
The package can be installed from CRAN:
```{r, eval = FALSE}
install.packages("CohortConstructor")
```
Or you can install the development version of the package from GitHub:
```{r, eval = FALSE}
# install.packages("devtools")
devtools::install_github("ohdsi/CohortConstructor")
```
## Creating and manipulating cohorts
To illustrate the functionality provided by CohortConstructor let's create a cohort of people with a fracture using the Eunomia dataset. We'll first load required packages and create a cdm reference for the data.
```{r, message=FALSE, warning=FALSE}
library(omopgenerics)
library(CDMConnector)
library(PatientProfiles)
library(dplyr)
library(CohortConstructor)
library(CohortCharacteristics)
```
```{r, message=TRUE, warning=FALSE}
con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomia_dir())
cdm <- cdm_from_con(con, cdm_schema = "main",
write_schema = c(prefix = "my_study_", schema = "main"))
cdm
```
### Generating concept-based fracture cohorts
We will first need to identify codes that could be used to represent fractures of interest. To find these we'll use the CodelistGenerator package (note, we will just find a few codes because we are using synthetic data with a subset of the full vocabularies).
```{r, message=TRUE}
library(CodelistGenerator)
hip_fx_codes <- getCandidateCodes(cdm, "hip fracture")
forearm_fx_codes <- getCandidateCodes(cdm, "forearm fracture")
fx_codes <- newCodelist(list("hip_fracture" = hip_fx_codes$concept_id,
"forearm_fracture"= forearm_fx_codes$concept_id))
fx_codes
```
Now we can quickly create our cohorts. For this we only need to provide the codes we have defined and we will get a cohort back, where we start by setting cohort exit as the same day as event start (the date of the fracture).
```{r}
cdm$fractures <- cdm |>
conceptCohort(conceptSet = fx_codes,
exit = "event_start_date",
name = "fractures")
```
After creating our initial cohort we will update it so that exit is set at up to 180 days after start (so long as individuals' observation end date is on or after this - if not, exit will be at observation period end).
```{r}
cdm$fractures <- cdm$fractures |>
padCohortEnd(days = 180)
```
We can see that our starting cohorts, before we add any additional restrictions, have the following associated settings, counts, and attrition.
```{r}
settings(cdm$fractures) |> glimpse()
cohort_count(cdm$fractures) |> glimpse()
attrition(cdm$fractures) |> glimpse()
```
### Create an overall fracture cohort
So far we have created three separate fracture cohorts. Let's say we also want a cohort of people with any of the fractures. We could union our three cohorts to create this overall cohort like so:
```{r, message=FALSE}
cdm$fractures <- unionCohorts(cdm$fractures,
cohortName = "any_fracture",
keepOriginalCohorts = TRUE,
name ="fractures")
```
```{r, message=FALSE}
settings(cdm$fractures)
cohortCount(cdm$fractures)
```
### Require in date range
Once we have created our base fracture cohort, we can then start applying additional cohort requirements. For example, first we can require that individuals' cohort start date fall within a certain date range.
```{r}
cdm$fractures <- cdm$fractures |>
requireInDateRange(dateRange = as.Date(c("2000-01-01", "2020-01-01")))
```
Now that we've applied this date restriction, we can see that our cohort attributes have been updated
```{r}
cohort_count(cdm$fractures) |> glimpse()
attrition(cdm$fractures) |>
filter(reason == "cohort_start_date between 2000-01-01 & 2020-01-01") |>
glimpse()
```
### Applying demographic requirements
We can also add restrictions on patient characteristics such as age (on cohort start date by default) and sex.
```{r}
cdm$fractures <- cdm$fractures |>
requireDemographics(ageRange = list(c(40, 65)),
sex = "Female")
```
Again we can see how many individuals we've lost after applying these criteria.
```{r}
attrition(cdm$fractures) |>
filter(reason == "Age requirement: 40 to 65") |>
glimpse()
attrition(cdm$fractures) |>
filter(reason == "Sex requirement: Female") |>
glimpse()
```
### Require presence in another cohort
We can also require that individuals are (or are not) in another cohort over some window. Here for example we require that study participants are in a GI bleed cohort any time prior up to their entry in the fractures cohort.
```{r}
cdm$gibleed <- cdm |>
conceptCohort(conceptSet = list("gibleed" = 192671L),
name = "gibleed")
cdm$fractures <- cdm$fractures |>
requireCohortIntersect(targetCohortTable = "gibleed",
intersections = 0,
window = c(-Inf, 0))
```
```{r}
attrition(cdm$fractures) |>
filter(reason == "Not in cohort gibleed between -Inf & 0 days relative to cohort_start_date") |>
glimpse()
```
```{r}
cdmDisconnect(cdm)
```
### More information
CohortConstructor provides much more functionality for creating and manipulating cohorts. See the package vignettes for more details.