-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
263 lines (212 loc) · 7.25 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
warning = FALSE, message = FALSE,
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# PatientProfiles <img src="man/figures/logo.png" align="right" height="200"/>
[](https://CRAN.R-project.org/package=PatientProfiles)
[](https://app.codecov.io/github/darwin-eu/PatientProfiles?branch=main)
[](https://github.com/darwin-eu/PatientProfiles/actions)
[](https://lifecycle.r-lib.org/articles/stages.html#stable)
[](https://cran.r-project.org/package=PatientProfiles)
[](https://cran.r-project.org/package=PatientProfiles)
## Package overview
PatientProfiles contains functions for adding characteristics to OMOP CDM tables containing patient level data (e.g. condition occurrence, drug exposure, and so on) and OMOP CDM cohort tables. The characteristics that can be added include an individual´s sex, age, and days of prior observation Time varying characteristics, such as age, can be estimated relative to any date in the corresponding table. In addition, PatientProfiles also provides functionality for identifying intersections between a cohort table and OMOP CDM tables containing patient level data or other cohort tables.
## Package installation
You can install the latest version of PatientProfiles like so:
```{r, eval=FALSE}
install.packages("PatientProfiles")
```
## Citation
```{r}
citation("PatientProfiles")
```
## Example usage
### Create a reference to data in the OMOP CDM format
The PatientProfiles package is designed to work with data in the OMOP CDM format, so our first step is to create a reference to the data using the CDMConnector package.
```{r, message=FALSE}
library(CDMConnector)
library(PatientProfiles)
library(dplyr)
```
Creating a connection to a Postgres database would for example look like:
```{r, eval=FALSE}
con <- DBI::dbConnect(
RPostgres::Postgres(),
dbname = Sys.getenv("CDM5_POSTGRESQL_DBNAME"),
host = Sys.getenv("CDM5_POSTGRESQL_HOST"),
user = Sys.getenv("CDM5_POSTGRESQL_USER"),
password = Sys.getenv("CDM5_POSTGRESQL_PASSWORD")
)
cdm <- cdm_from_con(
con,
cdm_schema = Sys.getenv("CDM5_POSTGRESQL_CDM_SCHEMA"),
write_schema = Sys.getenv("CDM5_POSTGRESQL_RESULT_SCHEMA")
)
```
To see how you would create a reference to your database please consult the CDMConnector package [documentation](https://darwin-eu.github.io/CDMConnector/articles/a04_DBI_connection_examples.html). For this example though we'll work with simulated data, and we'll generate an example cdm reference like so:
```{r}
cdm <- mockPatientProfiles(numberIndividuals = 1000)
```
### Adding individuals´ characteristics
#### Adding characteristics to patient-level data
Say we wanted to get individuals´sex and age at condition start date for records in the condition occurrence table. We can use the `addAge` and `addSex` functions to do this:
```{r}
cdm$condition_occurrence |>
glimpse()
cdm$condition_occurrence <- cdm$condition_occurrence |>
addAge(indexDate = "condition_start_date") |>
addSex()
cdm$condition_occurrence |>
glimpse()
```
We could, for example, then limit our data to only males aged between 18 and 65
```{r}
cdm$condition_occurrence |>
filter(age >= 18 & age <= 65) |>
filter(sex == "Male")
```
#### Adding characteristics of a cohort
As with other tables in the OMOP CDM, we can work in a similar way with cohort tables. For example, say we have the below cohort table
```{r}
cdm$cohort1 |>
glimpse()
```
We can add age, age groups, sex, and days of prior observation to a cohort like so
```{r}
cdm$cohort1 <- cdm$cohort1 |>
addAge(
indexDate = "cohort_start_date",
ageGroup = list(c(0, 18), c(19, 65), c(66, 100))
) |>
addSex() |>
addPriorObservation()
cdm$cohort1 |>
glimpse()
```
We could use this information to subset the cohort. For example limiting to those with at least 365 days of prior observation available before their cohort start date like so
```{r}
cdm$cohort1 |>
filter(prior_observation >= 365)
```
### Cohort intersections
#### Detect the presence of another cohort in a certain window
We can use `addCohortIntersectFlag` to add a flag for the presence (or not) of a cohort in a certain window.
```{r, echo = FALSE}
cdm <- mockPatientProfiles()
```
```{r}
cdm$cohort1 |>
glimpse()
cdm$cohort1 <- cdm$cohort1 |>
addCohortIntersectFlag(
targetCohortTable = "cohort2",
window = c(-Inf, -1)
)
cdm$cohort1 |>
glimpse()
```
#### Count appearances of a certain cohort in a certain window
If we wanted the number of appearances, we could instead use the `addCohortIntersectCount` function
```{r, echo = FALSE}
cdm <- mockPatientProfiles()
```
```{r}
cdm$cohort1 |>
glimpse()
cdm$cohort1 <- cdm$cohort1 |>
addCohortIntersectCount(
targetCohortTable = "cohort2",
targetCohortId = 1,
window = list("short_term" = c(1, 30), "mid_term" = c(31, 180))
)
cdm$cohort1 |>
glimpse()
```
#### Add a column with the first/last event in a certain window
Say we wanted the date at which an individual was in another cohort then we can use the `addCohortIntersectDate` function. As there might be multiple records for the other cohort, we can also choose the first or the last appearance in that cohort.
First occurrence:
```{r, echo = FALSE}
cdm <- mockPatientProfiles()
```
```{r}
cdm$cohort1 |>
glimpse()
cdm$cohort1 <- cdm$cohort1 |>
addCohortIntersectDate(
targetCohortTable = "cohort2",
targetCohortId = 1,
order = "first",
window = c(-Inf, Inf)
)
cdm$cohort1 |>
glimpse()
```
Last occurrence:
```{r, echo = FALSE}
cdm <- mockPatientProfiles()
```
```{r}
cdm$cohort1 |>
glimpse()
cdm$cohort1 <- cdm$cohort1 |>
addCohortIntersectDate(
targetCohortTable = "cohort2",
targetCohortId = 1,
order = "last",
window = c(-Inf, Inf)
)
cdm$cohort1 |>
glimpse()
```
#### Add the number of days instead of the date
Instead of returning a date, we could return the days to the intersection by using `addCohortIntersectDays`
```{r, echo = FALSE}
cdm <- mockPatientProfiles()
```
```{r}
cdm$cohort1 |>
glimpse()
cdm$cohort1 <- cdm$cohort1 |>
addCohortIntersectDays(
targetCohortTable = "cohort2",
targetCohortId = 1,
order = "last",
window = c(-Inf, Inf)
)
cdm$cohort1 |>
glimpse()
```
#### Combine multiple cohort intersects
If we want to combine multiple cohort intersects we can concatenate the operations using the `pipe` operator:
```{r, echo = FALSE}
cdm <- mockPatientProfiles()
```
```{r}
cdm$cohort1 |>
glimpse()
cdm$cohort1 <- cdm$cohort1 |>
addCohortIntersectDate(
targetCohortTable = "cohort2",
targetCohortId = 1,
order = "last",
window = c(-Inf, Inf)
) |>
addCohortIntersectCount(
targetCohortTable = "cohort2",
targetCohortId = 1,
window = c(-Inf, Inf)
)
cdm$cohort1 |>
glimpse()
```
```{r}
mockDisconnect(cdm)
```