generated from opensafely/research-template
/
data_input_process.R
228 lines (199 loc) · 6.79 KB
/
data_input_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
################################################################################
# This script:
# - reads, processes the extracted data, saves the following:
# - data_covs.rds = wide covariates and outcome data
# - data_*_vax_dates.rds = long and wide vaccine data
# - data_long_*_dates.rds = long covariates and outcome data
################################################################################
## setup
library(tidyverse)
library(lubridate)
library(glue)
## source functions
source(here::here("analysis", "functions", "data_process_functions.R"))
source(here::here("analysis", "functions", "data_properties.R"))
## create folders for outputs
fs::dir_create(here::here("output", "data"))
fs::dir_create(here::here("output", "tables"))
## import study_parameters
study_parameters <- readr::read_rds(
here::here("analysis", "lib", "study_parameters.rds"))
## regions
regions <- readr::read_csv(
here::here("analysis", "lib", "regions.csv")
)
################################################################################
# initial pre-processing
cat("#### extract data ####\n")
data_extract <-
arrow::read_feather(file = here::here("output", "input_vax.feather")) %>%
# because date types are not returned consistently by cohort extractor
mutate(across(c(contains("_date")),
~ floor_date(
as.Date(., format="%Y-%m-%d"),
unit = "days"))) %>%
mutate(across(imd, ~as.integer(as.character(.x))))
cat("#### process extracted data ####\n")
data_processed_0 <- data_extract %>%
# derive ethnicity variable
mutate(
# Region
region = factor(region, levels = regions$region),
# Ethnicity
ethnicity = if_else(is.na(ethnicity_6), ethnicity_6_sus, ethnicity_6),
ethnicity = fct_case_when(
ethnicity == "1" ~ "White",
ethnicity == "4" ~ "Black",
ethnicity == "3" ~ "South Asian",
ethnicity == "2" ~ "Mixed",
ethnicity == "5" ~ "Other",
TRUE ~ NA_character_
),
# IMD quintile
imd = fct_case_when(
imd < 1 | is.na(imd) ~ NA_character_,
imd < 32844*1/5 ~ "1 most deprived",
imd < 32844*2/5 ~ "2",
imd < 32844*3/5 ~ "3",
imd < 32844*4/5 ~ "4",
TRUE ~ "5 least deprived"
),
# Sex
sex = fct_case_when(
sex == "F" ~ "Female",
sex == "M" ~ "Male",
TRUE ~ NA_character_
),
#Subgroup
subgroup = fct_case_when(
jcvi_group %in% c("04b", "06") & age_1 < 65 ~ "18-64 years and clinically vulnerable",
jcvi_group %in% c("11", "12") ~ "18-39 years",
jcvi_group %in% c("07", "08", "09", "10") ~ "40-64 years",
jcvi_group %in% c("02", "03", "04a", "04b", "05") ~ "65+ years",
TRUE ~ NA_character_
)
) %>%
select(-ethnicity_6, -ethnicity_6_sus) %>%
droplevels()
################################################################################
cat("#### properties of data_processed_0 ####\n")
# for checking for errors
data_properties(
data = data_processed_0,
path = file.path("output", "tables")
)
cat("## check subgroups as desired ##\n")
data_processed_0 %>%
group_by(subgroup, jcvi_group) %>%
count() %>%
ungroup()
################################################################################
# process vaccine data
data_vax <- local({
data_vax_pfizer <- data_processed_0 %>%
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_0 %>%
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_0 %>%
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_processed_0 %>% # to get the unvaccinated
# filter(if_all(starts_with("covid_vax"), ~ is.na(.))) %>%
filter_at(vars(starts_with("covid_vax")), all_vars(is.na(.))) %>%
select(patient_id) %>%
full_join(
data_vax_pfizer %>%
full_join(data_vax_az, by=c("patient_id", "date")) %>%
full_join(data_vax_moderna, by=c("patient_id", "date")),
by = "patient_id"
) %>%
mutate(
brand = 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() %>%
droplevels()
data_vax
})
data_vax_wide <- data_vax %>%
pivot_wider(
id_cols= patient_id,
names_from = c("vax_index"),
values_from = c("date", "brand"),
names_glue = "covid_vax_{vax_index}_{.value}"
)
readr::write_rds(
data_vax_wide,
here::here("output", "data", "data_wide_vax_dates.rds"),
compress="gz")
################################################################################
# create dataset of deaths data
## join deaths data
data_deaths <- data_processed_0 %>%
select(patient_id, contains("death")) %>%
# in case coviddeath_date and death_date different dates
mutate(across(c(coviddeath_date, death_date),
~ if_else(
!is.na(coviddeath_date) & !is.na(death_date),
pmin(coviddeath_date, death_date, na.rm = TRUE),
.x
))) %>%
# add outcome for noncoviddeath
mutate(
noncoviddeath_date = if_else(
!is.na(death_date) & is.na(coviddeath_date),
death_date,
as.Date(NA_character_))
)
################################################################################
# save dataset of covariates
# (remove variables that are saved elsewhere)
data_processed <- data_processed_0 %>%
select(
# remove vaccine variables
-contains("_vax_"),
# remove death variables
-contains("death")
) %>%
# join processed deaths data
left_join(
data_deaths,
by = "patient_id"
)
readr::write_rds(
data_processed,
here::here("output", "data", "data_processed.rds"),
compress="gz")