generated from opensafely/research-template
/
00_process_data.R
304 lines (253 loc) · 10.6 KB
/
00_process_data.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
######################################
# This script:
# - imports data extracted by the cohort extractor
# - combines ethnicity columns
# - calculates survival time
# - standardises some variables (eg convert to factor) and derives some new ones
# - saves processed one-row-per-patient dataset
######################################
# Preliminaries ----
## Import libraries
library('tidyverse')
library('lubridate')
## Custom functions
### tte
tte <- function(origin_date, event_date, censor_date, na.censor=FALSE){
# returns time-to-event date or time to censor date, which is earlier
if (na.censor)
time <- event_date-origin_date
else
time <- pmin(event_date-origin_date, censor_date-origin_date, na.rm=TRUE)
as.numeric(time)
}
### Factorise
fct_case_when <- function(...) {
# uses dplyr::case_when but converts the output to a factor,
# with factors ordered as they appear in the case_when's ... argument
args <- as.list(match.call())
levels <- sapply(args[-1], function(f) f[[3]]) # extract RHS of formula
levels <- levels[!is.na(levels)]
factor(dplyr::case_when(...), levels=levels)
}
## Output processed data to rds
dir.create(here::here("output", "data"), showWarnings = FALSE, recursive=TRUE)
# Process data ----
## Print variable names
read_csv(here::here("output", "input.csv"),
n_max = 0,
col_types = cols()) %>%
names() %>%
print()
## Read in data (don't rely on defaults)
data_extract0 <- read_csv(
here::here("output", "input.csv"),
col_types = cols_only(
# Identifier
patient_id = col_integer(),
# Outcome
covid_vax_1_date = col_date(format="%Y-%m-%d"),
# Censoring
death_date = col_date(format="%Y-%m-%d"),
dereg_date = col_date(format="%Y-%m-%d"),
# Demographic
age = col_integer(),
sex = col_character(),
ethnicity = col_character(),
ethnicity_other = col_date(format="%Y-%m-%d"),
ethnicity_not_given = col_date(format="%Y-%m-%d"),
ethnicity_not_stated = col_date(format="%Y-%m-%d"),
ethnicity_no_record = col_date(format="%Y-%m-%d"),
# Clinical measurements and comorbidities
bmi = col_double(),
bmi_stage_date = col_date(format="%Y-%m-%d"),
sev_obesity = col_date(format="%Y-%m-%d"),
chronic_heart_disease = col_logical(),
diabetes = col_logical(),
chronic_kidney_disease_diagnostic = col_logical(),
chronic_kidney_disease_all_stages = col_logical(),
chronic_kidney_disease_all_stages_3_5 = col_logical(),
sev_mental_ill = col_date(format="%Y-%m-%d"),
learning_disability = col_date(format="%Y-%m-%d"),
chronic_neuro_dis_inc_sig_learn_dis = col_date(format="%Y-%m-%d"),
asplenia = col_logical(),
chronic_liver_disease = col_logical(),
chronis_respiratory_disease = col_date(format="%Y-%m-%d"),
immunosuppression_diagnosis = col_date(format="%Y-%m-%d"),
immunosuppression_medication = col_date(format="%Y-%m-%d"),
# Geographical
practice_id_at_start = col_integer(),
practice_id_at_end = col_integer(),
practice_id_at_death = col_integer(),
practice_id_at_dereg = col_integer(),
imd = col_character(),
region = col_character(),
stp = col_character(),
rural_urban = col_character(),
# Other
flu_vaccine = col_logical(),
shielded = col_logical(),
shielded_since_feb_15 = col_logical(),
prior_covid_date = col_date(format="%Y-%m-%d")
),
na = character() # more stable to convert to missing later
)
## Parse NAs
data_extract <- data_extract0 %>%
mutate(across(
.cols = where(is.character),
.fns = ~na_if(.x, "")
)) %>%
mutate(across(
.cols = c(where(is.numeric), -ends_with("_id")), #convert numeric+integer but not id variables
.fns = ~na_if(.x, 0)
)) %>%
arrange(patient_id) %>%
select(all_of((names(data_extract0))))
## Format columns (i.e, set factor levels)
data_processed <- data_extract %>%
mutate(
# Start date
start_date = as.Date("2020-12-07", format = "%Y-%m-%d"),
# End date
end_date = as.Date("2021-03-17", format = "%Y-%m-%d"),
# COVID vaccination
covid_vax = as.integer(ifelse(is.na(covid_vax_1_date), 0, 1)),
# Censoring
censor_date = pmin(death_date,
dereg_date,
as.Date("2021-04-01", format = "%Y-%m-%d"),
na.rm=TRUE),
# Follow-up time
follow_up_time = tte(start_date,
covid_vax_1_date,
censor_date),
# Age
ageband = cut(
age,
breaks = c(-Inf, 75, 80, 85, 90, 95, Inf),
labels = c("70-74", "75-79", "80-84", "85-89", "90-94", "95+"),
right = FALSE
),
# Sex
sex = fct_case_when(
sex == "F" ~ "Female",
sex == "M" ~ "Male",
#sex == "I" ~ "Inter-sex",
#sex == "U" ~ "Unknown",
TRUE ~ NA_character_
),
# Ethnicity
ethnicity = ifelse(is.na(ethnicity) & !is.na(ethnicity_other), 17,
ifelse(is.na(ethnicity) & !is.na(ethnicity_not_given), 18,
ifelse(is.na(ethnicity) & !is.na(ethnicity_not_stated), 19,
ifelse(is.na(ethnicity) & !is.na(ethnicity_no_record), 20,
ethnicity)))),
ethnicity = ifelse(is.na(ethnicity), 20, ethnicity),
ethnicity = fct_case_when(
ethnicity == "1" ~ "White - British",
ethnicity == "2" ~ "White - Irish",
ethnicity == "3" ~ "White - Any other White background",
ethnicity == "4" ~ "Mixed - White and Black Caribbean",
ethnicity == "5" ~ "Mixed - White and Black African",
ethnicity == "6" ~ "Mixed - White and Asian",
ethnicity == "7" ~ "Mixed - Any other mixed background",
ethnicity == "8" ~ "Asian or Asian British - Indian",
ethnicity == "9" ~ "Asian or Asian British - Pakistani",
ethnicity == "10" ~ "Asian or Asian British - Bangladeshi",
ethnicity == "11" ~ "Asian or Asian British - Any other Asian background",
ethnicity == "12" ~ "Black or Black British - Caribbean",
ethnicity == "13" ~ "Black or Black British - African",
ethnicity == "14" ~ "Black or Black British - Any other Black background",
ethnicity == "15" ~ "Other ethnic groups - Chinese",
ethnicity == "16" ~ "Other ethnic groups - Any other ethnic group",
ethnicity == "17" ~ "Patients with any other ethnicity code",
ethnicity == "18" ~ "Ethnicity not given - patient refused",
ethnicity == "19" ~ "Ethnicity not stated",
ethnicity == "20" ~ "Ethnicity not recorded",
#TRUE ~ "Unknown",
TRUE ~ NA_character_
),
# Morbidly obese
morbid_obesity_bmi = ifelse(bmi >= 40, 1, 0),
morbid_obesity_date = ifelse(sev_obesity >= bmi_stage_date, 1, 0),
morbid_obesity = ifelse(morbid_obesity_date == 1 & morbid_obesity_bmi == 0, 1, morbid_obesity_bmi),
morbid_obesity = ifelse(is.na(morbid_obesity), 0, morbid_obesity),
# Mental illness
sev_mental_ill = ifelse(is.na(sev_mental_ill), FALSE, TRUE),
# Learning disability
learning_disability = ifelse(is.na(learning_disability), FALSE, TRUE),
# CND inc LD
chronic_neuro_dis_inc_sig_learn_dis = ifelse(is.na(chronic_neuro_dis_inc_sig_learn_dis), FALSE, TRUE),
# CRD
chronis_respiratory_disease = ifelse(is.na(chronis_respiratory_disease), FALSE, TRUE),
# Immunosuppression
immunosuppression_diagnosis = ifelse(is.na(immunosuppression_diagnosis), FALSE, TRUE),
immunosuppression_medication = ifelse(is.na(immunosuppression_medication), FALSE, TRUE),
# IMD
imd = na_if(imd, "0"),
imd = fct_case_when(
imd == 1 ~ "1 most deprived",
imd == 2 ~ "2",
imd == 3 ~ "3",
imd == 4 ~ "4",
imd == 5 ~ "5 least deprived",
#TRUE ~ "Unknown",
TRUE ~ NA_character_
),
# Practice id at death, dereg or end
practice_id_latest_active_registration = ifelse(!is.na(death_date) & death_date < end_date, practice_id_at_death,
ifelse(!is.na(dereg_date) & dereg_date < end_date,
practice_id_at_dereg, practice_id_at_end)),
# Region
region = fct_case_when(
region == "London" ~ "London",
region == "East" ~ "East of England",
region == "East Midlands" ~ "East Midlands",
region == "North East" ~ "North East",
region == "North West" ~ "North West",
region == "South East" ~ "South East",
region == "South West" ~ "South West",
region == "West Midlands" ~ "West Midlands",
region == "Yorkshire and The Humber" ~ "Yorkshire and the Humber",
#TRUE ~ "Unknown",
TRUE ~ NA_character_
),
# stp
stp = as.factor(stp),
# Rural/urban
rural_urban = fct_case_when(
rural_urban == 1 ~ "Urban - major conurbation",
rural_urban == 2 ~ "Urban - minor conurbation",
rural_urban == 3 ~ "Urban - city and town",
rural_urban == 4 ~ "Urban - city and town in a sparse setting",
rural_urban == 5 ~ "Rural - town and fringe",
rural_urban == 6 ~ "Rural - town and fringe in a sparse setting",
rural_urban == 7 ~ "Rural village and dispersed",
rural_urban == 8 ~ "Rural village and dispersed in a sparse setting",
#TRUE ~ "Unknown",
TRUE ~ NA_character_
),
# Prior covid
prior_covid = as.integer(ifelse(is.na(prior_covid_date), 0, 1))
) %>%
filter(age >= 70,
sex %in% c("Male", "Female"),
!is.na(imd),
!is.na(ethnicity),
!is.na(region),
!is.na(rural_urban),
) %>%
select(patient_id, covid_vax, follow_up_time, practice_id_at_start, practice_id_latest_active_registration, stp,
age, ageband, sex, ethnicity, morbid_obesity, chronic_heart_disease, diabetes,
chronic_kidney_disease_diagnostic, chronic_kidney_disease_all_stages, chronic_kidney_disease_all_stages_3_5,
sev_mental_ill, learning_disability, chronic_neuro_dis_inc_sig_learn_dis, asplenia, chronic_liver_disease,
chronis_respiratory_disease, immunosuppression_diagnosis, immunosuppression_medication, imd, region, rural_urban,
prior_covid, flu_vaccine, shielded, shielded_since_feb_15)
# Data for modelling
data_processed_modelling <- data_processed %>%
select(-practice_id_at_start) %>%
mutate(practice_id_latest_active_registration = as.factor(practice_id_latest_active_registration)) %>%
droplevels()
# Save dataset as .rds files ----
write_rds(data_processed, here::here("output", "data", "data_all.rds"), compress="gz")
write_rds(data_processed_modelling, here::here("output", "data", "data_modelling.rds"), compress="gz")