generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_model_input.R
280 lines (191 loc) · 10.7 KB
/
make_model_input.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
# Load packages ----------------------------------------------------------------
print('Load packages')
library(magrittr)
library(data.table)
# Source functions -------------------------------------------------------------
print('Source functions')
source("analysis/fn-check_vitals.R")
# Specify arguments ------------------------------------------------------------
print('Specify arguments')
args <- commandArgs(trailingOnly=TRUE)
if(length(args)==0){
name <- "cohort_prevax_extf-sub_sex_female-depression" # prepare datasets for all active analyses
# name <- "cohort_vax-sub_history_none-depression" # prepare datasets for all active analyses whose name contains X
# name <- "vax-depression-main;vax-depression-sub_covid_hospitalised;vax-depression-sub_covid_nonhospitalised" # prepare datasets for specific active analyses
} else {
name <- args[[1]]
}
# Load active analyses ---------------------------------------------------------
print('Load active analyses')
active_analyses <- readr::read_rds("lib/active_analyses.rds")
# Identify model inputs to be prepared -----------------------------------------
print('Identify model inputs to be prepared')
if (name=="all") {
prepare <- active_analyses$name
} else if(grepl(";",name)) {
prepare <- stringr::str_split(as.vector(name), ";")[[1]]
} else {
prepare <- active_analyses[grepl(name,active_analyses$name),]$name
}
# Filter active_analyses to model inputs to be prepared ------------------------
print('Filter active_analyses to model inputs to be prepared')
active_analyses <- active_analyses[active_analyses$name %in% prepare,]
for (i in 1:nrow(active_analyses)) {
# Load data ------------------------------------------------------------------
print(paste0("Load data for ",active_analyses$name[i]))
input <- dplyr::as_tibble(readr::read_rds(paste0("output/input_",active_analyses$cohort[i],"_stage1.rds")))
# Restrict to required variables for dataset preparation ---------------------
print('Restrict to required variables for dataset preparation')
input <- input[,unique(c("patient_id",
"index_date",
"end_date_exposure",
"end_date_outcome",
active_analyses$exposure[i],
active_analyses$outcome[i],
unlist(strsplit(active_analyses$strata[i], split = ";")),
unlist(strsplit(active_analyses$covariate_other[i], split = ";")),
"sub_cat_covid19_hospital",
"sub_bin_covid19_confirmed_history",
"cov_cat_sex",
"cov_num_age",
"cov_cat_ethnicity",
colnames(input)[grepl("cov_cat_history_",colnames(input))]))]
# Identify final list of variables to keep -----------------------------------
print('Identify final list of variables to keep')
keep <- c("patient_id","index_date","exp_date","out_date","end_date_exposure","end_date_outcome")
varlists <- c("strata","covariate_age","covariate_sex","covariate_other")
for (j in varlists) {
if (active_analyses[i,j] == "NULL") {
tmp <- NULL
} else {
tmp <- stringr::str_split(as.vector(active_analyses[i,j]), ";")[[1]]
}
keep <- c(keep,tmp)
rm(tmp)
}
# Remove outcomes outside of follow-up time ----------------------------------
print('Remove outcomes outside of follow-up time')
input <- dplyr::rename(input,
"out_date" = active_analyses$outcome[i],
"exp_date" = active_analyses$exposure[i])
input <- input %>%
dplyr::mutate(out_date = replace(out_date, which(out_date>end_date_outcome | out_date<index_date), NA),
exp_date = replace(exp_date, which(exp_date>end_date_exposure | exp_date<index_date), NA),
sub_cat_covid19_hospital = replace(sub_cat_covid19_hospital, which(is.na(exp_date)),"no_infection"))
# Update end date to be outcome date where applicable ------------------------
print('Update end date to be outcome date where applicable')
input <- input %>%
dplyr::rowwise() %>%
dplyr::mutate(end_date_outcome = min(end_date_outcome, out_date, na.rm = TRUE))
# Make model input: main and day0 --------------------------------------------
if (grepl("main",active_analyses$analysis[i])) {
print(paste0('Make model input: ',active_analyses$analysis[i]))
df <- input[input$sub_bin_covid19_confirmed_history==FALSE,]
df <- df %>%
dplyr::select(tidyselect::all_of(keep))
check_vitals(df)
readr::write_rds(df, file.path("output", paste0("model_input-",active_analyses$name[i],".rds")), compress = "gz")
print(paste0("Saved: output/model_input-",active_analyses$name[i],".rds"))
rm(df)
}
# Make model input: sub_covid_hospitalised -----------------------------------
if (grepl("sub_covid_hospitalised",active_analyses$analysis[i])) {
print(paste0('Make model input: ',active_analyses$analysis[i]))
df <- input[input$sub_bin_covid19_confirmed_history==FALSE,]
df <- df %>%
dplyr::mutate(end_date_outcome = replace(end_date_outcome, which(sub_cat_covid19_hospital=="non_hospitalised"), exp_date-1),
exp_date = replace(exp_date, which(sub_cat_covid19_hospital=="non_hospitalised"), NA),
out_date = replace(out_date, which(out_date>end_date_outcome), NA))
df <- df[df$end_date_outcome>=df$index_date,]
df <- df %>%
dplyr::select(tidyselect::all_of(keep))
check_vitals(df)
readr::write_rds(df, file.path("output", paste0("model_input-",active_analyses$name[i],".rds")), compress = "gz")
print(paste0("Saved: output/model_input-",active_analyses$name[i],".rds"))
rm(df)
}
# Make model input: sub_covid_nonhospitalised --------------------------------
if (grepl("sub_covid_nonhospitalised",active_analyses$analysis[i])) {
print(paste0('Make model input: ',active_analyses$analysis[i]))
df <- input[input$sub_bin_covid19_confirmed_history==FALSE,]
df <- df %>%
dplyr::mutate(end_date_outcome = replace(end_date_outcome, which(sub_cat_covid19_hospital=="hospitalised"), exp_date-1),
exp_date = replace(exp_date, which(sub_cat_covid19_hospital=="hospitalised"), NA),
out_date = replace(out_date, which(out_date>end_date_outcome), NA))
df <- df[df$end_date_outcome>=df$index_date,]
df$index_date <- as.Date(df$index_date)
df <- df %>%
dplyr::select(tidyselect::all_of(keep))
check_vitals(df)
readr::write_rds(df, file.path("output", paste0("model_input-",active_analyses$name[i],".rds")), compress = "gz")
print(paste0("Saved: output/model_input-",active_analyses$name[i],".rds"))
rm(df)
}
# Make model input: sub_covid_history ----------------------------------------
if (grepl("sub_covid_history",active_analyses$analysis[i])) {
print(paste0('Make model input: ',active_analyses$analysis[i]))
df <- input[input$sub_bin_covid19_confirmed_history==TRUE,]
df <- df %>%
dplyr::select(tidyselect::all_of(keep))
check_vitals(df)
readr::write_rds(df, file.path("output", paste0("model_input-",active_analyses$name[i],".rds")), compress = "gz")
print(paste0("Saved: output/model_input-",active_analyses$name[i],".rds"))
rm(df)
}
# Make model input: sub_sex_* ------------------------------------------------
if (grepl("sub_sex_",active_analyses$analysis[i])) {
print(paste0('Make model input: ',active_analyses$analysis[i]))
sex <- stringr::str_to_title(gsub(".*sub_sex_","",active_analyses$analysis[i]))
df <- input[input$sub_bin_covid19_confirmed_history==FALSE &
input$cov_cat_sex==sex,]
df <- df %>%
dplyr::select(tidyselect::all_of(keep))
check_vitals(df)
readr::write_rds(df, file.path("output", paste0("model_input-",active_analyses$name[i],".rds")), compress = "gz")
print(paste0("Saved: output/model_input-",active_analyses$name[i],".rds"))
rm(df)
}
# Make model input: sub_age_* ------------------------------------------------
if (grepl("sub_age_",active_analyses$analysis[i])==TRUE) {
print(paste0('Make model input: ',active_analyses$analysis[i]))
min_age <- as.numeric(strsplit(gsub(".*sub_age_","",active_analyses$analysis[i]), split = "_")[[1]][1])
max_age <- as.numeric(strsplit(gsub(".*sub_age_","",active_analyses$analysis[i]), split = "_")[[1]][2])
df <- input[input$sub_bin_covid19_confirmed_history==FALSE &
input$cov_num_age>=min_age &
input$cov_num_age<ifelse(max_age==110,max_age+1,max_age),]
df <- df %>%
dplyr::select(tidyselect::all_of(keep))
check_vitals(df)
readr::write_rds(df, file.path("output", paste0("model_input-",active_analyses$name[i],".rds")), compress = "gz")
print(paste0("Saved: output/model_input-",active_analyses$name[i],".rds"))
rm(df)
}
# Make model input: sub_ethnicity_* ------------------------------------------
if (grepl("sub_ethnicity_",active_analyses$analysis[i])==TRUE) {
print(paste0('Make model input: ',active_analyses$analysis[i]))
ethnicity <- stringr::str_to_title(gsub(".*sub_ethnicity_","",active_analyses$analysis[i]))
ethnicity <- ifelse(ethnicity=="Asian","South Asian",ethnicity)
df <- input[input$sub_bin_covid19_confirmed_history==FALSE &
input$cov_cat_ethnicity==ethnicity,]
df <- df %>%
dplyr::select(tidyselect::all_of(keep))
check_vitals(df)
readr::write_rds(df, file.path("output", paste0("model_input-",active_analyses$name[i],".rds")), compress = "gz")
print(paste0("Saved: output/model_input-",active_analyses$name[i],".rds"))
rm(df)
}
# Make model input: sub_history_* --------------------------------------------
if (grepl("sub_history_",active_analyses$analysis[i])==TRUE) {
print(paste0('Make model input: ',active_analyses$analysis[i]))
history <- gsub(".*sub_history_","",active_analyses$analysis[i])
df <- input[input$sub_bin_covid19_confirmed_history==FALSE,]
df <- dplyr::rename(df, "history" = gsub("out_date","cov_cat_history",active_analyses$outcome[i]))
df <- df[df$history==history & !is.na(df$history),]
df <- df %>%
dplyr::select(tidyselect::all_of(keep))
check_vitals(df)
readr::write_rds(df, file.path("output", paste0("model_input-",active_analyses$name[i],".rds")), compress = "gz")
print(paste0("Saved: output/model_input-",active_analyses$name[i],".rds"))
rm(df)
}
}