generated from opensafely/research-template
/
table1.R
351 lines (299 loc) · 10.2 KB
/
table1.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
library(tidyverse)
library(glue)
library(lubridate)
library(gt)
################################################################################
fs::dir_create(here::here("output", "report", "tables"))
################################################################################
# read list of covariates for model
model_varlist <- readr::read_rds(
here::here("analysis", "lib", "model_varlist.rds"))
# read strata_vars
strata_vars <- readr::read_rds(
here::here("analysis", "lib", "strata_vars.rds"))
strata_vars <- strata_vars[strata_vars!="elig_date"]
# processed data
data_processed <- readr::read_rds(
here::here("output", "data", "data_processed.rds"))
# covariates data
data_covariates <- readr::read_rds(
here::here("output", "data", "data_covariates.rds"))
# read subgroups
subgroups <- readr::read_rds(
here::here("analysis", "lib", "subgroups.rds"))
# redaction functions
source(here::here("analysis", "functions", "redaction_functions.R"))
################################################################################
# function to be applied in dplyr::filter
no_evidence_of <- function(cov_date, index_date) {
is.na(cov_date) | index_date < cov_date
}
################################################################################
# prepare data
data_tables <- data_covariates %>%
filter(k==1) %>%
left_join(data_processed %>%
select(patient_id, subgroup,
all_of(unname(strata_vars)),
sex, imd, ethnicity,
death_date, dereg_date),
by = "patient_id") %>%
mutate(group = if_else(arm == "unvax", "unvax", "vax"))
# eligible for comparison period 1
eligibility_count <- data_tables %>%
group_by(group) %>%
count() %>%
ungroup() %>%
transmute(
description = glue("{group}: satisfying eligibility criteria up to and including box E."),
n
)
# remove if death before start of comparison 1
data_tables <- data_tables %>%
filter_at(
vars("death_date"),
all_vars(no_evidence_of(., start_k_date)))
eligibility_count <- eligibility_count %>%
bind_rows(
data_tables %>%
group_by(group) %>%
count() %>%
ungroup() %>%
transmute(
description = glue("{group}: after removing those who died before start of period 1."),
n
))
# remove if dereg before start of comparison 1
data_tables <- data_tables %>%
filter_at(
vars("dereg_date"),
all_vars(no_evidence_of(., start_k_date)))
eligibility_count <- eligibility_count %>%
bind_rows(
data_tables %>%
group_by(group) %>%
count() %>%
ungroup() %>%
transmute(
description = glue("{group}: after removing those who deregistered before start of period 1."),
n
))
# remove if subsequent_vax before start of comparison 1
data_tables <- data_tables %>%
filter_at(
vars("subsequent_vax_date"),
all_vars(no_evidence_of(., start_k_date)))
eligibility_count <- eligibility_count %>%
bind_rows(
data_tables %>%
group_by(group) %>%
count() %>%
ungroup() %>%
transmute(
description = glue("{group}: after removing those who received a subsequent dose before start of period 1."),
n
))
eligibility_count_p1 <- eligibility_count %>%
mutate(group = str_extract(description, "\\w+:")) %>%
arrange(group) %>%
# round to nearest 10
mutate(across(n, ~round(.x, -1))) %>%
group_by(group) %>%
mutate(n_removed = lag(n) - n) %>%
ungroup() %>%
select(-group)
readr::write_csv(
eligibility_count_p1,
here::here("output", "tables", "eligibility_count_p1.csv"))
################################################################################
# split data in subgropus
data_tables <- data_tables %>%
select(patient_id, arm, region, jcvi_group, subgroup,
all_of(unname(unlist(model_varlist)))) %>%
group_split(subgroup)
################################################################################
# make table1 for all and each subgroup
for (i in c(0, seq_along(data_tables))) {
cat(glue("---- loop {i} ----"), "\n")
cat("---- define obejcts ----\n")
variables <- c(unname(strata_vars), unname(unlist(model_varlist)))
variables <- variables[variables != "age"]
vars_ordered_levs <- c("region", "jcvi_group", "sex", "imd", "ethnicity", "bmi", "multimorb", "test_hist_n")
# tibble for assigning tidy variable names
var_labels <- tibble(
variable = c(strata_vars, model_varlist$clinical, model_varlist$demographic),
variable_label = names(c(strata_vars, model_varlist$clinical, model_varlist$demographic))
)
if (i == 0) {
data <- bind_rows(data_tables) %>%
droplevels()
subgroup <- "All subgroups"
subgroup_label <- 5
variables <- c("subgroup", variables)
vars_ordered_levs <- c("subgroup", vars_ordered_levs)
var_labels <- var_labels %>%
add_row(variable = "subgroup", variable_label = "Subgroup",
.before=TRUE)
min_elig_date <- "2020-12-08"
} else {
data <- data_tables[[i]] %>%
droplevels()
subgroup <- unique(data$subgroup)
subgroup_label <- which(subgroups == subgroup)
min_elig_date <- data_processed %>%
filter(subgroup %in% subgroup) %>%
summarise(min_elig_date = min(elig_date))
min_elig_date <- min_elig_date$min_elig_date
}
# function for creating tibble of categories for each variable
var_tibble <- function(var) {
var_region <- tibble(
variable = var,
category = levels(data[[var]])
)
}
# tibble for specifying order of variables and categories
var_order <- tibble(
variable = variables
) %>%
left_join(
bind_rows(
lapply(vars_ordered_levs,
var_tibble)),
by = "variable"
) %>%
mutate(across(category, ~ if_else(is.na(.x), "yes", .x)))
cat("---- summarise variables ----\n")
# summarise each variable and (within variables) redact values <=5
summary_var <- function(.data, var) {
out <- .data %>%
group_by(arm, !! sym(var)) %>%
count() %>%
ungroup(!! sym(var)) %>%
mutate(arm_total = sum(n)) %>%
ungroup() %>%
mutate(percent = round(100*n/arm_total,0)) %>%
group_by(arm, !! sym(var)) %>%
mutate(across(n, redactor2)) %>%
ungroup() %>%
mutate(across(percent,
~if_else(
is.na(n) | n == 0,
"-",
as.character(.x)))) %>%
mutate(across(n,
~if_else(
is.na(.x) | .x == 0,
"-",
scales::comma(.x, accuracy = 1)))) %>%
mutate(value = as.character(glue("{n} ({percent}%)"))) %>%
select(arm, !! sym(var), value) %>%
pivot_wider(
names_from = arm,
values_from = value
) %>%
mutate(variable = var) %>%
rename("category" = var)
if (is.logical(out$category)) {
out <- out %>%
filter(category) %>%
mutate(across(category, ~ "yes"))
}
return(out)
}
cat("---- make table 1 ----\n")
# make table1
table1 <- bind_rows(lapply(
variables,
function(x)
data %>% summary_var(var = x)
))
cat("---- tidy table 1 ----\n")
# vairables under "History of" heading
history_of_vars <- c(
"chronic_respiratory_disease",
"chronic_heart_disease",
"chronic_liver_disease",
"ckd",
"chronic_neuro_inc_ld",
"diabetes",
"any_immunosuppression",
"ld_inc_ds_and_cp",
"sev_ment")
# tidy table1
table1_tidy <- var_order %>%
left_join(var_labels, by = "variable") %>%
left_join(table1, by = c("category", "variable")) %>%
mutate(across(category,
~ if_else(variable %in% history_of_vars, variable_label, .x))) %>%
mutate(across(variable_label,
~ if_else(variable %in% history_of_vars, "History of", .x))) %>%
mutate(across(variable_label, ~ str_replace(.x, "min_elig_date", as.character(min_elig_date)))) %>%
rename(Variable = variable_label, Characteristic = category, Unvaccinated = unvax) %>%
select(-variable) %>%
select(Variable, Characteristic, everything()) %>%
mutate(across(c(BNT162b2, ChAdOx1, Unvaccinated),
~ if_else(is.na(.x), "- (-%)", .x)))
# age summary
age_summary <- data %>%
group_by(arm) %>%
summarise(
median = median(age, na.rm=TRUE),
iqr = IQR(age, na.rm = TRUE),
.groups = "keep") %>%
ungroup() %>%
transmute(arm, value = as.character(glue("{median} ({iqr})"))) %>%
pivot_wider(names_from = "arm", values_from = "value") %>%
mutate(Variable = "Age", Characteristic = "Median (IQR)")
# age_missing <- data %>%
# group_by(arm) %>%
# summarise(
# missing = sum(is.na(age)),
# .groups = "keep") %>%
# ungroup() %>%
# transmute(arm, value = scales::comma(missing, accuracy = 1)) %>%
# pivot_wider(names_from = "arm", values_from = "value") %>%
# mutate(Variable = "Age", Characteristic = "Missing")
table1_tidy_n <- data %>%
group_by(arm) %>%
count() %>%
ungroup() %>%
pivot_wider(names_from = arm, values_from = n) %>%
mutate(Variable = "", Characteristic = "N") %>%
mutate(across(c(BNT162b2, ChAdOx1, unvax),
~ scales::comma(.x, accuracy = 1))) %>%
bind_rows(
age_summary
) %>%
rename(Unvaccinated = unvax) %>%
bind_rows(
table1_tidy
)
cat("---- save table1.csv ----\n")
# save table1_tidy
readr::write_csv(table1_tidy_n,
here::here("output", "report", "tables", glue("table1_{subgroup_label}_REDACTED.csv")))
cat("---- save table1.html ----\n")
table1_tidy_n %>%
gt(
groupname_col="Variable",
rowname_col = "Characteristic"
) %>%
tab_header(
title = glue("Subgroup: {subgroup}"),
subtitle = "Patient characteristics as of second vaccination period + 2 weeks") %>%
tab_style(
style = cell_text(weight="bold"),
locations = list(
cells_column_labels(
columns = everything()
),
cells_row_groups(
groups = everything()
))
) %>%
gtsave(
filename = glue("table1_{subgroup_label}_REDACTED.html"),
path = here::here("output", "report", "tables")
)
}