generated from opensafely/research-template
/
data_process.R
352 lines (307 loc) · 13.2 KB
/
data_process.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
######################################
# Processes data from cohort extract
######################################
## Packages
library('tidyverse')
library('lubridate')
library('here')
library('gt')
library('gtsummary')
library('arrow')
library('reshape2')
## Import custom user functions
source(here::here("lib", "functions", "fct_case_when.R"))
source(here::here("lib", "functions", "define_covid_hosp_admissions.R"))
source(here::here("lib", "functions", "define_allcause_hosp_admissions.R"))
source(here::here("lib", "functions", "define_status_and_fu_all.R"))
source(here::here("lib", "functions", "define_status_and_fu_primary.R"))
source(here::here("lib", "functions", "define_status_and_fu_secondary.R"))
## Print session info to metadata log file
sessionInfo()
## Print variable names
cat("#### read in data extract ####\n")
data_extract <- read_csv(
here::here("output", "input.csv.gz"),
col_types = cols_only(
# Identifier
patient_id = col_integer(),
# POPULATION ----
age = col_integer(),
sex = col_character(),
ethnicity = col_character(),
imdQ5 = col_character(),
region_nhs = col_character(),
stp = col_factor(),
rural_urban = col_character(),
# MAIN ELIGIBILITY - FIRST POSITIVE SARS-CoV-2 TEST IN PERIOD ----
covid_test_positive_date = col_date(format = "%Y-%m-%d"),
# TREATMENT - NEUTRALISING MONOCLONAL ANTIBODIES OR ANTIVIRALS ----
paxlovid_covid_therapeutics = col_date(format = "%Y-%m-%d"),
sotrovimab_covid_therapeutics = col_date(format = "%Y-%m-%d"),
remdesivir_covid_therapeutics = col_date(format = "%Y-%m-%d"),
molnupiravir_covid_therapeutics = col_date(format = "%Y-%m-%d"),
casirivimab_covid_therapeutics = col_date(format = "%Y-%m-%d"),
date_treated = col_date(format = "%Y-%m-%d"),
# PREVIOUS TREATMENT - NEUTRALISING MONOCLONAL ANTIBODIES OR ANTIVIRALS ----
# OVERALL ELIGIBILITY CRITERIA VARIABLES ----
symptomatic_covid_test = col_character(),
covid_symptoms_snomed = col_date(format = "%Y-%m-%d"),
pregnancy = col_logical(),
# CENSORING ----
death_date = col_date(format = "%Y-%m-%d"),
dereg_date = col_date(format = "%Y-%m-%d"),
# HIGH RISK GROUPS ----
high_risk_cohort_covid_therapeutics = col_factor(),
huntingtons_disease_nhsd = col_logical() ,
myasthenia_gravis_nhsd = col_logical() ,
motor_neurone_disease_nhsd = col_logical() ,
multiple_sclerosis_nhsd = col_logical() ,
solid_organ_transplant_nhsd = col_logical(),
hiv_aids_nhsd = col_logical(),
immunosupression_nhsd = col_logical(),
imid_nhsd = col_logical(),
liver_disease_nhsd = col_logical(),
ckd_stage_5_nhsd = col_logical(),
haematological_disease_nhsd = col_logical(),
cancer_opensafely_snomed = col_logical(),
downs_syndrome_nhsd = col_logical(),
# CLINICAL/DEMOGRAPHIC COVARIATES ----
diabetes = col_logical(),
bmi = col_character(),
smoking_status = col_character(),
copd = col_logical(),
dialysis = col_logical(),
cancer = col_logical(),
lung_cancer = col_logical(),
haem_cancer = col_logical(),
# VACCINATION ----
vaccination_status = col_factor(),
date_most_recent_cov_vac = col_date(format = "%Y-%m-%d"),
pfizer_most_recent_cov_vac = col_logical(),
az_most_recent_cov_vac = col_logical(),
moderna_most_recent_cov_vac = col_logical(),
# VARIANT ----
sgtf = col_factor(),
variant = col_factor(),
# OUTCOMES ----
# covid specific
covid_hosp_admission_date0 = col_date(format = "%Y-%m-%d"),
covid_hosp_admission_date1 = col_date(format = "%Y-%m-%d"),
covid_hosp_admission_date2 = col_date(format = "%Y-%m-%d"),
covid_hosp_admission_date3 = col_date(format = "%Y-%m-%d"),
covid_hosp_admission_date4 = col_date(format = "%Y-%m-%d"),
covid_hosp_admission_date5 = col_date(format = "%Y-%m-%d"),
covid_hosp_admission_date6 = col_date(format = "%Y-%m-%d"),
covid_hosp_admission_first_date7_27 = col_date(format = "%Y-%m-%d"),
covid_hosp_discharge_first_date0_7 = col_date(format = "%Y-%m-%d"),
covid_hosp_date_mabs_procedure = col_date(format = "%Y-%m-%d"),
# all cause
allcause_hosp_admission_date0 = col_date(format = "%Y-%m-%d"),
allcause_hosp_admission_date1 = col_date(format = "%Y-%m-%d"),
allcause_hosp_admission_date2 = col_date(format = "%Y-%m-%d"),
allcause_hosp_admission_date3 = col_date(format = "%Y-%m-%d"),
allcause_hosp_admission_date4 = col_date(format = "%Y-%m-%d"),
allcause_hosp_admission_date5 = col_date(format = "%Y-%m-%d"),
allcause_hosp_admission_date6 = col_date(format = "%Y-%m-%d"),
allcause_hosp_admission_first_date7_27 = col_date(format = "%Y-%m-%d"),
allcause_hosp_discharge_first_date0_7 = col_date(format = "%Y-%m-%d"),
allcause_hosp_date_mabs_procedure = col_date(format = "%Y-%m-%d"),
# death
died_ons_covid_any_date = col_date(format = "%Y-%m-%d")
),
)
# data cleaning
cat("#### data cleaning ####\n")
## Format columns (i.e, set factor levels)
data_processed <- data_extract %>%
mutate(
# Cinic/demo variables -----
ageband = cut(
age,
breaks = c(18, 40, 60, 80, Inf),
labels = c("18-39", "40-59", "60-79", "80+"),
right = FALSE
),
sex = fct_case_when(
sex == "F" ~ "Female",
sex == "M" ~ "Male",
TRUE ~ NA_character_
),
ethnicity = fct_case_when(
ethnicity == "0" ~ "Unknown",
ethnicity == "1" ~ "White",
ethnicity == "2" ~ "Mixed",
ethnicity == "3" ~ "Asian or Asian British",
ethnicity == "4" ~ "Black or Black British",
ethnicity == "5" ~ "Other ethnic groups",
#TRUE ~ "Unknown"
TRUE ~ NA_character_),
bmi_group = fct_case_when(
bmi == "Not obese" ~ "Not obese",
bmi == "Obese I (30-34.9)" ~ "Obese I (30-34.9)",
bmi == "Obese II (35-39.9)" ~ "Obese II (35-39.9)",
bmi == "Obese III (40+)" ~ "Obese III (40+)",
TRUE ~ NA_character_),
smoking_status = fct_case_when(
smoking_status == "S" ~ "Smoker",
smoking_status == "E" ~ "Ever",
smoking_status == "N" ~ "Never",
smoking_status == "M" ~ "Missing",
#TRUE ~ "Unknown"
TRUE ~ NA_character_),
imdQ5 = fct_case_when(
imdQ5 == "5 (least deprived)" ~ "5 (least deprived)",
imdQ5 == "4" ~ "4",
imdQ5 == "3" ~ "3",
imdQ5 == "2" ~ "2",
imdQ5 == "1 (most deprived)" ~ "1 (most deprived)",
TRUE ~ NA_character_
),
region_nhs = fct_case_when(
region_nhs == "London" ~ "London",
region_nhs == "East" ~ "East of England",
region_nhs == "East Midlands" ~ "East Midlands",
region_nhs == "North East" ~ "North East",
region_nhs == "North West" ~ "North West",
region_nhs == "South East" ~ "South East",
region_nhs == "South West" ~ "South West",
region_nhs == "West Midlands" ~ "West Midlands",
region_nhs == "Yorkshire and The Humber" ~ "Yorkshire and the Humber",
#TRUE ~ "Unknown",
TRUE ~ NA_character_),
# Rural/urban
rural_urban = fct_case_when(
rural_urban %in% c(1:2) ~ "Urban - conurbation",
rural_urban %in% c(3:4) ~ "Urban - city and town",
rural_urban %in% c(5:6) ~ "Rural - town and fringe",
rural_urban %in% c(7:8) ~ "Rural - village and dispersed",
#TRUE ~ "Unknown",
TRUE ~ NA_character_
),
# Time-between positive test and last vaccination
tb_postest_vacc = ifelse(!is.na(date_most_recent_cov_vac),
difftime(date_most_recent_cov_vac, covid_test_positive_date) %>% as.numeric(),
NA_integer_),
tb_postest_vacc_cat = fct_case_when(
is.na(tb_postest_vacc) ~ "Unknown",
tb_postest_vacc < 7 ~ "< 7 days",
tb_postest_vacc >=7 & tb_postest_vacc <28 ~ "7-27 days",
tb_postest_vacc >= 28 & tb_postest_vacc <84 ~ "28-83 days",
tb_postest_vacc >= 84 ~ ">= 84 days"
),
# NEUTRALISING MONOCLONAL ANTIBODIES OR ANTIVIRALS ----
# Treatment assignment window 'treated within 5 days -> <= 4 days'
treat_window = covid_test_positive_date + days(4),
# Time-between positive test and day of treatment
tb_postest_treat = ifelse(!is.na(date_treated),
difftime(date_treated, covid_test_positive_date) %>% as.numeric(),
NA_integer_),
# Flag records where treatment date falls in treatment assignment window
treat_check = ifelse(date_treated >= covid_test_positive_date &
date_treated <= treat_window,
1,
0),
# Treatment strategy categories
treatment_strategy_cat = case_when(
date_treated == sotrovimab_covid_therapeutics &
treat_check == 1 ~ "Sotrovimab",
date_treated == molnupiravir_covid_therapeutics &
treat_check == 1 ~ "Molnupiravir",
TRUE ~ "Untreated"
) %>% as.factor(),
# Treatment strategy overall
treatment = case_when(
(date_treated == sotrovimab_covid_therapeutics &
treat_check == 1) |
(date_treated == molnupiravir_covid_therapeutics &
treat_check == 1) ~ "Treated",
TRUE ~ "Untreated"
) %>% as.factor(),
# Treatment date
treatment_date = ifelse(treatment == "Treated", date_treated, NA_Date_),
# Identify patients treated with sot and mol on same day
treated_sot_mol_same_day =
case_when(is.na(sotrovimab_covid_therapeutics) ~ 0,
is.na(molnupiravir_covid_therapeutics) ~ 0,
sotrovimab_covid_therapeutics ==
molnupiravir_covid_therapeutics ~ 1,
TRUE ~ 0),
# Time-between symptom onset and treatment in those treatead
tb_symponset_treat =
case_when(is.na(date_treated) ~ NA_real_,
symptomatic_covid_test == "Y" ~ min(covid_test_positive_date,
covid_symptoms_snomed) %>%
difftime(., date_treated) %>%
as.numeric()
),
) %>%
# because makes logic better readable
rename(covid_death_date = died_ons_covid_any_date) %>%
# add columns first admission in day 0-6, second admission etc. to be used
# to define hospital admissions (hosp admissions for sotro treated are
# different from the rest as sometimes their admission is just an admission
# to get the sotro infusion)
summarise_covid_admissions() %>%
# adds column covid_hosp_admission_date
add_covid_hosp_admission_outcome() %>%
# idem as explained above for all cause hospitalisation
summarise_allcause_admissions() %>%
# adds column allcause_hosp_admission_date
add_allcause_hosp_admission_outcome() %>%
mutate(
# Outcome prep --> outcomes are added in add_*_outcome() functions below
study_window = covid_test_positive_date + days(27),
# make distinction between noncovid death and covid death, since noncovid
# death is a censoring event and covid death is an outcome
noncovid_death_date = case_when(
!is.na(death_date) & is.na(covid_death_date) ~ death_date,
TRUE ~ NA_Date_
),
# make distinction between noncovid hosp admission and covid hosp
# admission, non covid hosp admission is not used as a censoring event in
# our study, but we'd like to report how many pt were admitted to the
# hospital for a noncovid-y reason before one of the other events
noncovid_hosp_admission_date = case_when(
!is.na(allcause_hosp_admission_date) &
is.na(covid_hosp_admission_date) ~ allcause_hosp_admission_date,
TRUE ~ NA_Date_
),
) %>%
# adds column status_all and fu_all
add_status_and_fu_all() %>%
# adds column status_primary and fu_primary
add_status_and_fu_primary() %>%
# adds column status_secondary and fu_secondary
add_status_and_fu_secondary()
## Apply additional eligibility and exclusion criteria
data_processed_eligible <- data_processed %>%
filter(
# Exclude patients treated with both sotrovimab and molnupiravir on the same
# day
treated_sot_mol_same_day == 0,
)
cat("#### data_processed ####\n")
print(dim(data_processed))
cat("#### data_processed_eligible ####\n")
print(dim(data_processed_eligible))
data_processed_eligible_day0 <- data_processed_eligible
# in the initial analysis, all patients with an outcome on day 0, 1, 2, 3, or 4,
# are excluded. [FYI, secondary outcomes are 'dereg', 'allcause_hosp' or
# 'allcause_death']
data_processed_eligible_day5 <-
data_processed_eligible %>%
filter(fu_secondary > 4) %>%
mutate(fu_primary = fu_primary - 5,
fu_secondary = fu_secondary - 5) # Because starting at day 5
cat("#### data_processed_eligible day 0 ####\n")
print(dim(data_processed_eligible_day0))
cat("#### data_processed_eligible day 5 ####\n")
print(dim(data_processed_eligible_day5))
# save data
# data_processed_eligible_day0 and data_processed_eligible_day5 are saved
# and data_processed with all patients is not saved to save memory
fs::dir_create(here::here("output", "data"))
write_rds(data_processed_eligible_day0,
here::here("output", "data", "data_processed_day0.rds"))
write_rds(data_processed_eligible_day5,
here::here("output", "data", "data_processed_day5.rds"))