generated from opensafely/research-template
/
data_check.R
175 lines (140 loc) · 4.67 KB
/
data_check.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
################################################################################
# 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
#
################################################################################
################################################################################
library(tidyverse)
library(data.table)
library(dtplyr)
sink("./data_checks.txt")
# ---------------------------------------------------------------------------- #
#----------------------#
# 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
# Identify vars containing event dates
event_dates <- c("primary_care_case_probable","first_pos_test_sgss","covid_admission_date", "ons_covid_death_date")
# args = commandArgs(trailingOnly=TRUE)
# args <- c("./output/input.csv","tpp_msoa_coverage.rds")
args = commandArgs(trailingOnly=TRUE)
## Load shapefiles
# msoa_shp <- readRDS(args[2])
tpp_cov <- readRDS(args[2])
input <- fread(args[1], data.table = FALSE, na.strings = "") %>%
left_join(tpp_cov, by = "msoa") %>%
mutate(across(where(is.character), as.factor)) %>%
rowwise() %>%
mutate(case = any(!is.na(c_across(event_dates))))
# ---------------------------------------------------------------------------- #
hh_miss_msoa <- input %>%
filter(is.na(msoa)) %>%
pull(household_id) %>%
n_distinct()
pat_miss_msoa <- input %>%
filter(is.na(msoa)) %>%
nrow()
hh_miss_type <- input %>%
filter(is.na(care_home_type)) %>%
pull(household_id) %>%
n_distinct()
pat_miss_type <- input %>%
filter(is.na(care_home_type)) %>%
nrow()
pat_cov_miss <- input %>%
filter((is.na(msoa) | is.na(care_home_type))) %>%
rowwise() %>%
filter(any(!is.na(c_across(event_dates)))) %>%
nrow()
print("HHs with missing MSOA: n = ")
hh_miss_msoa
print("Patients with missing HH MSOA: n = ")
pat_miss_msoa
print("HHs with missing type: n = ")
hh_miss_type
print("Patients with missing HH type: n = ")
pat_miss_type
print("COVID cases with missing MSOA or HH type: n = ")
pat_cov_miss
print("No. households, patients and probable cases per carehome type:")
input %>%
group_by(care_home_type) %>%
summarise(n_hh = n_distinct(household_id),
n_pat = n(),
n_case = sum(case, na.rm = TRUE))
print("Probable prisons/institutions (size>15 and not CH)")
input %>%
mutate(institution = (care_home_type == "U" & household_size > 15)) %>%
group_by(institution) %>%
summarise(n_hh = n_distinct(household_id),
n_pat = n(),
n_case = sum(case, na.rm = TRUE))
# TPP coverage by MSOA
png("./tpp_coverage_msoa.png", height = 800, width = 800)
input %>%
group_by(msoa) %>%
summarise(tpp_cov = unique(tpp_cov)) %>%
ggplot(aes(tpp_cov)) +
geom_histogram(bins = 30, fill = "steelblue") +
theme_minimal()
dev.off()
# png("./tpp_coverage_map.png", height = 800, width = 800)
# input %>%
# group_by(msoa) %>%
# summarise(tpp_cov = unique(tpp_cov)) %>%
# full_join(msoa_shp, by = c("msoa" = "MSOA11CD")) %>%
# ggplot(aes(geometry = geometry, fill = tpp_cov)) +
# geom_sf(lwd = 0) +
# scale_fill_gradient2() +
# theme_minimal()
# dev.off()
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(household_id),
n_pat = n(),
n_case = sum(case, na.rm = TRUE))
print("Care homes % TPP coverage:")
summary(
input %>%
filter(care_home_type != "U") %>%
dplyr::select(household_id, percent_tpp) %>%
unique() %>%
pull(percent_tpp)
)
print("Care homes % TPP coverage category:")
summary(
input %>%
filter(care_home_type != "U") %>%
dplyr::select(household_id, percent_tpp) %>%
unique() %>%
mutate(percent_tpp_cat = cut(percent_tpp, 5)) %>%
pull(percent_tpp_cat)
)
input %>%
filter(care_home_type != "U") %>%
mutate(percent_tpp_cat = cut(percent_tpp, 5)) %>%
group_by(percent_tpp_cat) %>%
summarise(n_hh = n_distinct(household_id),
n_pat = n(),
n_case = sum(case, na.rm = TRUE))
png("./tpp_coverage_carehomes.png", height = 800, width = 800)
input %>%
filter(care_home_type != "U") %>%
dplyr::select(household_id, percent_tpp) %>%
unique() %>%
ggplot(aes(percent_tpp)) +
geom_histogram(bins = 30, fill = "steelblue") +
theme_minimal()
dev.off()
sink()