generated from opensafely/research-template
/
descriptive.R
386 lines (317 loc) · 13 KB
/
descriptive.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
################################################################################
# Description: Preliminary data summaries
#
# Generate summary tables and descriptive plots
#
# Depends on data_setup.R and get_community_prevalence.R
#
# Author: Emily S Nightingale
# Date: 06/08/2020
#
################################################################################
sink("./log_descriptive.txt", type = "output")
time_desc <- Sys.time()
################################################################################
#----------------------#
# SETUP ENVIRONMENT #
#----------------------#
library(tidyverse)
library(lubridate)
library(data.table)
library(sf)
theme_set(theme_bw())
# ---------------------------------------------------------------------------- #
#----------------------#
# LOAD DATA #
#----------------------#
# Shapefiles
msoa_shp <- readRDS("./data/msoa_shp.rds")
# Individual data - all
input <- readRDS("./input_clean.rds")
# Community prevalence
comm_prev <- readRDS("./community_prevalence.rds")
# Individual data - care home residents
ch <- readRDS("./ch_linelist.rds")
# Aggregated data - daily care home events
ch_long <- readRDS("./ch_agg_long.rds")
# Analysis
dat <- readRDS("./analysisdata.rds")
study_per <- range(dat$date)
# ---------------------------------------------------------------------------- #
# Total care homes in analysis dataset
N_ch_tot <- n_distinct(dat$household_id)
print("Total included care homes:")
N_ch_tot
# Number of carehomes per MSOA
ch %>%
group_by(msoa) %>%
summarise(
n_resid = n_distinct(patient_id),
n_ch = n_distinct(household_id),
n_gp = n_distinct(practice_id)) %>%
ungroup() -> per_msoa
write.csv(per_msoa, file = "./ch_gp_permsoa.csv", row.names = FALSE)
print("Summary: number of carehomes per MSOA")
per_msoa %>%
pull(n_ch) %>%
summary()
#------------------------------------------------------------------------------#
## Summary tables: care home characteristics ##
# Summarise characteristics overall, and by whether or not care home had any
# covid-event recorded in data (ever-affected). Size of each home estimated as
# number of patients registered under that household ID, but actual capacity may
# be larger.
chars <- c("household_id","msoa","n_resid","ch_size","ch_type","rural_urban",
"imd","hh_med_age","hh_p_female","hh_prop_min","hh_p_dem",
"first_event", "ever_affected")
included <- unique(dat$household_id)
ch_chars <- ch_long %>%
dplyr::select(all_of(chars)) %>%
unique() %>%
filter(household_id %in% included)
print("Missingness in care home characteristics:")
print(
ch_chars %>%
summarise_all(function(x) sum(is.na(x))) %>%
pivot_longer(cols = everything())
)
ch_overall <- ch_chars %>%
mutate(ever_affected = "Overall")
chars_waffect <- ch_chars %>%
mutate(ever_affected = ifelse(ever_affected, "Affected","Unaffected")) %>%
bind_rows(ch_overall) %>%
mutate(ever_affected = factor(ever_affected,
levels = c("Overall","Affected","Unaffected")))
# Tabulate care home type (factor):
chars_waffect %>%
group_by(ever_affected, ch_type) %>%
summarise(n_resid = n()) %>%
ungroup() %>%
mutate(ch_type = replace_na(as.character(ch_type), "missing")) %>%
pivot_wider(names_from = ch_type, values_from = n_resid, names_prefix = "Type:") %>%
rowwise() %>%
mutate(n_resid = sum(c_across(cols = -ever_affected))) %>%
ungroup() %>%
mutate_at(vars(-n_resid, -ever_affected),
function(x) paste0(x, " (", round(x/.$n_resid,2), ")")) %>%
column_to_rownames("ever_affected") %>%
dplyr::select(-n_resid) -> tab_type
# Tabulate other variables
chars_waffect %>%
group_by(ever_affected) %>%
summarise(N = n(),
`No. TPP residents` = sum(n_resid, na.rm = T),
ch_size_mean = round(mean(ch_size, na.rm = T),2),
ch_size_sd = round(sqrt(var(ch_size, na.rm = T)),2),
# `% rural` = round(sum(rural_urban == "rural")/N, 2),
imd_mean= round(mean(imd, na.rm = T)),
imd_sd= round(sqrt(var(imd, na.rm = T))),
imd_quants = paste(round(quantile(imd,
probs = c(0.25, 0.75),
na.rm = T)), collapse = ", "),
) %>%
mutate(N_perc = round(N/N_ch_tot,2),
# `N (%)` = paste0(N, " (",N_perc,")"),
`size mean(sd)` = paste0(ch_size_mean, " (",ch_size_sd,")"),
`IMD mean(sd)` = paste0(imd_mean, " (",imd_sd,")")) %>%
ungroup() %>%
remove_rownames() %>%
column_to_rownames(var = "ever_affected") %>%
dplyr::select(N, `IMD mean(sd)`, `size mean(sd)`, `No. TPP residents`) %>% #`% rural`,
cbind(tab_type) -> tab1
print("Summarise carehome characteristics by ever affected:")
tab1
#------------------------------------------------------------------------------#
# Age, dementia status and ethnicity of care home residents, stratified by
# whether or not their home was affected (percentages out of total residents in
# that stratum):
ch_resid_all <- ch %>%
mutate(ever_affected = "Overall")
ch %>%
right_join(dplyr::select(ch_chars, household_id, msoa, ever_affected)) %>%
mutate(ever_affected = ifelse(ever_affected,"Affected","Unaffected")) %>%
bind_rows(ch_resid_all) %>%
mutate(ever_affected = factor(ever_affected,
levels = c("Overall","Affected","Unaffected"))) -> ch_resid_all
ch_resid_all %>%
group_by(ever_affected) %>%
summarise(`No. TPP residents` = n(),
med_age = round(median(age, na.rm = T)),
q_age = paste(round(quantile(age,
probs = c(0.25, 0.75),
na.rm = T)), collapse = ", "),
n_minor = sum(ethnicity != 1, na.rm = T),
prop_minor = mean(ethnicity != 1, na.rm = T),
n_dem = sum(dementia, na.rm = T)
) %>%
mutate(`age med[IQR]` = paste0(med_age, " [",q_age,"]"),
`minority ethnicity n(%)` = paste0(n_minor, " (",round(n_minor/`No. TPP residents`,4),")"),
`dementia n(%)` = paste0(n_dem, " (",round(n_dem/`No. TPP residents`,4),")")) %>%
ungroup() %>%
remove_rownames() %>%
column_to_rownames("ever_affected") %>%
dplyr::select(`No. TPP residents`, `age med[IQR]`, `minority ethnicity n(%)`, `dementia n(%)` ) -> tab_age
ch_resid_all %>%
group_by(ever_affected, ethnicity) %>%
summarise(n_resid = n()) %>%
ungroup() %>%
mutate(ethnicity = replace_na(as.character(ethnicity), "missing")) %>%
pivot_wider(names_from = ethnicity, values_from = n_resid, names_prefix = "Ethn:") %>%
rowwise() %>%
mutate(n_resid = sum(c_across(cols = -ever_affected))) %>%
ungroup() %>%
mutate_at(vars(-n_resid, -ever_affected),
function(x) paste0(x, " (", round(x/.$n_resid,4), ")")) %>%
column_to_rownames("ever_affected") %>%
dplyr::select(-n_resid) -> tab_ethn
tab2 <- cbind(tab_age, tab_ethn)
print("Summarise resident characteristics by ever affected:")
tab2
################################################################################
## FIGURES
################################################################################
pdf(file = "./descriptive.pdf", height = 7, width = 9)
## Age distribution
# png("./age_histogram.png", height = 600, width = 800)
ggplot(input, aes(age)) +
geom_histogram() +
facet_wrap(~ care_home_type, scales = "free_y")
# dev.off()
## Care home survival
# Cumulative care home survival
# png("./ch_survival.png", height = 500, width = 500)
ch_long %>%
group_by(date) %>%
filter(first_event > date) %>%
summarise(n = n_distinct(household_id)) %>%
ggplot(aes(date, n)) +
geom_line() +
labs(title = "Survival of care homes from COVID-19 introduction",
x = "", y = "No. without event")
# dev.off()
ch_long %>%
group_by(date, ch_type) %>%
filter(first_event > date & ch_type != "PS") %>%
summarise(n = n_distinct(household_id)) %>%
ggplot(aes(date, n, col = ch_type)) +
geom_line() +
# facet_wrap(~ch_type, scales = "free_y") +
labs(title = "Survival of care homes from COVID-19 introduction",
x = "", y = "No. without event", col = "Type")
# Type of first event
# png("./ch_first_event_type.png", height = 800, width = 1000)
ch_long %>%
filter(ever_affected) %>%
ggplot(aes(first_event, fill = first_event_which)) +
geom_histogram() +
theme_minimal() +
theme(legend.position = c(0.8,0.8))
# dev.off()
ch_long %>%
group_by(msoa, household_id) %>%
summarise(ever_affected = unique(ever_affected)) %>%
group_by(msoa) %>%
summarise(affect_prop = mean(as.numeric(ever_affected))) -> affect_bymsoa
# png("./ch_first_event_map.png", height = 1000, width = 1000, res = 150)
# msoa_shp %>%
# full_join(affect_bymsoa, by = c("MSOA11CD" = "msoa")) %>%
# ggplot(aes(geometry = geometry, fill = affect_prop)) +
# geom_sf(lwd = 0) +
# labs(title = "Proportion of TPP-covered carehomes ever affected during study period",
# fill = "Proportion") +
# scale_fill_viridis_c() +
# theme(legend.position = c(0.2,0.8))
# dev.off()
# ch_long %>%
# filter(ever_affected == TRUE) %>%
# group_by(msoa, household_id) %>%
# summarise(first_event = unique(first_event)) %>%
# group_by(msoa) %>%
# summarise(average_first_event = median(first_event, na.rm = TRUE),
# first_event = min(first_event)) -> first_bymsoa
# msoa_shp %>%
# full_join(first_bymsoa, by = c("MSOA11CD" = "msoa")) %>%
# ggplot(aes(geometry = geometry, fill = average_first_event)) +
# geom_sf(lwd = 0) +
# labs(title = "Average timing of first care home event per MSOA",
# fill = "Date of first event") +
# scale_fill_viridis_c() +
# theme(legend.position = c(0.2,0.8))
# msoa_shp %>%
# full_join(first_bymsoa, by = c("MSOA11CD" = "msoa")) %>%
# ggplot(aes(geometry = geometry, fill = first_event)) +
# geom_sf(lwd = 0) +
# labs(title = "First care home event per MSOA",
# fill = "Date of first event") +
# scale_fill_viridis_c() +
# theme(legend.position = c(0.2,0.8))
#------------------------------------------------------------------------------#
## Community burden
# Average daily incidence
comm_prev %>%
filter(msoa %in% dat$msoa) %>%
group_by(date) %>%
summarise(probable_cases_rate = mean(probable_cases_rate, na.rm = T)) %>%
ungroup() -> comm_prev_avg
# Community incidence over time
# png("./community_inc.png", height = 500, width = 500)
comm_prev %>%
filter(msoa %in% dat$msoa) %>%
ggplot(aes(date, probable_cases_rate)) +
geom_line(aes(group = msoa), alpha = 0.1) +
geom_line(data = comm_prev_avg, col = "white", lty = "dashed", lwd = 1.5) +
labs(title = "Probable cases per 100,000, by MSOA",
x = "", y = "Rate") +
scale_x_date(limits = study_per)
# dev.off()
#------------------------------------------------------------------------------#
## Community incidence versus care home introduction
# png("./comm_vs_ch.png", height = 800, width = 800)
dat %>%
mutate(event_ahead = as.factor(event_ahead)) %>%
pivot_longer(c("probable_cases_rate","probable_roll7","probable_roll7_lag1wk","probable_roll7_lag2wk")) %>%
ggplot(aes(event_ahead, value)) +
geom_boxplot() +
coord_flip() +
facet_grid(rows = "name", scales = "free") +
labs(title = "Community incidence versus 14-day-ahead introduction",
y = "Daily probable cases in community, per 100,000",
x = "Introduction in next 14 days")
# dev.off()
#------------------------------------------------------------------------------#
## Hospital discharges of care home residents
# png("./discharges.png", height = 500, width = 500)
# dat %>%
# group_by(date) %>%
# summarise(n_disch = sum(n_disch, na.rm = T)) %>%
# ggplot(aes(date, n_disch)) +
# geom_line() +
# labs(title = "Total hospital discharges of care home residents",
# x = "", y = "Count")
# dev.off()
#------------------------------------------------------------------------------#
## Community, care home and older population epidemics
## Currently just absolute numbers as don't have denominator of population in
## community and carehome per MSOA
# png("./compare_epidemics.png", height = 500, width = 500)
input %>%
filter(!is.na(primary_care_case_probable) & primary_care_case_probable > ymd("2020-01-01")) %>%
mutate(group = case_when(care_home_type == "U" & age < 70 ~ "Community",
care_home_type != "U" ~ "Care home",
care_home_type == "U" & age >= 70 ~ "Community, aged 70+")) %>%
group_by(primary_care_case_probable, group) %>%
summarise(n = n()) %>% #, msoa_pop = unique(msoa_pop), pop_gt70 = unique(`70+`)
ggplot(aes(primary_care_case_probable, n, col = group)) +
geom_line() +
labs(title = "Daily probable cases identified through primary care",
col = "Population",
x = "Date",
y = "Count") +
theme(legend.position = c(0.2,0.8))
# dev.off()
dev.off()
################################################################################
time_desc <- Sys.time() - time_desc
write(paste0("Total time running descriptive: ",round(time_desc,2)), file="log_descriptive.txt", append = TRUE)
sink()
################################################################################