-
Notifications
You must be signed in to change notification settings - Fork 0
/
outcome_cohorts.R
230 lines (193 loc) · 10.8 KB
/
outcome_cohorts.R
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
# ============================================================================ #
# Outcome cohorts #
# Núria Mercadé #
# 01-11-2022 #
# ============================================================================ #
## Connect to database + load packages
source("./AURUM_CDM_connection.R")
# Cohort's ID
covidDiagnosis_id <- 1 # Broad + PC + EXCL
AZvaccine_id <- 2 # Astrazeneca vaccine
PFvaccine_id <- 3 # Pfizer vaccine
covid_id <- 4 # Covid diagnosis - washout window of 6 weeks between events
VTE_id <- 5 # Venous Thromboembolism - washout window of 90 days between events
DVT_id <- 6 # Deep Vein Thrombosis - washout window of 90 days between events
PE_id <- 7 # Pulmonary Embolism - washout window of 90 days between events
HS_id <- 8 # Haemorrhagic Stroke - washout window of 90 days between events
IS_id <- 9 # Ischemic Stroke - washout window of 90 days between events
TIA_id <- 10 # Transient Ischemic Attack or Transient Cerebral Ischemia - washout window of 90 days between events
MI_id <- 11 # Myocardial Infarction - washout window of 90 days between events
MP_id <- 12 # Myocarditis Pericarditis - washout window of 90 days between events
VACA_id <- 13 # Ventricular Arrhythmia or Cardiac Arrest - washout window of 90 days between events
HF_id <- 14 # Heart Failure - washout window of 90 days between events
# Vaccination period of interest
library(lubridate)
priorOUT <- 180 # wash out window for outcome event prior to covid
# Time windows between covid-19 and disease to classify pacs:
# First month after infection
date11 <- "0"
date12 <- "30"
# 31 to 90 after infection
date21 <- "31"
date22 <- "90"
# 91 to 180 after infection
date31 <- "91"
date32 <- "180"
# 181 and up to a year after infection
date41 <- "181"
date42 <- "365"
# COHORTS DB
covid_db <- cohorts_db %>%
filter(cohort_definition_id == covid_id) %>%
select(person_id = subject_id, covid_date = cohort_start_date) %>%
compute()
# Vaccine AZ and PF
vaccine_db <- cohorts_db %>%
inner_join(tibble(cohort_definition_id = c(AZvaccine_id,PFvaccine_id)), by = "cohort_definition_id", copy = TRUE) %>%
select(person_id = subject_id, vaccine_date = cohort_start_date) %>%
compute()
# Cohort table to fill
PASC_cohort_table <- covid_db %>%
transmute( cohort_definition_id = person_id, subject_id = person_id, cohort_start_date = covid_date, cohort_end_date = covid_date) %>%
filter(subject_id == 1)
for (j in VTE_id:HF_id)
{
# j refers to the cohort ID of the disease cohort;
# jj will be used to set a cohort_definition_id for the outcome cohorts
jj <- j-4
# Disease db
outcome_db <- cohorts_db %>%
filter(cohort_definition_id == j) %>%
select(person_id = subject_id, outcome_date = cohort_start_date) %>%
compute()
# For each disease we get 16 outcome cohorts = 4 time windows after infection * 4 censoring types
# ---------------------------- NO CENSORING ----------------------------- #
# PACS db: covid + disease
outcome1_db <- outcome_db %>%
inner_join(covid_db, by = "person_id") %>%
compute()
# COVIDS to exclude: have a complication event 180 days before
covidToExclude <- outcome1_db %>%
group_by(person_id,covid_date) %>%
filter(covid_date > outcome_date & covid_date < outcome_date + days(priorOUT)) %>%
ungroup() %>%
compute()
# Exclude covids above + ensure that disease happens within 1 year after covid
outcomePacs11_db <- outcome1_db %>%
anti_join(covidToExclude, by = c("person_id", "outcome_date", "covid_date")) %>%
group_by(person_id) %>%
filter(covid_date < outcome_date) %>%
filter(covid_date >= outcome_date - days(date42)) %>%
ungroup() %>%
compute()
# Add time column to classify in time intervals + delete outcome_date (disease date) column to distinct
outcomePacs1_db <- outcomePacs11_db %>%
mutate(distancePACS = outcome_date-covid_date) %>%
mutate(cohort_definition_id = if_else(distancePACS >= date11 & distancePACS <= date12, 4*jj-3 + HF_id,
if_else(distancePACS >= date21 & distancePACS <= date22, 4*jj-2 + HF_id,
if_else(distancePACS >= date31 & distancePACS <= date32, 4*jj-1 + HF_id,
if_else(distancePACS >= date41 & distancePACS <= date42, 4*jj + HF_id,NA))))) %>%
select(-outcome_date,-distancePACS) %>%
distinct() %>%
filter(!is.na(cohort_definition_id)) %>%
compute()
# Fill cohort table: cohort_id, person_id, cohort_start_date (covid), cohort_end_date (min date disease)
PASC_cohort_table <- PASC_cohort_table %>%
full_join(outcomePacs1_db %>%
left_join(outcomePacs11_db %>%
select(person_id, covid_date, outcome_date),by = c("person_id","covid_date")) %>%
group_by(cohort_definition_id,person_id,covid_date) %>%
filter(outcome_date == min(outcome_date)) %>%
ungroup() %>%
rename(subject_id = person_id, cohort_start_date = covid_date, cohort_end_date = outcome_date)) %>%
compute()
# --------------------------- COVID CENSORING --------------------------- #
# Associate disease outcome to the nearest prior covid
outcomePacs21_db <- outcomePacs11_db %>%
group_by(person_id, outcome_date) %>%
filter(covid_date == max(covid_date)) %>%
ungroup() %>%
compute()
# Classify by time window after covid
outcomePacs2_db <- outcomePacs21_db %>%
mutate(distancePACS = outcome_date-covid_date) %>%
mutate(cohort_definition_id = if_else(distancePACS >= date11 & distancePACS <= date12, 4*jj-3 + 40 + HF_id,
if_else(distancePACS >= date21 & distancePACS <= date22, 4*jj-2 + 40 + HF_id,
if_else(distancePACS >= date31 & distancePACS <= date32, 4*jj-1 + 40 + HF_id,
if_else(distancePACS >= date41 & distancePACS <= date42, 4*jj + 40 + HF_id,NA))))) %>%
select(-outcome_date,-distancePACS) %>%
distinct() %>%
filter(!is.na(cohort_definition_id)) %>%
compute()
# Fill cohort table: cohort_id, person_id, cohort_start_date (covid), cohort_end_date (min date disease)
PASC_cohort_table <- PASC_cohort_table %>%
full_join(outcomePacs2_db %>%
left_join(outcomePacs21_db %>%
select(person_id, covid_date, outcome_date),by = c("person_id","covid_date")) %>%
group_by(cohort_definition_id,person_id,covid_date) %>%
filter(outcome_date == min(outcome_date)) %>%
ungroup() %>%
rename(subject_id = person_id, cohort_start_date = covid_date, cohort_end_date = outcome_date)) %>%
compute()
# -------------------------- VACCINE CENSORING -------------------------- #
# excludePACSvax: covid vaccine between covid and disease
excludePACSvax <- outcomePacs11_db %>%
left_join(vaccine_db) %>%
filter(covid_date < vaccine_date) %>%
filter(vaccine_date < outcome_date) %>%
compute()
outcomePacs31_db <- outcomePacs11_db %>%
anti_join(excludePACSvax, by = c("person_id", "outcome_date", "covid_date")) %>% compute()
# Classify by time window after covid
outcomePacs3_db <- outcomePacs31_db %>%
mutate(distancePACS = outcome_date-covid_date) %>%
mutate(cohort_definition_id = if_else(distancePACS >= date11 & distancePACS <= date12, 4*jj-3 + 80 + HF_id,
if_else(distancePACS >= date21 & distancePACS <= date22, 4*jj-2 + 80 + HF_id,
if_else(distancePACS >= date31 & distancePACS <= date32, 4*jj-1 + 80 + HF_id,
if_else(distancePACS >= date41 & distancePACS <= date42, 4*jj + 80 + HF_id,NA))))) %>%
select(-outcome_date,-distancePACS) %>%
distinct() %>%
filter(!is.na(cohort_definition_id)) %>%
compute()
# Fill cohort table: cohort_id, person_id, cohort_start_date (covid), cohort_end_date (min date disease)
PASC_cohort_table <- PASC_cohort_table %>%
full_join(outcomePacs3_db %>%
left_join(outcomePacs31_db %>%
select(person_id, covid_date, outcome_date),by = c("person_id","covid_date")) %>%
group_by(cohort_definition_id,person_id,covid_date) %>%
filter(outcome_date == min(outcome_date)) %>%
ungroup() %>%
rename(subject_id = person_id, cohort_start_date = covid_date, cohort_end_date = outcome_date)) %>%
compute()
# ---------------------- COVID + VACCINE CENSORING ---------------------- #
# The last two cohorts together
outcomePacs41_db <- outcomePacs21_db %>% inner_join(outcomePacs31_db) %>% compute()
# Classify by time window after covid
outcomePacs4_db <- outcomePacs41_db %>%
mutate(distancePACS = outcome_date-covid_date) %>%
mutate(cohort_definition_id = if_else(distancePACS >= date11 & distancePACS <= date12, 4*jj-3 + 120 + HF_id,
if_else(distancePACS >= date21 & distancePACS <= date22, 4*jj-2 + 120 + HF_id,
if_else(distancePACS >= date31 & distancePACS <= date32, 4*jj-1 + 120 + HF_id,
if_else(distancePACS >= date41 & distancePACS <= date42, 4*jj + 120 + HF_id,NA))))) %>%
select(-outcome_date,-distancePACS) %>%
distinct() %>%
filter(!is.na(cohort_definition_id)) %>%
compute()
# Fill cohort table: cohort_id, person_id, cohort_start_date (covid), cohort_end_date (min date disease)
PASC_cohort_table <- PASC_cohort_table %>%
full_join(outcomePacs4_db %>%
left_join(outcomePacs41_db %>%
select(person_id, covid_date, outcome_date),by = c("person_id","covid_date")) %>%
group_by(cohort_definition_id,person_id,covid_date) %>%
filter(outcome_date == min(outcome_date)) %>%
ungroup() %>%
rename(subject_id = person_id, cohort_start_date = covid_date, cohort_end_date = outcome_date)) %>%
compute()
}
# ------------------------ Add cohorts to the database ----------------------- #
# sql_query <- glue::glue("INSERT INTO {results_database_schema}.{cohort_tab_name}\n",
# " SELECT * FROM (\n",
# dbplyr::sql_render(cohort_table),
# "\n) AS from_table")
#
# DBI::dbExecute(con, as.character(sql_query))