generated from opensafely/research-template
/
table_2.R
206 lines (172 loc) · 9.87 KB
/
table_2.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
######################################
# This script
# - produces a table with the number of patients fully vaccinated (2 doses + 2 weeks) in
# selected clinical and demographic groups
# - saves table as html
######################################
# Preliminaries ----
## Import libraries
library('tidyverse')
library('here')
library('glue')
library('gt')
library('gtsummary')
## Import custom user functions
source(here("analysis", "functions.R"))
## Create output directory
fs::dir_create(here::here("output", "tables"))
## Import data
data_processed <- read_rds(here::here("output", "data", "data_processed.rds"))
# Format data ----
## Filter on 80+ group
data_cohort_over80 <- data_processed %>%
mutate(group = ifelse(care_home_65plus == 1, 1, NA),
group = ifelse(is.na(group) & ageband == 3, 2, group),
group = ifelse(is.na(group) & hscworker == 1, 3, group),
group = ifelse(is.na(group) & ageband == 2, 4, group),
group = ifelse(is.na(group) & shielded == 1, 5, group),
group = ifelse(is.na(group) & age >=50 & age <70, 6, group),
group = ifelse(is.na(group), 7, group),
group = factor(group)) %>%
filter(group == 2)
# Table 2 ----
rates0_over80 <- data_cohort_over80 %>%
mutate(time_since_fully_vaccinated = cut(follow_up_time_vax2 - 14,
breaks = c(14, 28, 42, 56, 84, Inf),
labels = c("2-4 weeks", "4-6 weeks", "6-8 weeks", "8-12 weeks", "12+ weeks"),
right = FALSE),
time_between_vaccinations = cut(tbv,
breaks = c(0, 42, 84, Inf),
labels = c("6 weeks or less", "6-12 weeks", "12 weeks or more"),
right = FALSE),
smoking_status = ifelse(is.na(smoking_status), "N&M", smoking_status)) %>%
select(ageband2,
sex,
bmi,
smoking_status,
ethnicity,
imd,
region,
asthma,
asplenia,
bpcat,
chd,
chronic_neuro_dis_inc_sig_learn_dis,
chronic_resp_dis,
chronic_kidney_disease,
end_stage_renal,
cld,
diabetes,
immunosuppression,
learning_disability,
sev_mental_ill,
organ_transplant,
time_since_fully_vaccinated,
time_between_vaccinations,
prior_covid) %>%
tbl_summary()
rates0_over80 <- rates0_over80$table_body %>%
select(group = variable, variable = label, count = stat_0) %>%
separate(count, c("count","perc"), sep = "([(])") %>%
#mutate(count = as.numeric(count),
# perc = gsub('.{2}$', '', perc)) %>%
mutate(count = gsub(" ", "", count),
count = as.numeric(gsub(",", "", count))) %>%
filter(!(is.na(count))) %>%
select(-perc) %>%
mutate(variable = ifelse(variable == "prior_covid", 1, variable))
rates1_over80 <- calculate_rates(group = "covid_positive_test",
follow_up = "time_to_positive_test",
data = data_cohort_over80,
Y = 1,
dig = 2,
variables = c("ageband2", "sex", "bmi", "smoking_status", "ethnicity",
"imd", "region", "asthma", "asplenia", "bpcat", "chd",
"chronic_neuro_dis_inc_sig_learn_dis", "chronic_resp_dis",
"chronic_kidney_disease", "end_stage_renal","cld",
"diabetes", "immunosuppression", "learning_disability",
"sev_mental_ill", "organ_transplant", "time_since_fully_vaccinated",
"time_between_vaccinations", "prior_covid"))
rates2_over80 <- calculate_rates(group = "covid_hospital_admission",
follow_up = "time_to_hospitalisation",
data = data_cohort_over80,
Y = 1,
dig = 2,
variables = c("ageband2", "sex", "bmi", "smoking_status", "ethnicity",
"imd", "region", "asthma", "asplenia", "bpcat", "chd",
"chronic_neuro_dis_inc_sig_learn_dis", "chronic_resp_dis",
"chronic_kidney_disease", "end_stage_renal","cld",
"diabetes", "immunosuppression", "learning_disability",
"sev_mental_ill", "organ_transplant", "time_since_fully_vaccinated",
"time_between_vaccinations", "prior_covid"))
rates3_over80 <- calculate_rates(group = "covid_death",
follow_up = "time_to_covid_death",
data = data_cohort_over80,
Y = 1,
dig = 2,
variables = c("ageband2", "sex", "bmi", "smoking_status", "ethnicity",
"imd", "region", "asthma", "asplenia", "bpcat", "chd",
"chronic_neuro_dis_inc_sig_learn_dis", "chronic_resp_dis",
"chronic_kidney_disease", "end_stage_renal","cld",
"diabetes", "immunosuppression", "learning_disability",
"sev_mental_ill", "organ_transplant", "time_since_fully_vaccinated",
"time_between_vaccinations", "prior_covid"))
table2_over80s <- left_join(rates0_over80, rates1_over80, by = c("group", "variable")) %>%
left_join(rates2_over80, by = c("group", "variable")) %>%
left_join(rates3_over80, by = c("group", "variable")) %>%
mutate(follow_up.x = round(follow_up.x, digits = 0),
follow_up.y = round(follow_up.y, digits = 0),
follow_up = round(follow_up, digits = 0))
colnames(table2_over80s) = c("Variable", "level",
"Fully vaccinated",
"covid_positive_test", "PYs_1", "Rate1", "LCI1", "UCI1",
"covid_hospital_admission", "PYs_2", "Rate2", "LCI2", "UCI2",
"covid_death", "PYs_3", "Rate3", "LCI3", "UCI3")
# Redaction ----
## Redact values < 8
threshold = 8
table2_over80s_redacted <- table2_over80s %>%
mutate(`Fully vaccinated` = ifelse(`Fully vaccinated` < threshold, NA, `Fully vaccinated`),
covid_positive_test = ifelse(covid_positive_test < threshold, NA, covid_positive_test),
PYs_1 = ifelse(is.na(covid_positive_test), NA, PYs_1),
Rate1 = ifelse(is.na(covid_positive_test), NA, Rate1),
LCI1 = ifelse(is.na(covid_positive_test), NA, LCI1),
UCI1 = ifelse(is.na(covid_positive_test), NA, UCI1),
covid_hospital_admission = ifelse(covid_hospital_admission < threshold, NA, covid_hospital_admission),
PYs_2 = ifelse(is.na(covid_hospital_admission), NA, PYs_2),
Rate2 = ifelse(is.na(covid_hospital_admission), NA, Rate2),
LCI2 = ifelse(is.na(covid_hospital_admission), NA, LCI2),
UCI2 = ifelse(is.na(covid_hospital_admission), NA, UCI2),
covid_death = ifelse(covid_death < threshold, NA, covid_death),
PYs_3 = ifelse(is.na(covid_death), NA, PYs_3),
Rate3 = ifelse(is.na(covid_death), NA, Rate3),
LCI3 = ifelse(is.na(covid_death), NA, LCI3),
UCI3 = ifelse(is.na(covid_death), NA, UCI3))
## Round to nearest 5
table2_over80s_redacted <- table2_over80s_redacted %>%
mutate(`Fully vaccinated` = plyr::round_any(`Fully vaccinated`, 5),
covid_positive_test = plyr::round_any(covid_positive_test, 5),
covid_hospital_admission = plyr::round_any(covid_hospital_admission, 5),
covid_death = plyr::round_any(covid_death, 5))
# ## Recalculate column totals
# results.table_redacted[1, "Fully vaccinated"] <- sum(results.table_redacted[-1,]$`Fully vaccinated`, na.rm = T)
# results.table_redacted[1, "Positive COVID test"] <- sum(results.table_redacted[-1,]$`Positive COVID test`, na.rm = T)
# results.table_redacted[1, "Hospitalised with COVID"] <- sum(results.table_redacted[-1,]$`Hospitalised with COVID`, na.rm = T)
# results.table_redacted[1, "COVID Deaths"] <- sum(results.table_redacted[-1,]$`COVID Deaths`, na.rm = T)
## Replace na with [REDACTED]
# table2_over80s_redacted <- table2_over80s_redacted %>%
# replace(is.na(.), "[REDACTED]")
## Formatting
table2_over80s_redacted <- table2_over80s_redacted %>%
mutate(Fully_vaccinated_count = `Fully vaccinated`,
Positive_test_count = paste(covid_positive_test, " (", PYs_1, ")", sep = ""),
Positive_test_rate = paste(Rate1, " (", LCI1, "-", UCI1, ")", sep = ""),
Hospitalised_count = paste(covid_hospital_admission, " (", PYs_2, ")", sep = ""),
Hospitalised_rate = paste(Rate2, " (", LCI2, "-", UCI2, ")", sep = ""),
Death_count = paste(covid_death, " (", PYs_3, ")", sep = ""),
Death_rate = paste(Rate3, " (", LCI3, "-", UCI3, ")", sep = "")) %>%
select(Variable, level, Fully_vaccinated_count, Positive_test_count, Positive_test_rate, Hospitalised_count, Hospitalised_rate,
Death_count, Death_rate)
# Save as html ----
gt::gtsave(gt(table2_over80s), here::here("output","tables", "table2.html"))
gt::gtsave(gt(table2_over80s_redacted), here::here("output","tables", "table2_redacted.html"))