generated from opensafely/covid-vaccine-research-template
/
data_process_treated.R
343 lines (272 loc) · 10.1 KB
/
data_process_treated.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
######################################
# This script:
# imports data extracted by the cohort extractor (or dummy data)
# fills in unknown ethnicity from GP records with ethnicity from SUS (secondary care)
# tidies missing values
# standardises some variables (eg convert to factor) and derives some new ones
# organises vaccination date data to "vax X type", "vax X date" (rather than "pfizer X date", "az X date", ...)
######################################
# import command-line arguments ----
args <- commandArgs(trailingOnly=TRUE)
if(length(args)==0){
# use for interactive testing
removeobjects <- FALSE
agegroup <- "over12"
} else {
#FIXME replace with actual eventual action variables
removeobjects <- TRUE
agegroup <- args[[1]]
}
# define vaccination of interest
if(agegroup=="under12") treatment <- "pfizerC"
if(agegroup=="over12") treatment <- "pfizerA"
# Import libraries ----
library('tidyverse')
library('lubridate')
library('arrow')
library('here')
library('glue')
source(here("lib", "functions", "utility.R"))
# import globally defined study dates and convert to "Date"
study_dates <-
jsonlite::read_json(path=here("lib", "design", "study-dates.json")) %>%
map(as.Date)
# output processed data to rds ----
fs::dir_create(here("output", "data"))
# process ----
# use externally created dummy data if not running in the server
# check variables are as they should be
if(Sys.getenv("OPENSAFELY_BACKEND") %in% c("", "expectations")){
# ideally in future this will check column existence and types from metadata,
# rather than from a cohort-extractor-generated dummy data
data_studydef_dummy <- read_feather(here("output", "input_treated.feather")) %>%
# because date types are not returned consistently by cohort extractor
mutate(across(ends_with("_date"), ~ as.Date(.))) %>%
# because of a bug in cohort extractor -- remove once pulled new version
mutate(patient_id = as.integer(patient_id))
data_custom_dummy <- read_feather(here("lib", "dummydata", "dummy_treated.feather")) %>%
mutate(
msoa = sample(factor(c("1", "2")), size=n(), replace=TRUE) # override msoa so matching success more likely
)
not_in_studydef <- names(data_custom_dummy)[!( names(data_custom_dummy) %in% names(data_studydef_dummy) )]
not_in_custom <- names(data_studydef_dummy)[!( names(data_studydef_dummy) %in% names(data_custom_dummy) )]
if(length(not_in_custom)!=0) stop(
paste(
"These variables are in studydef but not in custom: ",
paste(not_in_custom, collapse=", ")
)
)
if(length(not_in_studydef)!=0) stop(
paste(
"These variables are in custom but not in studydef: ",
paste(not_in_studydef, collapse=", ")
)
)
# reorder columns
data_studydef_dummy <- data_studydef_dummy[,names(data_custom_dummy)]
unmatched_types <- cbind(
map_chr(data_studydef_dummy, ~paste(class(.), collapse=", ")),
map_chr(data_custom_dummy, ~paste(class(.), collapse=", "))
)[ (map_chr(data_studydef_dummy, ~paste(class(.), collapse=", ")) != map_chr(data_custom_dummy, ~paste(class(.), collapse=", ")) ), ] %>%
as.data.frame() %>% rownames_to_column()
if(nrow(unmatched_types)>0) stop(
#unmatched_types
"inconsistent typing in studydef : dummy dataset\n",
apply(unmatched_types, 1, function(row) paste(paste(row, collapse=" : "), "\n"))
)
data_extract <- data_custom_dummy
} else {
data_extract <- read_feather(here("output", "input_treated.feather")) %>%
#because date types are not returned consistently by cohort extractor
mutate(across(ends_with("_date"), as.Date))
}
data_processed <- data_extract %>%
mutate(
sex = fct_case_when(
sex == "F" ~ "Female",
sex == "M" ~ "Male",
#sex == "I" ~ "Inter-sex",
#sex == "U" ~ "Unknown",
TRUE ~ NA_character_
),
# ethnicity_combined = if_else(is.na(ethnicity), ethnicity_6_sus, ethnicity),
#
# ethnicity_combined = fct_case_when(
# ethnicity_combined == "1" ~ "White",
# ethnicity_combined == "4" ~ "Black",
# ethnicity_combined == "3" ~ "South Asian",
# ethnicity_combined == "2" ~ "Mixed",
# ethnicity_combined == "5" ~ "Other",
# #TRUE ~ "Unknown",
# TRUE ~ NA_character_
#
# ),
region = fct_collapse(
region,
`East of England` = "East",
`London` = "London",
`Midlands` = c("West Midlands", "East Midlands"),
`North East and Yorkshire` = c("Yorkshire and The Humber", "North East"),
`North West` = "North West",
`South East` = "South East",
`South West` = "South West"
),
# prior_tests_cat = cut(prior_covid_test_frequency, breaks=c(0, 1, 2, 3, Inf), labels=c("0", "1", "2", "3+"), right=FALSE),
prior_covid_infection = (!is.na(postest_0_date)) | (!is.na(covidadmitted_0_date)) | (!is.na(primary_care_covid_case_0_date)),
# latest covid event before study start
anycovid_0_date = pmax(postest_0_date, covidemergency_0_date, covidadmitted_0_date, na.rm=TRUE),
# # earliest covid event after study start
# anycovid_1_date = pmin(postest_1_date, covidemergency_1_date, covidadmitted_1_date, covidcc_1_date, coviddeath_date, na.rm=TRUE),
#
# noncoviddeath_date = if_else(!is.na(death_date) & is.na(coviddeath_date), death_date, as.Date(NA_character_)),
#
# cause_of_death = fct_case_when(
# !is.na(coviddeath_date) ~ "covid-related",
# !is.na(death_date) ~ "not covid-related",
# TRUE ~ NA_character_
# ),
)
# reshape vaccination data ----
data_vax <- local({
data_vax_any <- data_processed %>%
select(patient_id, matches("covid\\_vax\\_any\\_\\d+\\_date")) %>%
pivot_longer(
cols = -patient_id,
names_to = c(NA, "vax_any_index"),
names_pattern = "^(.*)_(\\d+)_date",
values_to = "date",
values_drop_na = TRUE
) %>%
arrange(patient_id, date)
data_vax_pfizerA <- data_processed %>%
select(patient_id, matches("covid\\_vax\\_pfizerA\\_\\d+\\_date")) %>%
pivot_longer(
cols = -patient_id,
names_to = c(NA, "vax_pfizerA_index"),
names_pattern = "^(.*)_(\\d+)_date",
values_to = "date",
values_drop_na = TRUE
) %>%
arrange(patient_id, date)
data_vax_pfizerC <- data_processed %>%
select(patient_id, matches("covid\\_vax\\_pfizerC\\_\\d+\\_date")) %>%
pivot_longer(
cols = -patient_id,
names_to = c(NA, "vax_pfizerC_index"),
names_pattern = "^(.*)_(\\d+)_date",
values_to = "date",
values_drop_na = TRUE
) %>%
arrange(patient_id, date)
data_vax <-
data_vax_any %>%
full_join(data_vax_pfizerA, by=c("patient_id", "date")) %>%
full_join(data_vax_pfizerC, by=c("patient_id", "date")) %>%
mutate(
type = fct_case_when(
is.na(vax_pfizerC_index) & (!is.na(vax_pfizerA_index)) ~ "pfizerA",
(!is.na(vax_pfizerC_index)) & is.na(vax_pfizerA_index) ~ "pfizerC",
!is.na(vax_any_index) ~ "other",
TRUE ~ NA_character_
)
) %>%
arrange(patient_id, date) %>%
group_by(patient_id) %>%
mutate(
vax_index=row_number()
) %>%
ungroup()
data_vax
})
data_vax_wide = data_vax %>%
pivot_wider(
id_cols= patient_id,
names_from = c("vax_index"),
values_from = c("date", "type"),
names_glue = "covid_vax_{vax_index}_{.value}"
)
data_processed <- data_processed %>%
left_join(data_vax_wide, by ="patient_id") %>%
mutate(
vax1_type = covid_vax_1_type,
vax2_type = covid_vax_2_type,
vax1_type_descr = fct_case_when(
vax1_type == "pfizerA" ~ "BNT162b2 30micrograms/0.3ml",
vax1_type == "pfizerC" ~ "BNT162b2 10mcg/0.2ml",
vax1_type == "any" ~ "Other",
TRUE ~ NA_character_
),
vax2_type_descr = fct_case_when(
vax2_type == "pfizerA" ~ "BNT162b2 30micrograms/0.3ml",
vax2_type == "pfizerC" ~ "BNT162b2 10mcg/0.2ml",
vax2_type == "any" ~ "Other",
TRUE ~ NA_character_
),
vax1_date = covid_vax_1_date,
vax2_date = covid_vax_2_date,
) %>%
select(
-starts_with("covid_vax_"),
)
#write_rds(data_processed, here("output", "data", "data_processed_treated.rds"), compress="gz")
## select eligible patients and create flowchart ----
# Define selection criteria ----
data_criteria <- data_processed %>%
transmute(
patient_id,
has_age = !is.na(age),
has_sex = !is.na(sex) & !(sex %in% c("I", "U")),
has_imd = imd_Q5 != "Unknown",
#has_ethnicity = !is.na(ethnicity_combined),
has_region = !is.na(region),
vax1_betweenentrydates = case_when(
(vax1_type==treatment) &
(vax1_date >= study_dates[[glue("first{agegroup}_date")]]) &
(vax1_date <= study_dates[[glue("{agegroup}end_date")]]) ~ TRUE,
TRUE ~ FALSE
),
has_vaxgap12 = vax2_date >= (vax1_date+17) | is.na(vax2_date), # at least 17 days between first two vaccinations
no_recentcovid90 = is.na(anycovid_0_date) | ((vax1_date - anycovid_0_date)>90),
include = (
vax1_betweenentrydates & has_vaxgap12 &
has_age & has_sex & has_imd & # has_ethnicity &
has_region &
no_recentcovid90
),
)
data_treated_eligible <-
data_criteria %>%
filter(include) %>%
select(patient_id) %>%
left_join(data_processed, by="patient_id") %>%
droplevels()
write_rds(data_treated_eligible, here("output", "data", "data_treated_eligible.rds"), compress="gz")
## Flowchart ----
data_flowchart <- data_criteria %>%
transmute(
c0 = vax1_betweenentrydates & has_vaxgap12,
c1 = c0 & (has_age & has_sex & has_imd & has_region),
c2 = c1 + no_recentcovid90
) %>%
summarise(
across(.fns=sum)
) %>%
pivot_longer(
cols=everything(),
names_to="criteria",
values_to="n"
) %>%
mutate(
n_exclude = lag(n) - n,
pct_exclude = n_exclude/lag(n),
pct_all = n / first(n),
pct_step = n / lag(n),
crit = str_extract(criteria, "^c\\d+"),
criteria = fct_case_when(
crit == "c0" ~ "Received age-correct vaccine within study entry dates",
crit == "c1" ~ " with no missing demographic information",
crit == "c2" ~ " with no COVID-19 90 days prior",
TRUE ~ NA_character_
)
)
write_csv(data_flowchart, here("output", "data", "flowchart_treated_eligible.csv"))