generated from opensafely/research-template
/
data_clean.R
240 lines (191 loc) · 7.5 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
################################################################################
# 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
# * community_prevalence.csv
# - derived dataset of daily probable case counts per MSOA plus population estimates
# 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 with non-missing household ID
filter(grepl("E",msoa) & !is.na(msoa) & household_id > 0 & !is.na(household_id)) %>%
# Join with MSOA coverage data
left_join(tpp_cov, by = "msoa") %>%
rowwise() %>%
# Identify individuals with any covid event
mutate(case = any(!is.na(c_across(all_of(event_dates))))) %>%
ungroup() %>%
# Set up var formats
mutate(# Define unique household identifier
HHID = paste(msoa, household_id, sep = ":"),
# 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),
across(all_of(dates), ymd),
across(where(is.character), as.factor),
# Identify potential prisons/institutions
institution = (care_home_type == "U" & household_size > 20),
# Define delay vars
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)))
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)
# ---------------------------------------------------------------------------- #
# Drop rows with missing MSOA or care home type
input <- input %>%
filter(!is.na(msoa) & !is.na(care_home_type))
print("Summary: Final")
summary(input)
# Save cleaned input data
saveRDS(input, "./input_clean.rds")
write_csv(input, "./input_clean.csv")
# ---------------------------------------------------------------------------- #
#----------------------#
# CHECK COUNTS #
#----------------------#
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()
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))
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))
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)
)
print("Care home residents test-diagnosis delay")
summary(
input %>%
filter(care_home_type != "U") %>%
dplyr::select(prob_death_delay, test_death_delay)
)
# ---------------------------------------------------------------------------- #
sink()
################################################################################
################################################################################