forked from opensafely/post-covid-cvd-methods
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_model_input.R
More file actions
217 lines (186 loc) · 5.1 KB
/
Copy pathmake_model_input.R
File metadata and controls
217 lines (186 loc) · 5.1 KB
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
# ------------------------------------------------------------------------------
#
# make_model_input.R
#
# Generates survival data for use when fitting cox regression models
#
# Arguments:
# - name - string, provides both the cohort and outcome
# (e.g. cohort_prevax-main-ami)
#
# Returns:
# - File of cox regression model input data
# (e.g. model_input-cohort_prevax-main-ami.rds)
#
# Authors: Venexia Walker, UoB ehrQL Team
#
# ------------------------------------------------------------------------------
# Load packages ----------------------------------------------------------------
print("Load packages")
library(magrittr)
library(data.table)
library(stringr)
library(lubridate)
# Source functions -------------------------------------------------------------
print("Source functions")
lapply(
list.files("analysis/model", full.names = TRUE, pattern = "fn-"),
source
)
# Specify arguments ------------------------------------------------------------
print("Specify arguments")
args <- commandArgs(trailingOnly = TRUE)
if (length(args) == 0) {
name <- "cohort_prevax-main-ami"
} else {
name <- args[[1]]
}
analysis <- gsub(
"cohort_.*vax-",
"",
name
)
# Define model output folder ---------------------------------------
print("Creating output/model output folder")
# setting up the sub directory
model_dir <- "output/model/"
# check if sub directory exists, create if not
fs::dir_create(here::here(model_dir))
# Load and prepare data by selecting project-required columns
print("Load and prepare data for analysis")
pmi <- prepare_model_input(name)
# Restrict to required population -------------------------------------------
print('Restrict to required population')
# Creating a pre-existing condition variable where appropriate
if (grepl("preex", name)) {
# True false indicator of preex
preex <- as.logical(
gsub(
".*preex_([^\\-]+)-.*",
"\\1",
name
)
)
# Remove preex string from analysis string
analysis <- gsub(
"_preex_.*",
"",
analysis
)
df <- pmi$input[pmi$input$sup_bin_preex == preex, ]
} else {
df <- pmi$input
}
## Perform subgroup-specific manipulation
print("Perform subgroup-specific manipulation")
print(paste0("Make model input: ", analysis))
check_for_subgroup <- (grepl("main", analysis)) # TRUE if subgroup is main, FALSE otherwise
# Make model input: main/sub_covidhistory ------------------------------------
if (grepl("sub_covidhistory", analysis)) {
check_for_subgroup <- TRUE
df <- df[df$sub_bin_covidhistory == TRUE, ] # Only selecting for this subgroup
} else {
df <- df[df$sub_bin_covidhistory == FALSE, ] # all other subgroups (inc. Main)
}
# Make model input: sub_covidhospital ----------------------------------------
if (grepl("sub_covidhospital", analysis)) {
check_for_subgroup <- TRUE
if (grepl("sub_covidhospital_TRUE", analysis)) {
covidhosp <- TRUE
}
if (grepl("sub_covidhospital_FALSE", analysis)) {
covidhosp <- FALSE
}
str_covidhosp_cens <- ifelse(covidhosp, "non_hospitalised", "hospitalised")
df <- df %>%
dplyr::mutate(
end_date_outcome = as.Date(
ifelse(
sub_cat_covidhospital == str_covidhosp_cens,
exp_date - 1,
end_date_outcome
),
origin = .Date(0)
),
exp_date = as.Date(
ifelse(
sub_cat_covidhospital == str_covidhosp_cens,
NA_Date_,
exp_date
),
origin = .Date(0)
),
out_date = as.Date(
ifelse(
out_date > end_date_outcome,
NA_Date_,
out_date
),
origin = .Date(0)
)
) %>%
dplyr::filter(end_date_outcome >= index_date)
}
# Make model input: sub_sex_* ------------------------------------------------
if (grepl("sub_sex_", analysis)) {
check_for_subgroup <- TRUE
sex <- str_to_title(gsub(
".*sub_sex_",
"",
analysis
))
df <- df[df$cov_cat_sex == sex, ]
}
# Make model input: sub_age_* ------------------------------------------------
if (grepl("sub_age_", analysis) == TRUE) {
check_for_subgroup <- TRUE
min_age <- as.numeric(strsplit(
gsub(".*sub_age_", "", analysis),
split = "_"
)[[1]][1])
max_age <- as.numeric(strsplit(
gsub(".*sub_age_", "", analysis),
split = "_"
)[[1]][2])
df <- df[
df$cov_num_age >= min_age &
df$cov_num_age < (max_age + 1),
]
}
# Make model input: sub_ethnicity_* ------------------------------------------
if (grepl("sub_ethnicity_", analysis) == TRUE) {
check_for_subgroup <- TRUE
ethnicity <- str_to_title(gsub(
"_",
" ",
gsub(
".*sub_ethnicity_",
"",
analysis
)
))
df <- df[df$cov_cat_ethnicity == ethnicity, ]
}
# Stop code if no subgroup/main analysis was correctly selected
if (isFALSE(check_for_subgroup)) {
stop(paste0("Input: ", name, " did not undergo any subgroup filtering!"))
}
# Save model output
df <- df %>%
dplyr::select(tidyselect::all_of(pmi$keep))
check_vitals(df)
readr::write_rds(
df,
file.path(
model_dir,
paste0("model_input-", name, ".rds")
),
compress = "gz"
)
print(paste0(
"Saved: ",
model_dir,
"model_input-",
name,
".rds"
))