generated from opensafely/covid-vaccine-research-template
/
data_process_control.R
212 lines (156 loc) · 6.29 KB
/
data_process_control.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
######################################
# This script:
# imports data extracted by the cohort extractor (or dummy data)
# fills in unknown ethnicity from GP records with ethnicity from SUS (secondary care)
# tidies missing values
# standardises some variables (eg convert to factor) and derives some new ones
# organises vaccination date data to "vax X type", "vax X date" (rather than "pfizer X date", "az X date", ...)
######################################
# Preliminaries ----
## Import libraries ----
library('tidyverse')
library('lubridate')
library('arrow')
library('here')
library('glue')
## import command-line arguments ----
args <- commandArgs(trailingOnly=TRUE)
if(length(args)==0){
# use for interactive testing
removeobjects <- FALSE
agegroup <- "over12"
matching_round <- as.integer("1")
} else {
#FIXME replace with actual eventual action variables
removeobjects <- TRUE
agegroup <- args[[1]]
matching_round <- as.integer(args[[2]])
}
source(here("lib", "functions", "utility.R"))
## Import design elements
source(here("analysis", "design.R"))
# import globally defined study dates and convert to "Date"
study_dates <-
jsonlite::read_json(path=here("lib", "design", "study-dates.json")) %>%
map(as.Date)
start_date <- study_dates[[glue("{agegroup}start_date")]]
matching_round_date <- study_dates[[glue("{agegroup}start_date")]] + (matching_round-1)*14
# define vaccination of interest
if(agegroup=="under12") {
treatment <- "pfizerC"
}
if(agegroup=="over12") {
treatment <- "pfizerA"
}
fs::dir_create(here("output", "data"))
# process ----
# use externally created dummy data if not running in the server
# check variables are as they should be
if(Sys.getenv("OPENSAFELY_BACKEND") %in% c("", "expectations")){
# ideally in future this will check column existence and types from metadata,
# rather than from a cohort-extractor-generated dummy data
data_studydef_dummy <- read_feather(here("output", glue("input_control_potential{matching_round}.feather"))) %>%
# because date types are not returned consistently by cohort extractor
mutate(across(ends_with("_date"), ~ as.Date(.))) %>%
# because of a bug in cohort extractor -- remove once pulled new version
mutate(patient_id = as.integer(patient_id))
data_custom_dummy <- read_feather(here("lib", "dummydata", "dummy_control_potential1.feather")) %>%
mutate(
msoa = sample(factor(c("1", "2")), size=n(), replace=TRUE) # override msoa so matching success more likely
) %>%
select(
-covid_vax_pfizerA_1_date, -covid_vax_pfizerA_2_date, -covid_vax_pfizerC_1_date, -covid_vax_pfizerC_2_date, -covid_vax_any_2_date
)
not_in_studydef <- names(data_custom_dummy)[!( names(data_custom_dummy) %in% names(data_studydef_dummy) )]
not_in_custom <- names(data_studydef_dummy)[!( names(data_studydef_dummy) %in% names(data_custom_dummy) )]
if(length(not_in_custom)!=0) stop(
paste(
"These variables are in studydef but not in custom: ",
paste(not_in_custom, collapse=", ")
)
)
if(length(not_in_studydef)!=0) stop(
paste(
"These variables are in custom but not in studydef: ",
paste(not_in_studydef, collapse=", ")
)
)
# reorder columns
data_studydef_dummy <- data_studydef_dummy[,names(data_custom_dummy)]
unmatched_types <- cbind(
map_chr(data_studydef_dummy, ~paste(class(.), collapse=", ")),
map_chr(data_custom_dummy, ~paste(class(.), collapse=", "))
)[ (map_chr(data_studydef_dummy, ~paste(class(.), collapse=", ")) != map_chr(data_custom_dummy, ~paste(class(.), collapse=", ")) ), ] %>%
as.data.frame() %>% rownames_to_column()
if(nrow(unmatched_types)>0) stop(
#unmatched_types
"inconsistent typing in studydef : dummy dataset\n",
apply(unmatched_types, 1, function(row) paste(paste(row, collapse=" : "), "\n"))
)
data_extract <- data_custom_dummy
} else {
data_extract <- read_feather(here("output", glue("input_control_potential{matching_round}.feather"))) %>%
#because date types are not returned consistently by cohort extractor
mutate(across(ends_with("_date"), as.Date))
}
data_processed <- data_extract %>%
mutate(
sex = fct_case_when(
sex == "F" ~ "Female",
sex == "M" ~ "Male",
#sex == "I" ~ "Inter-sex",
#sex == "U" ~ "Unknown",
TRUE ~ NA_character_
),
# ethnicity_combined = if_else(is.na(ethnicity), ethnicity_6_sus, ethnicity),
#
# ethnicity_combined = fct_case_when(
# ethnicity_combined == "1" ~ "White",
# ethnicity_combined == "4" ~ "Black",
# ethnicity_combined == "3" ~ "South Asian",
# ethnicity_combined == "2" ~ "Mixed",
# ethnicity_combined == "5" ~ "Other",
# #TRUE ~ "Unknown",
# TRUE ~ NA_character_
#
# ),
region = fct_collapse(
region,
`East of England` = "East",
`London` = "London",
`Midlands` = c("West Midlands", "East Midlands"),
`North East and Yorkshire` = c("Yorkshire and The Humber", "North East"),
`North West` = "North West",
`South East` = "South East",
`South West` = "South West"
),
prior_covid_infection = (!is.na(postest_0_date)) | (!is.na(covidadmitted_0_date)) | (!is.na(primary_care_covid_case_0_date)),
# latest covid event before study start
anycovid_0_date = pmax(postest_0_date, covidemergency_0_date, covidadmitted_0_date, na.rm=TRUE),
vax1_date = covid_vax_any_1_date,
)
## select eligible patients and create flowchart ----
# Define selection criteria ----
data_criteria <- data_processed %>%
transmute(
patient_id,
has_age = !is.na(age),
has_sex = !is.na(sex) & !(sex %in% c("I", "U")),
has_imd = imd_Q5 != "Unknown",
vaccinated = vax1_date<matching_round_date,
#has_ethnicity = !is.na(ethnicity_combined),
has_region = !is.na(region),
no_recentcovid90 = is.na(anycovid_0_date) | ((matching_round_date - anycovid_0_date)>90),
include = (
has_age & has_sex & has_imd & # has_ethnicity &
has_region &
no_recentcovid90 &
!vaccinated
),
)
data_control_potential <- data_criteria %>%
filter(include) %>%
select(patient_id) %>%
left_join(data_processed, by="patient_id") %>%
droplevels()
write_rds(data_control_potential, here("output", "data", glue("data_control_potential{matching_round}.rds")), compress="gz")