generated from opensafely/research-template
/
dummy_data_vax.R
178 lines (165 loc) · 6.59 KB
/
dummy_data_vax.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
######################################
# This script:
# - creates dummy data for study_definition.py
######################################
library(tidyverse)
library(lubridate)
library(glue)
source(here::here("analysis", "functions", "dummy_data_functions.R"))
set.seed(5476)
# date vars
# set these to have occured since start of pandemic
date_vars_recent <- c("death_date",
"longres_date",
"endoflife_date",
"midazolam_date",
"coviddeath_date",
"dereg_date")
jcvi_group_patterns <- readr::read_csv(here::here("analysis", "lib", "jcvi_groups.csv")) %>%
mutate(across(definition, ~str_extract(.x, "age_. >=\\d{2}"))) %>%
# add dummy conditions for groups 1 and 6, as longres and atrisk data not available here (done correctly in real data)
mutate(across(definition, ~case_when(group=="01" ~ "age_1 >=85",
group=="04b" ~ "age_1 >=68",
group=="06" ~ "age_1 >=62",
!is.na(.x) ~ .x,
TRUE ~ "TRUE")))
# conditions for eligibility dates
elig_date_patterns <- readr::read_csv(here::here("analysis", "lib", "elig_dates.csv")) %>%
mutate(across(description, ~str_replace_all(.x, "p=", "p=="))) %>%
mutate(across(description, ~str_replace_all(.x, "OR", "|"))) %>%
mutate(across(description, ~str_replace_all(.x, "AND", "&"))) %>%
mutate(across(description, ~str_replace_all(.x, "DEFAULT", "TRUE")))
dummy_data_elig <- tibble(patient_id = 1:n) %>%
mutate(age_1 = as.integer(runif(nrow(.), 16, 90), 0),
age_2 = age_1,
imd = sample(
x = seq(100L,32100L,100L),
size = n,
replace = TRUE)) %>%
var_category(sex, categories = c("F", "M")) %>%
var_category(ethnicity_6,
categories = c(as.character(1:5), NA_character_),
ratios = c(rep(0.99/5,5), 0.01)) %>%
var_category(ethnicity_6_sus,
categories = c(as.character(1:5), NA_character_),
ratios = c(rep(0.99/5,5), 0.01)) %>%
var_category(region, categories = regions$region, ratios = regions$ratio) %>%
# binary vars for exclusion criteria
mutate(hscworker = rbernoulli(n = nrow(.), p=0.01)) %>%
mutate(housebound = rbernoulli(n = nrow(.), p=0.01)) %>%
# jcvi_group
var_category(
name = jcvi_group,
categories = jcvi_group_patterns$group,
conditions = jcvi_group_patterns$definition
) %>%
# elig_date
var_category(
name = elig_date,
categories = elig_date_patterns$date,
conditions = elig_date_patterns$description)
# fix vaccine dates so that they have roughly correct distribution
dummy_data_vax <- dummy_data_elig %>%
mutate(
covid_vax_pfizer_1_date = as.Date(elig_date) + days(round(rnorm(nrow(.), mean = 10, sd = 3))),
covid_vax_az_1_date = as.Date(elig_date) + days(round(rnorm(nrow(.), mean = 10, sd = 3))),
covid_vax_moderna_1_date = as.Date(elig_date) + days(round(rnorm(nrow(.), mean = 10, sd = 3)))) %>%
mutate(
vaccine_1_type = sample(
x = c("pfizer", "az", "moderna", "none"),
size = nrow(.),
replace = TRUE,
prob = c(0.4, 0.4, 0.1, 0.1)
),
missing_pfizer_2 = rbernoulli(nrow(.), p=0.05),
missing_az_2 = rbernoulli(nrow(.), p=0.05),
missing_moderna_2 = rbernoulli(nrow(.), p=0.05),
missing_pfizer_3 = rbernoulli(nrow(.), p=0.9),
missing_az_3 = rbernoulli(nrow(.), p=0.9),
missing_moderna_3 = rbernoulli(nrow(.), p=0.9)
) %>%
mutate(across(covid_vax_pfizer_1_date,
~if_else(
vaccine_1_type %in% "pfizer",
.x,
NA_Date_))) %>%
mutate(across(covid_vax_az_1_date,
~if_else(
vaccine_1_type %in% "az",
.x,
NA_Date_))) %>%
mutate(across(covid_vax_moderna_1_date,
~if_else(
vaccine_1_type %in% "moderna",
.x,
NA_Date_))) %>%
mutate(across(matches("covid_vax_\\w+_1_date"),
~ if_else(
vaccine_1_type %in% "none",
NA_Date_,
.x
))) %>%
mutate(
covid_vax_pfizer_2_date = covid_vax_pfizer_1_date + days(round(rnorm(nrow(.), mean = 10*7, sd = 3))),
covid_vax_az_2_date = covid_vax_az_1_date + days(round(rnorm(nrow(.), mean = 10*7, sd = 3))),
covid_vax_moderna_2_date = covid_vax_moderna_1_date + days(round(rnorm(nrow(.), mean = 10*7, sd = 3))),
) %>%
mutate(across(covid_vax_pfizer_2_date,
~if_else(
missing_pfizer_2,
NA_Date_,
.x))) %>%
mutate(across(covid_vax_az_2_date,
~if_else(
missing_az_2,
NA_Date_,
.x))) %>%
mutate(across(covid_vax_moderna_2_date,
~if_else(
missing_moderna_2,
NA_Date_,
.x))) %>%
mutate(
covid_vax_pfizer_3_date = covid_vax_pfizer_2_date + days(round(rnorm(nrow(.), mean = 6*4*7, sd = 7))),
covid_vax_az_3_date = covid_vax_az_2_date + days(round(rnorm(nrow(.), mean = 6*4*7, sd = 7))),
covid_vax_moderna_3_date = covid_vax_moderna_2_date + days(round(rnorm(nrow(.), mean = 6*4*7, sd = 7))),
) %>%
select(-starts_with("missing"), -vaccine_1_type)
### from dummy_data_covs
dummy_data_covs <- dummy_data_vax %>%
# date vars recent
bind_cols(
pmap(
list(a = date_vars_recent,
b = rep(0.2, length(date_vars_recent))),
function(a,b)
var_date(
.data = .,
name = !! a,
incidence = b,
earliest="2020-11-01",
latest="2021-12-31",
keep_vars = FALSE
))) %>%
# add event counts
mutate(
postest_n = rpois(nrow(.),3),
covidadmitted_n = rpois(nrow(.),1),
covid_primary_care_positive_test_n = rpois(nrow(.),1),
covid_primary_care_code_n = rpois(nrow(.),1),
covid_primary_care_sequalae_n = rpois(nrow(.),1)
) %>%
# add death_date if coviddeath_date
mutate(across(death_date,
~if_else(
!is.na(coviddeath_date),
coviddeath_date,
.x))) %>%
mutate(across(contains("_date"), as.POSIXct)) %>%
mutate(across(ends_with("date"), as.POSIXct)) %>%
mutate(across(c(ethnicity_6, ethnicity_6_sus, jcvi_group, region, sex),
as.factor)) %>%
droplevels()
# final dummy data
dummy_data <- dummy_data_covs
arrow::write_feather(dummy_data, here::here("analysis", "dummy_data_vax.feather"))