generated from opensafely/research-template
/
data_setup.R
316 lines (247 loc) · 11.5 KB
/
data_setup.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
################################################################################
# Description: Set up analysis data of care home daily survival, including care
# home characteristics, covid introduction events, hospital discharges and
# community prevalence of probable infections.
#
# input: Cleaned input data from data_clean.R
# output: Aggregated dataset for landmark analysis of 14-day care home
# infection risk.
#
# Author: Emily S Nightingale
# Date: 06/08/2020
#
################################################################################
time_total <- Sys.time()
################################################################################
#----------------------#
# SETUP ENVIRONMENT #
#----------------------#
library(tidyverse)
library(lubridate)
library(data.table)
library(dtplyr)
library(zoo)
# write("Data setup log",file="data_setup_log.txt")
sink("data_setup_log.txt")
# Function: calculate mode value
getmode <- function(v) {
uniqv <- unique(na.omit(v))
uniqv[which.max(tabulate(match(v, uniqv)))]
}
# ---------------------------------------------------------------------------- #
#----------------------#
# LOAD DATA #
#----------------------#
# * input.csv
# - individual health records for identification of covid events
# * community_prevalence.csv
# - derived dataset of daily probable case counts per MSOA plus population estimates
# args <- c("./input_clean.rds","./data/cases_rolling_nation.csv", 50)
args = commandArgs(trailingOnly = TRUE)
input <- readRDS(args[1])
case_eng <- read.csv(args[2])
ch_cov_cutoff <- as.numeric(args[3])
# Set study period
study_per <- seq(as.Date("2020-03-01"), as.Date("2020-12-07"), by = "days")
# Identify vars containing event dates: probable covid identified via primary care, positive test result, covid-related hospital admission and covid-related death (underlying and mentioned)
event_dates <- c("primary_care_case_probable","first_pos_test_sgss","covid_admission_date", "ons_covid_death_date")
# Time horizon for prediction
ahead <- 14
# ---------------------------------------------------------------------------- #
# Run script to aggregate non-carehome cases by MSOA
source("./analysis/get_community_incidence.R")
print("Summary: Daily community incidence")
summary(comm_inc)
# ---------------------------------------------------------------------------- #
# Check number of individuals in community and aged 65+ in care homes
input %>%
mutate(age_ge65 = (age >= 65)) %>%
group_by(care_home_type, age_ge65) %>%
tally()
# Split out carehome residents
input %>%
filter(care_home_type != "U") -> ch
print("Summary: all care home residents")
summary(ch)
# ---------------------------------------------------------------------------- #
#-----------------------------#
# Care home characteristics #
#-----------------------------#
# Summarise care home resident characteristics
# **will be replaced with CQC vars when codelists available**
# NOTE: multiple events in same HH may have different sizes in dummy data
# same HH mas have both rural/urban and multiple IMD values in dummy data
ch_chars <- ch %>%
group_by(HHID) %>%
summarise(percent_tpp = getmode(percent_tpp),
exclude = (percent_tpp < ch_cov_cutoff),
region = getmode(region),
msoa = getmode(msoa),
n_resid = n(), # number of individuals registered under CHID
ch_size = getmode(household_size), # TPP-derived household size - discrepancies with n_resid and CQC number of beds?
ch_type = getmode(care_home_type), # Care, nursing, other
rural_urban8 = getmode(rural_urban), # Rural/urban location classification - select mode value over all residents
rural_urban8_miss = sum(is.na(rural_urban)),
imd = getmode(imd), # In case missing for some indivs, take mode over HH residents
imd_miss = sum(is.na(rural_urban)),
hh_med_age = median(age), # average age of registered residents
age_miss = sum(is.na(age)),
hh_p_female = mean(sex == "F"), # % registered residents female
hh_maj_ethn = getmode(ethnicity), # majority ethnicity of registered residents (5 categories)
ethn_miss = sum(is.na(ethnicity)),
hh_prop_min = mean(ethnicity != 1, na.rm = T),
hh_p_dem = mean(dementia, na.rm = T)) %>% # % registered residents with dementia - implies whether care home is dementia-specific
ungroup() %>%
mutate(imd_quint = as.factor(cut(imd, 5)),
hh_maj_dem = (hh_p_dem >= 0.5),
rural_urban = as.factor(case_when(rural_urban8 %in% 1:4 ~ "urban",
rural_urban8 %in% 5:8 ~ "rural")))
print("Summary: Care home characteristics")
summary(ch_chars)
print("No. unique homes:")
nrow(ch_chars)
n_distinct(ch_chars$HHID)
# ---------------------------------------------------------------------------- #
# Exclude care homes on TPP coverage
print("% TPP coverage - by HHID:")
summary(
ch %>%
group_by(HHID) %>%
summarise(percent_tpp = getmode(percent_tpp)) %>%
ungroup() %>%
mutate(percent_tpp_cat = cut(percent_tpp,
breaks = 10,
include.lowest = TRUE)) %>%
pull(percent_tpp_cat)
)
ch_bycovg <- split(ch_chars, ch_chars$exclude) %>%
lapply(FUN = function(x) unique(pull(x, HHID)))
excl <- ch_bycovg[["TRUE"]]
incl <- ch_bycovg[["FALSE"]]
print(paste0("Care homes excluded with ",ch_cov_cutoff,"% coverage cut off: n = ",length(excl)))
print(paste0("Care homes included with ",ch_cov_cutoff,"% coverage cut off: n = ",length(incl)))
# Keep only homes with sufficient coverage
ch_chars <- ch_chars %>%
filter(HHID %in% incl)
# Also keep only residents in homes with sufficient coverage
ch <- ch %>%
filter(HHID %in% incl)
# ---------------------------------------------------------------------------- #
#-----------------------------#
# Care home first event #
#-----------------------------#
# Identify first covid event in care home, out of all possible events of interest.
# Exclude care homes with first event prior to 2020-04-15
# Care homes which don't have any event in period are assigned the date "3000-01-01"
ch_first_event <- ch %>%
mutate_at(vars(all_of(event_dates)), function(x) replace_na(ymd(x),ymd("3000-01-01"))) %>%
group_by(HHID) %>%
summarise_at(vars(all_of(event_dates)),min) %>%
ungroup() %>%
rename_at(-1, function(x) paste0("first_",x)) %>%
rowwise() %>%
mutate(first_event = ymd(replace_na(min(c_across(starts_with("first_"))),"3000-01-01")),
first_event_which = as.factor(event_dates[which.min(c_across(starts_with("first_")))])) %>%
group_by(HHID) %>%
mutate(first_event_pre_per = (first_event < ymd("2020-04-15")),
first_event_post_per = (first_event > ymd("2020-12-07") & first_event < ymd("3000-01-01")),
ever_affected = between(first_event, ymd("2020-04-15"), ymd("2020-12-07")))
ch_first_event$first_event_which[!ch_first_event$ever_affected] <- NA
print("Care homes with first event prior to study period (excluded from analysis):")
ch_first_event %>%
group_by(first_event_pre_per) %>%
tally()
print("Care homes with first event posterior to study period (excluded from analysis):")
ch_first_event %>%
group_by(first_event_post_per) %>%
tally()
# Join care home characteristics with first event dates
ch_wevent <- ch_chars %>%
full_join(ch_first_event) %>%
# Exclude care homes with first event prior to study period
filter(!first_event_pre_per) %>%
mutate(date = first_event) %>%
select(-first_primary_care_case_probable:-first_ons_covid_death_date)
print("Summary: First events")
summary(ch_wevent)
print("Care homes affected during study period:")
ch_wevent %>%
group_by(ever_affected) %>%
tally()
print("Summary: Characteristics of care homes affected during study period")
ch_wevent %>%
filter(ever_affected) %>%
summary()
# ---------------------------------------------------------------------------- #
#-----------------------------#
# Expand rows #
#-----------------------------#
# Expand rows in data.table for speed:
start <- Sys.time()
vars <- names(select(ch_wevent, exclude, HHID:rural_urban, first_event:first_event_which, ever_affected))
ch_wevent <- as.data.table(ch_wevent)
# Replicate per region (by vars are all values I want to copy down per date):
all_dates <- ch_wevent[,.(date = study_per),by = vars]
# Merge and fill count with 0:
setkey(ch_wevent, exclude, HHID, msoa, region, n_resid, ch_size, ch_type,
rural_urban8, rural_urban, imd, imd_quint, hh_med_age, hh_p_female, hh_prop_min,
hh_p_dem, hh_maj_dem, first_event, ever_affected, first_event_which, date)
setkey(all_dates, exclude, HHID, msoa, region, n_resid, ch_size, ch_type,
rural_urban8, rural_urban, imd, imd_quint, hh_med_age, hh_p_female, hh_prop_min,
hh_p_dem, hh_maj_dem, first_event, ever_affected, first_event_which, date)
ch_wevent <- ch_wevent[all_dates,roll = TRUE]
# Finished expanding carehome dates: time =
round(Sys.time() - start,2)
# ---------------------------------------------------------------------------- #
#-----------------------------#
# Setup analysis dataset #
#-----------------------------#
# Join with community prevalence data and define
# 7-day rolling mean/difference + lags.
# For each date, define event_ahead = 1 if that care home's first event occurs
# in next <ahead> days
ch_long <- comm_inc %>%
right_join(ch_wevent, by = c("msoa","date")) %>% #View()
group_by(HHID) %>%
mutate(day = 1:n(),
wave = factor(date >= ymd("2020-08-01"), labels = c("first","second")),
event_ahead = replace_na(as.numeric(
first_event %within% interval(date,date + ahead)
),0)) %>%
ungroup()
print("Homes in ch_long data:")
ch_long %>%
group_by(exclude, ever_affected) %>%
summarise(N = n_distinct(HHID))
#To create the dataset for landmarking analysis, need to define a subset for
#each day of carehomes which have not yet had an event and then bind all subsets
#together.
make_data_t <- function(t, ahead = 14){
# filter to day t and drop CHs who had event before t
data <- filter(ch_long, day == t & !(first_event <= study_per[t]))
return(data)
}
# apply function for each date in range and bind
dat <- bind_rows(lapply(1:length(study_per), make_data_t))
print("No. homes in full analysis data:")
dat %>%
group_by(exclude) %>%
summarise(N = n_distinct(HHID))
# ---------------------------------------------------------------------------- #
# Check distribution of community incidence measures against events
print("Summary: community incidence by occurrence of a care home event:")
dat %>%
pivot_longer(c("msoa_probable_rate","msoa_roll7","msoa_lag1wk","msoa_lag2wk","eng_roll7","eng_lag1wk","eng_lag2wk")) %>%
group_by(event_ahead, name) %>%
summarise(min = min(value, na.rm = T), max = max(value, na.rm = T), mean = mean(value, na.rm = T), sd = sqrt(var(value, na.rm = T)), med = median(value, na.rm = T))
# ---------------------------------------------------------------------------- #
# Save analysis data
saveRDS(comm_inc, "./community_incidence.rds")
saveRDS(ch, file = "./ch_linelist.rds")
saveRDS(ch_long, file = "./ch_agg_long.rds")
saveRDS(dat, file = "./analysisdata.rds")
# ---------------------------------------------------------------------------- #
# Total time running data_setup:
round(Sys.time() - time_total,2)
sink()
################################################################################