generated from opensafely/research-template
/
data_clean.R
280 lines (224 loc) · 9.02 KB
/
data_clean.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
################################################################################
# Description: Script to produce initial checks and summaries of raw input data
#
# input: individual patient GP record data extracted from OpenSAFELY according
# to "./analyis/study_definition.py".
#
# Author: Emily S Nightingale
# Date: 01/10/2020
#
################################################################################
################################################################################
#----------------------#
# SETUP ENVIRONMENT #
#----------------------#
library(tidyverse)
library(data.table)
library(dtplyr)
library(lubridate)
sink("./data_clean_log.txt", type = "output")
options(datatable.old.fread.datetime.character = TRUE)
# Replace dates outside specified range with NAs (default outside 2020)
na_replace_dates <- function(x, min = '2020-01-01', max = '2020-12-31') {
x[x < min] <- NA
x[x > max] <- NA
return(ymd(x))
}
# ---------------------------------------------------------------------------- #
#----------------------#
# LOAD DATA #
#----------------------#
# * input.csv
# - individual health records for identification of covid events
# * tpp_coverage_included.rds
# - Estimated coverage of TPP per MSOA, including only MSOAs with coverage >=80%
# args <- c("input.csv","tpp_coverage_included.rds", 2)
args = commandArgs(trailingOnly = TRUE)
input_raw <- fread(args[1], data.table = FALSE, na.strings = "") %>%
# check for mixed HH/perc TPP agreement
mutate(perc_tpp_lt100 = (percent_tpp < 100))
# Load MSOA TPP coverage
tpp_cov <- readRDS(args[2])
# MSOA TPP coverage cut off
msoa_cov_cutoff <- 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")
dates <- c(event_dates,"discharge_date")
# ---------------------------------------------------------------------------- #
#----------------------#
# CLEANING #
#----------------------#
print("Summary: Raw input")
summary(input_raw)
input <- input_raw %>%
# Filter just to records from England
filter(grepl("E",msoa)) %>%
# Join with MSOA coverage data
left_join(tpp_cov, by = "msoa") %>%
# Set up variables of interest
mutate(HHID = paste(msoa, household_id, sep = ":"), # Redefine unique household identifier
# Redefine -1/0 values as NA
across(c(age, ethnicity, imd, rural_urban), function(x) na_if(x,-1)),
across(c(imd, household_size), function(x) na_if(x,0)),
# Variable formatting
dementia = replace_na(dementia,0),
ethnicity = as.factor(ethnicity),
rural_urban = as.factor(rural_urban),
# Date formats
across(all_of(dates), ymd),
# Set all character variables as factor
across(where(is.character), as.factor),
# Identify potential prisons/institutions - still needed?
institution = (care_home_type == "U" & household_size > 20),
# Define delays
test_death_delay = as.integer(ons_covid_death_date - first_pos_test_sgss),
prob_death_delay = as.integer(ons_covid_death_date - first_pos_test_sgss),
# Replace event dates pre 2020 and post end of study as NA
across(all_of(event_dates), na_replace_dates, max = max(study_per))) %>%
# Identify individuals with any covid event
rowwise() %>%
mutate(case = any(!is.na(c_across(all_of(event_dates))))) %>%
ungroup()
print("Summary: Cleaned")
summary(input)
# ---------------------------------------------------------------------------- #
# Filter MSOAs by TPP coverage (missing value when merged with included MSOAs in tpp_cov)
exclude <- input %>%
filter(is.na(tpp_cov_wHHID))
print(paste0("Individuals excluded with MSOA ",msoa_cov_cutoff,"% coverage cut off: n = ",nrow(exclude)))
print(paste0("MSOAs excluded with MSOA ",msoa_cov_cutoff,"% coverage cut off: n = ",n_distinct(exclude$msoa)))
input <- input %>%
filter(!is.na(tpp_cov_wHHID))
# Should now have no records with coverage < cutoff
summary(input$tpp_cov_wHHID)
# Double check
nrow(filter(input, tpp_cov_wHHID < msoa_cov_cutoff))
input <- input %>%
filter(tpp_cov_wHHID >= msoa_cov_cutoff)
# ---------------------------------------------------------------------------- #
# Check missingness in location/type
print("Total Patients")
n_distinct(input$patient_id)
print("Patients with missing HH MSOA:")
summary(is.na(input$msoa))
print("Patients with missing HH type:")
summary(is.na(input$care_home_type))
print("HHs with missing MSOA: n = ")
input %>%
filter(is.na(msoa)) %>%
pull(HHID) %>%
n_distinct()
print("HHs with missing type: n = ")
input %>%
filter(is.na(care_home_type)) %>%
pull(HHID) %>%
n_distinct()
print("COVID cases with missing MSOA or HH type: n = ")
input %>%
filter(is.na(msoa) | is.na(care_home_type)) %>%
rowwise() %>%
filter(any(!is.na(c_across(all_of(event_dates))))) %>%
pull(patient_id) %>%
n_distinct()
# ---------------------------------------------------------------------------- #
# Drop rows with missing MSOA, household ID or care home type
input <- input %>%
filter(!is.na(msoa) & household_id > 0 & !is.na(household_id) & !is.na(care_home_type))
# ---------------------------------------------------------------------------- #
# Check uniqueness of household ID
print("No. households, by household_id alone and by household_ID + MSOA")
input %>%
summarise(N_hhID = n_distinct(household_id),
N_msoa_hhID = n_distinct(HHID))
print("Uniqueness of household characteristics over residents:")
input %>%
group_by(household_id) %>%
summarise(msoa = n_distinct(msoa),
region = n_distinct(region),
household_size = n_distinct(household_size),
imd = n_distinct(imd),
rural_urban = n_distinct(rural_urban)) -> n_distinct_chars
summary(n_distinct_chars)
# ---------------------------------------------------------------------------- #
# Counts of household, individuals and cases
# By household type
print("No. households, patients and probable cases per carehome type:")
input %>%
group_by(care_home_type) %>%
summarise(n_hh = n_distinct(HHID),
n_pat = n_distinct(patient_id),
n_case = sum(case, na.rm = TRUE))
# By institution
print("Probable prisons/institutions (size>20 and not CH)")
input %>%
mutate(institution = (care_home_type == "U" & household_size > 20)) %>%
group_by(institution) %>%
summarise(n_hh = n_distinct(HHID),
n_pat = n_distinct(patient_id),
n_case = sum(case, na.rm = TRUE))
# By TPP coverage
print("Care homes registered under > 1 system:")
input %>%
filter(care_home_type != "U") %>%
mutate(mixed_household = replace_na(mixed_household, 0)) %>%
group_by(mixed_household) %>%
summarise(n_hh = n_distinct(HHID),
n_pat = n_distinct(patient_id),
n_case = sum(case, na.rm = TRUE))
print("Care homes with < 100% coverage:")
input %>%
filter(care_home_type != "U") %>%
group_by(percent_tpp < 100) %>%
summarise(n_hh = n_distinct(HHID),
n_pat = n_distinct(patient_id),
n_case = sum(case, na.rm = TRUE))
print("Care homes % TPP coverage:")
summary(
input %>%
filter(care_home_type != "U") %>%
dplyr::select(HHID, percent_tpp) %>%
unique() %>%
pull(percent_tpp)
)
print("Care homes % TPP coverage category:")
summary(
input %>%
filter(care_home_type != "U") %>%
dplyr::select(HHID, percent_tpp) %>%
unique() %>%
mutate(percent_tpp_cat = cut(percent_tpp,
breaks = 10,
include.lowest = TRUE)) %>%
pull(percent_tpp_cat)
)
# ---------------------------------------------------------------------------- #
# Check household sizes
print("Household size by care home type:")
input %>%
filter(!is.na(household_size)) %>%
group_by(care_home_type) %>%
summarise(mean = mean(household_size),
sd = sd(household_size),
median = median(household_size),
minmax = paste(min(household_size), max(household_size), sep = ", "))
print("Number of records by care home type:")
input %>%
group_by(care_home_type, HHID) %>%
summarise(n_resid = n()) %>%
group_by(care_home_type) %>%
summarise(mean = mean(n_resid),
sd = sd(n_resid),
median = median(n_resid),
minmax = paste(min(n_resid), max(n_resid), sep = ", "))
# ---------------------------------------------------------------------------- #
print("Summary: Final")
summary(input)
# Save cleaned input data
saveRDS(input, "./input_clean.rds")
write_csv(input, "./input_clean.csv")
# ---------------------------------------------------------------------------- #
sink()
################################################################################
################################################################################