generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_cumulative_incidence.R
182 lines (158 loc) · 5.93 KB
/
plot_cumulative_incidence.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
library(tidyverse)
library(glue)
library(survival)
library(lubridate)
################################################################################
# create output directories
fs::dir_create(here::here("output", "subsequent_vax", "images"))
fs::dir_create(here::here("output", "subsequent_vax", "tables"))
################################################################################
## import study_parameters
study_parameters <- readr::read_rds(
here::here("analysis", "lib", "study_parameters.rds"))
# read subgroups
subgroups <- readr::read_rds(
here::here("analysis", "lib", "subgroups.rds"))
subgroup_labels <- seq_along(subgroups)
subgroups_long <- if_else(
subgroups %in% subgroups[c(2,3)],
as.character(glue("{subgroups}*")),
subgroups
)
subgroups_long_wrap <- str_wrap(subgroups_long, width = 25)
################################################################################
# read data
data_all <- readr::read_rds(
here::here("output", "data", "data_all.rds")) %>%
select(patient_id, subgroup, arm, start_1_date, end_6_date, subsequent_vax_date, dereg_date, death_date)
################################################################################
# redaction function for KM curves
source(here::here("analysis", "functions", "round_km.R"))
################################################################################
# if running locally read extracted data:
# if(Sys.getenv("OPENSAFELY_BACKEND") %in% "") {
#
# release_folder <- "release20220226"
# image_path <- here::here(release_folder)
#
# survtable_redacted <- readr::read_csv(
# here::here(release_folder, "survtable_redacted.csv")) %>%
# mutate(across(subgroup,
# ~ case_when(
# str_detect(.x, "65") ~ 1,
# str_detect(.x, "16-64") ~ 2,
# str_detect(.x, "40-64") ~ 3,
# str_detect(.x, "18-39") ~ 4,
# TRUE ~ NA_real_
# ))) %>%
# mutate(across(subgroup,
# factor,
# levels = subgroup_labels,
# labels = subgroups_long_wrap))
#
# } else { # else derive the data
# function to be applied in dplyr::filter
no_evidence_of <- function(cov_date, index_date) {
is.na(cov_date) | index_date < cov_date
}
data_tte <- data_all %>%
mutate(across(subgroup,
factor,
levels = subgroups,
labels = subgroups_long_wrap)) %>%
mutate(
# start date of comparison 1
start_fu_date = start_1_date,
# end date of final comparison or end of data availability
end_fu_date = pmin(end_6_date, study_parameters$end_date)
) %>%
select(-start_1_date, -end_6_date) %>%
# remove if subsequent vaccine, death or dereg on or before start_of_period
filter_at(
all_of(c("subsequent_vax_date", "death_date", "dereg_date")),
all_vars(no_evidence_of(., start_fu_date))) %>%
group_by(start_fu_date) %>%
mutate(across(c(end_fu_date, subsequent_vax_date, death_date, dereg_date),
# time in weeks between start_fu_date and event
~ as.integer(.x - start_fu_date)/7)) %>%
ungroup() %>%
select(-start_fu_date) %>%
rename_at(vars(ends_with("_date")),
~ str_remove(.x, "_date")) %>%
mutate(
tte = pmin(subsequent_vax, dereg, death, end_fu, na.rm = TRUE),
status = if_else(
# because subsequent_vax must occur before death and dereg if occuring on same day
!is.na(subsequent_vax) & subsequent_vax == tte,
TRUE,
FALSE
)) %>%
select(patient_id, arm, subgroup, tte, status) %>%
mutate(across(arm,
~if_else(
arm == "unvax",
"Unvaccinated",
.x)
))
survtable_redacted <- round_km(
data = data_tte,
time = "tte",
event = "status",
strata = c("subgroup", "arm")
) %>%
ungroup() %>%
mutate(
c.inc = 1-surv,
c.inc.ll = 1-surv.ul,
c.inc.ul = 1-surv.ll
) %>%
mutate(across(starts_with("c."), round, digits = 5)) %>%
select(subgroup, arm, time, n.risk, n.event, n.censor, c.inc)
readr::write_csv(
survtable_redacted,
here::here("output", "subsequent_vax", "tables", "survtable_redacted.csv"))
# }
################################################################################
# scale for x-axis
K <- study_parameters$K
x_breaks <- seq(0,K*4,4)
x_labels <- x_breaks + 2
# create plot
plot_out <- survtable_redacted %>%
ggplot(aes(x = time, y = c.inc, colour = subgroup)) +
geom_step(size=0.8) +
facet_wrap(~ arm, nrow=2) +
scale_color_viridis_d(
name = "Subgroup"
) +
scale_x_continuous(
breaks = seq(0,24,4), # scale is time since start of period 1
labels = seq(2,26,4) # label scale as time since second vax
) +
labs(
x = "Weeks since second dose",
y = "Incidence of 1st dose Incidence of 3rd dose"
) +
theme_bw() +
theme(
panel.border = element_blank(),
axis.line.y = element_line(colour = "black"),
axis.text = element_text(size=8),
axis.title.x = element_text(size = 10, margin = margin(t = 20, r = 0, b = 10, l = 0)),
axis.title.y = element_text(size = 10, margin = margin(t = 0, r = 10, b = 0, l = 0)),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
strip.background = element_blank(),
strip.placement = "outside",
strip.text.y.left = element_text(angle = 0),
panel.spacing = unit(0.8, "lines"),
plot.title = element_text(hjust = 0, size = 11),
plot.title.position = "plot",
plot.caption.position = "plot",
plot.caption = element_text(hjust = 0, face= "italic"),
legend.position = c(0.75,0.2),
legend.key.size = unit(0.8, "cm")
)
ggsave(plot = plot_out,
filename = here::here("output", "subsequent_vax", "images", "ci_vax.png"),
width=16, height=12, units="cm")