generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_1.R
144 lines (123 loc) · 4.66 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
######################################
# 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 <- "all"
}
# 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=="transplant") {
data_filtered = subset(data_filtered, organ_transplant==1 | bone_marrow_transplant==1)
} else if (subgroup=="haem_cancer") {
data_filtered = subset(data_filtered, haem_cancer==1)
} else if (subgroup=="immuno") {
data_filtered = subset(data_filtered, immunosuppression_diagnosis==1 | immunosuppression_medication==1)
} else if (subgroup=="radio_chemo") {
data_filtered = subset(data_filtered, radio_chemo==1)
} 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,
bmi,
smoking_status_comb,
# Immunosuppression
organ_transplant_cat,
bone_marrow_transplant_cat,
haem_cancer_cat,
immunosuppression_diagnosis_cat,
immunosuppression_medication_cat,
immunosuppression_admin_cat,
radio_chemo_cat,
# Vaccination
n_doses_omicron,
pre_omicron_vaccine_group,
# Prior infection group
pre_omicron_infection_group,
## Risk group (clinical)
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
)
## 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,".html")))
write_rds(table1_redacted, here::here("output", "table_1", paste0("table_1_",wave,".rds")), compress = "gz")