generated from opensafely/research-template
/
data_process.R
346 lines (281 loc) · 12 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
######################################
# 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 libraries ----
library('tidyverse')
library('lubridate')
library('arrow')
library('here')
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.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", "dummyinput.feather"))
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.feather")) %>%
#because date types are not returned consistently by cohort extractor
mutate(across(ends_with("_date"), as.Date))
}
data_processed <- data_extract %>%
mutate(
# studystart_date = as.Date(study_dates$studystart_date), # i.e., this is interpreted later as [midnight at the _end of_ the start date] = [midnight at the _start of_ start date + 1], So that for example deaths on start_date+1 occur at t=1, not t=0.
# firstpfizer_date = as.Date(study_dates$firstpfizer_date),
# firstaz_date = as.Date(study_dates$firstaz_date),
# firstmoderna_date = as.Date(study_dates$firstmoderna_date),
# studyend_date = as.Date(study_dates$studyend_date),
ageband = cut(
age,
breaks=c(-Inf, 18, 30, 40, 50, 60, 65, Inf),
labels=c("under 18", "18-30", "30s", "40s", "50s", "60-64", "65+"),
right=FALSE
),
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"
),
imd = as.integer(as.character(imd)), # imd is a factor, so convert to character then integer to get underlying values
imd = if_else(imd<=0, NA_integer_, imd),
imd_Q5 = fct_case_when(
(imd >=1) & (imd < 32844*1/5) ~ "1 most deprived",
(imd >= 32844*1/5) & (imd < 32844*2/5) ~ "2",
(imd >= 32844*2/5) & (imd < 32844*3/5) ~ "3",
(imd >= 32844*3/5) & (imd < 32844*4/5) ~ "4",
(imd >= 32844*4/5) ~ "5 least deprived",
TRUE ~ NA_character_
),
rural_urban_group = fct_case_when(
rural_urban %in% c(1,2) ~ "Urban conurbation",
rural_urban %in% c(3,4) ~ "Urban city or town",
rural_urban %in% c(5,6,7,8) ~ "Rural town or village",
TRUE ~ NA_character_
),
care_home_combined = care_home_tpp | care_home_code, # any carehome flag
# clinically at-risk group
cv = immunosuppressed | chronic_kidney_disease | chronic_resp_disease | diabetes | chronic_liver_disease |
chronic_neuro_disease | chronic_heart_disease | asplenia | learndis | sev_mental,
multimorb =
(sev_obesity) +
(chronic_heart_disease) +
(chronic_kidney_disease)+
(diabetes) +
(chronic_liver_disease)+
(chronic_resp_disease | asthma)+
(immunosuppressed | asplenia)+
(chronic_neuro_disease)#+
#(learndis)+
#(sev_mental),
,
multimorb = cut(multimorb, breaks = c(0, 1, 2, Inf), labels=c("0", "1", "2+"), right=FALSE),
# https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/1007737/Greenbook_chapter_14a_30July2021.pdf#page=12
jcvi_group = fct_case_when(
care_home_combined | age_march2020>=80 | hscworker ~ "1 & 2",
age_march2020>=75 ~ "3",
age_march2020>=70 | (cev & (age_march2020>=16)) ~ "4",
age_march2020>=65 ~ "5",
between(age_march2020, 16, 64.999) & cv ~ "6",
age_march2020>=60 ~ "7",
age_march2020>=55 ~ "8",
age_march2020>=50 ~ "9",
TRUE ~ "10"
),
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(positive_test_0_date) | !is.na(covidadmitted_0_date) | !is.na(primary_care_covid_case_0_date),
cause_of_death = fct_case_when(
!is.na(coviddeath_date) ~ "covid-related",
!is.na(death_date) ~ "not covid-related",
TRUE ~ NA_character_
),
covidemergency_date = pmin(covidemergency_date, covidadmitted_date, na.rm=TRUE),
covidadmitted_ccdays = as.integer(as.character(covidadmitted_ccdays)), # covidadmitted_ccdays is a factor, so convert to character then integer
noncoviddeath_date = if_else(!is.na(death_date) & is.na(coviddeath_date), death_date, as.Date(NA_character_)),
covidcc_date = if_else(!is.na(covidadmitted_date) & covidadmitted_ccdays>0, covidadmitted_date, as.Date(NA_character_)),
)
# reshape vaccination data ----
data_vax <- local({
# data_vax_all <- data_processed %>%
# select(patient_id, matches("covid\\_vax\\_\\d+\\_date")) %>%
# pivot_longer(
# cols = -patient_id,
# names_to = c(NA, "vax_index"),
# names_pattern = "^(.*)_(\\d+)_date",
# values_to = "date",
# values_drop_na = TRUE
# ) %>%
# arrange(patient_id, date)
data_vax_pfizer <- data_processed %>%
select(patient_id, matches("covid\\_vax\\_pfizer\\_\\d+\\_date")) %>%
pivot_longer(
cols = -patient_id,
names_to = c(NA, "vax_pfizer_index"),
names_pattern = "^(.*)_(\\d+)_date",
values_to = "date",
values_drop_na = TRUE
) %>%
arrange(patient_id, date)
data_vax_az <- data_processed %>%
select(patient_id, matches("covid\\_vax\\_az\\_\\d+\\_date")) %>%
pivot_longer(
cols = -patient_id,
names_to = c(NA, "vax_az_index"),
names_pattern = "^(.*)_(\\d+)_date",
values_to = "date",
values_drop_na = TRUE
) %>%
arrange(patient_id, date)
data_vax_moderna <- data_processed %>%
select(patient_id, matches("covid\\_vax\\_moderna\\_\\d+\\_date")) %>%
pivot_longer(
cols = -patient_id,
names_to = c(NA, "vax_moderna_index"),
names_pattern = "^(.*)_(\\d+)_date",
values_to = "date",
values_drop_na = TRUE
) %>%
arrange(patient_id, date)
data_vax <-
data_vax_pfizer %>%
full_join(data_vax_az, by=c("patient_id", "date")) %>%
full_join(data_vax_moderna, by=c("patient_id", "date")) %>%
mutate(
type = fct_case_when(
(!is.na(vax_az_index)) & is.na(vax_pfizer_index) & is.na(vax_moderna_index) ~ "az",
is.na(vax_az_index) & (!is.na(vax_pfizer_index)) & is.na(vax_moderna_index) ~ "pfizer",
is.na(vax_az_index) & is.na(vax_pfizer_index) & (!is.na(vax_moderna_index)) ~ "moderna",
(!is.na(vax_az_index)) + (!is.na(vax_pfizer_index)) + (!is.na(vax_moderna_index)) > 1 ~ "duplicate",
TRUE ~ NA_character_
)
) %>%
arrange(patient_id, date) %>%
group_by(patient_id) %>%
mutate(
vax_index=row_number()
) %>%
ungroup()
data_vax
})
write_rds(data_vax, here("output", "data", "data_vaxlong.rds"), compress="gz")
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,
vax3_type = covid_vax_3_type,
vax4_type = covid_vax_4_type,
vax12_type = paste0(vax1_type, "-", vax2_type),
vax1_type_descr = fct_case_when(
vax1_type == "pfizer" ~ "BNT162b2",
vax1_type == "az" ~ "ChAdOx1",
vax1_type == "moderna" ~ "Moderna",
TRUE ~ NA_character_
),
vax2_type_descr = fct_case_when(
vax2_type == "pfizer" ~ "BNT162b2",
vax2_type == "az" ~ "ChAdOx1",
vax2_type == "moderna" ~ "Moderna",
TRUE ~ NA_character_
),
vax3_type_descr = fct_case_when(
vax3_type == "pfizer" ~ "BNT162b2",
vax3_type == "az" ~ "ChAdOx1",
vax3_type == "moderna" ~ "Moderna",
TRUE ~ NA_character_
),
vax4_type_descr = fct_case_when(
vax4_type == "pfizer" ~ "BNT162b2",
vax4_type == "az" ~ "ChAdOx1",
vax4_type == "moderna" ~ "Moderna",
TRUE ~ NA_character_
),
vax1_date = covid_vax_1_date,
vax2_date = covid_vax_2_date,
vax3_date = covid_vax_3_date,
vax4_date = covid_vax_4_date,
vax1_day = as.integer(floor((vax1_date - study_dates$studystart_date))+1), # day 0 is the day before "start_date"
vax2_day = as.integer(floor((vax2_date - study_dates$studystart_date))+1), # day 0 is the day before "start_date"
vax3_day = as.integer(floor((vax3_date - study_dates$studystart_date))+1), # day 0 is the day before "start_date"
vax4_day = as.integer(floor((vax4_date - study_dates$studystart_date))+1), # day 0 is the day before "start_date"
vax1_week = as.integer(floor((vax1_date - study_dates$studystart_date)/7)+1), # week 1 is days 1-7.
vax2_week = as.integer(floor((vax2_date - study_dates$studystart_date)/7)+1), # week 1 is days 1-7.
vax3_week = as.integer(floor((vax3_date - study_dates$studystart_date)/7)+1), # week 1 is days 1-7.
vax4_week = as.integer(floor((vax4_date - study_dates$studystart_date)/7)+1), # week 1 is days 1-7.
) %>%
select(
-starts_with("covid_vax_"),
)
write_rds(data_processed, here("output", "data", "data_processed.rds"), compress="gz")