generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_1.R
180 lines (153 loc) · 6.33 KB
/
table_1.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
######################################
# This script
# - produces a table summarising selected clinical and demographic groups in study cohort, stratified by primary vaccine product
# - saves table as html
######################################
## Import libraries
library(tidyverse)
library(here)
library(glue)
library(dplyr)
library(gt)
library(gtsummary)
library(reshape2)
# Select wave and subgroup based on input arguments
args <- commandArgs(trailingOnly=TRUE)
if(length(args)==0){
wave <- "wave4"
subgroup <- "all"
} else {
wave <- args[[1]]
subgroup <- args[[2]]
}
# Import function to rename subgroups
source(here("analysis", "utils", "rename_subgroups.R"))
# Import filtered data
data_filtered <- read_rds(here::here("output", "filtered", paste0("input_",wave,".rds")))
## Set rounding and redaction thresholds
rounding_threshold = 5
redaction_threshold = 10
## Select subset
if (subgroup=="all") {
data_filtered = data_filtered
} else if (subgroup!="all") {
data_filtered = subset(data_filtered, imm_subgroup==subgroup)
} else {
stop ("Arguments not specified correctly.")
}
# Format data
data_filtered <- data_filtered %>%
mutate(
N = 1,
)
## Baseline variables
counts <- data_filtered %>%
select(N,
# Demographics
agegroup,
sex,
ethnicity,
region,
imd,
care_home,
smoking_status_comb,
# Immunosuppression
imm_subgroup,
any_transplant_type,
any_transplant_cat,
any_bone_marrow_type,
any_bone_marrow_cat,
radio_chemo_cat,
immunosuppression_medication_cat,
immunosuppression_diagnosis_cat,
# Immunosuppression (binary)
any_transplant,
any_bone_marrow,
radio_chemo,
immunosuppression_medication,
immunosuppression_diagnosis,
# Vaccination
n_doses_wave,
pre_wave_vaccine_group,
# Prior infection group
pre_wave_infection_group,
# Prior infection/vaccination
pre_wave_vax_infection_comb,
# At risk morbidity count
multimorb_cat,
## Risk group (clinical)
bmi,
asthma,
diabetes_controlled,
ckd_rrt,
bp_ht,
chronic_respiratory_disease,
chronic_cardiac_disease,
cancer,
chronic_liver_disease,
stroke,
dementia,
other_neuro,
asplenia,
ra_sle_psoriasis,
learning_disability,
sev_mental_ill
)
# Retain detailed immunosuppression variable for all data or specific subset, otherwise retain binary variable
if (subgroup=="all") {
counts = counts %>% select(-c(any_transplant, any_bone_marrow, radio_chemo, immunosuppression_medication, immunosuppression_diagnosis))
}
if (subgroup=="Tx") {
counts = counts %>% select(-c(imm_subgroup, any_bone_marrow_type, any_bone_marrow_cat, radio_chemo_cat, immunosuppression_medication_cat, immunosuppression_diagnosis_cat,
any_transplant))
}
if (subgroup=="HC") {
counts = counts %>% select(-c(imm_subgroup, any_transplant_type, any_transplant_cat, radio_chemo_cat, immunosuppression_medication_cat, immunosuppression_diagnosis_cat,
any_bone_marrow))
}
if (subgroup=="RC") {
counts = counts %>% select(-c(imm_subgroup, any_transplant_type, any_transplant_cat, any_bone_marrow_type, any_bone_marrow_cat, immunosuppression_medication_cat, immunosuppression_diagnosis_cat,
radio_chemo))
}
if (subgroup=="IMM") {
counts = counts %>% select(-c(imm_subgroup, any_transplant_type, any_transplant_cat, any_bone_marrow_type, any_bone_marrow_cat, radio_chemo_cat, immunosuppression_diagnosis_cat,
immunosuppression_medication))
}
if (subgroup=="IMD") {
counts = counts %>% select(-c(imm_subgroup, any_transplant_type, any_transplant_cat, any_bone_marrow_type, any_bone_marrow_cat, radio_chemo_cat, immunosuppression_medication_cat,
immunosuppression_diagnosis))
}
## Create table 1
counts_summary = counts %>% tbl_summary()
counts_summary$inputs$data <- NULL
table1 <- counts_summary$table_body %>%
select(group = variable, variable = label, count = stat_0) %>%
separate(count, c("count","perc"), sep = "([(])") %>%
mutate(count = gsub(" ", "", count)) %>%
mutate(count = as.numeric(gsub(",", "", count))) %>%
filter(!(is.na(count))) %>%
select(-perc)
table1$percent = round(table1$count/nrow(data_filtered)*100,1)
colnames(table1) = c("subgroup", "level", "count", "percent")
# Relabel variables for plotting
table1 <- table1 %>% rename_subgroups()
## Calculate rounded total
rounded_n = plyr::round_any(nrow(data_filtered), rounding_threshold)
## Round individual values to rounding threshold
table1_redacted <- table1 %>%
mutate(count = plyr::round_any(count, rounding_threshold))
table1_redacted$percent = round(table1_redacted$count/rounded_n*100,1)
table1_redacted$non_count = rounded_n - table1_redacted$count
## Redact any rows with rounded cell counts or non-counts <= redaction threshold
table1_redacted$summary = paste0(prettyNum(table1_redacted$count, big.mark=",")," (",format(table1_redacted$percent,nsmall=1),"%)")
table1_redacted$summary = gsub(" ", "", table1_redacted$summary, fixed = TRUE) # Remove spaces generated by decimal formatting
table1_redacted$summary = gsub("(", " (", table1_redacted$summary, fixed = TRUE) # Add first space before (
table1_redacted$summary[(table1_redacted$count>0 & table1_redacted$count<=redaction_threshold) | (table1_redacted$non_count>0 & table1_redacted$non_count<=redaction_threshold)] = "[Redacted]"
table1_redacted$summary[table1_redacted$subgroup=="N"] = prettyNum(table1_redacted$count[table1_redacted$subgroup=="N"], big.mark=",")
table1_redacted <- table1_redacted %>% select(-non_count, -count, -percent)
## Save as html/rds
output_dir <- here("output", "table_1")
fs::dir_create(output_dir)
gt::gtsave(gt(table1_redacted), here::here("output","table_1", paste0("table_1_",wave,"_",subgroup,".html")))
write_csv(table1_redacted, here::here("output", "table_1", paste0("table_1_",wave,"_",subgroup,".csv")))
write_rds(table1_redacted, here::here("output", "table_1", paste0("table_1_",wave,"_",subgroup,".rds")), compress = "gz")