generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itt_analysis.R
153 lines (121 loc) · 5.63 KB
/
itt_analysis.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
################################################################
# This script:
# - Conducts regression model and plots predicted values
################################################################
# For running locally only #
# setwd("C:/Users/aschaffer/OneDrive - Nexus365/Documents/GitHub/vax-fourth-dose-RD")
# getwd()
# Import libraries #
library('tidyverse')
library('lubridate')
library('arrow')
library('here')
library('reshape2')
library('dplyr')
library('fs')
library('ggplot2')
library('RColorBrewer')
library('data.table')
dir_create(here::here("output", "covid_outcomes"), showWarnings = FALSE, recurse = TRUE)
dir_create(here::here("output", "covid_outcomes", "by_start_date"), showWarnings = FALSE, recurse = TRUE)
dir_create(here::here("output", "modelling"), showWarnings = FALSE, recurse = TRUE)
dir_create(here::here("output", "modelling","figures"), showWarnings = FALSE, recurse = TRUE)
### Function to run sharp RD analysis, output coefficients and predicted values
### Plot crude rates
sharp <- function(start_date){
# Read in data
data <- read.csv(here::here("output", "covid_outcomes", "by_start_date", paste0("outcomes_byage_3mon_",start_date,".csv"))) %>%
subset(!is.na(age_3mos) & age_3mos >= 180 & age_3mos < 220)
mod <- function(out, name, suffix){
# Prep data
len <- nrow(data)
df <- data %>%
mutate(over50 = if_else(age_3mos >= 200, 1, 0, 0),
age_3mos_c = as.numeric(age_3mos - 200),
quarter = rep(1:4, length.out = len)) %>%
rename(outcome = {{out}})
# Model
mod <- glm(outcome / 100000 ~ age_3mos_c*over50, data = df,
family = binomial("logit"), weights = total)
# Save coefficients and 95% CIs
coef <- data.frame(est = mod$coefficients)
coef2 <- coef %>% data.frame() %>%
mutate(var = row.names(coef)) %>%
cbind(confint(mod), aic = AIC(mod)) %>%
mutate(start_date = start_date,
outcome = name) %>%
rename(lci = `2.5 %`, uci = `97.5 %`)
# Save coefficients
write.csv(coef2, here::here("output", "modelling", paste0("coef_",suffix,"_",start_date,".csv")),
row.names = FALSE)
# Create new data for predicting counterfactual value at cutoff
newdata <- df %>% mutate(over50 = 0)
# Predicted values
pred.df1 <- predict(mod, se.fit = TRUE, type = "response") %>%
data.frame() %>%
mutate(pred1 = fit,
lci1 = fit - 1.96*se.fit,
uci1 = fit + 1.96*se.fit) %>%
select(c("pred1","lci1","uci1"))
# Predicted counterfactual values
pred.df2 <- predict(mod, se.fit=TRUE, type = "response",
newdata=newdata) %>%
data.frame() %>%
mutate(pred2= fit,
lci2 = fit - 1.96*se.fit,
uci2 = fit + 1.96*se.fit) %>%
select(c("pred2","lci2","uci2"))
# Combine with original data
df_pred <- cbind(age_3mos = df$age_3mos,
rate = df$out,
total = df$total,
pred.df1, pred.df2
) %>%
mutate(pred1 = pred1 * 100000,
lci1 = lci1 * 100000,
uci1 = uci1 * 100000,
pred2 = pred2 * 100000,
lci2 = lci2 * 100000,
uci2 = uci2 * 100000,
start = start_date,
outcome = name)
write.csv(df_pred, here::here("output", "modelling", paste0("predicted_",suffix,"_",start_date,".csv")), row.names = FALSE)
ggplot() +
geom_ribbon(data=subset(df_pred, age_3mos <= 200),
aes(x=age_3mos / 4, ymin=lci2, ymax=uci2), alpha=0.2, fill = "gray50") +
geom_ribbon(data=subset(df_pred, age_3mos >= 200),
aes(x=age_3mos / 4, ymin=lci1, ymax=uci1), alpha=0.2, fill = "gray50") +
geom_point(data=df_pred, aes(x = age_3mos / 4, y = rate), size = 1.25, alpha= .5) +
geom_vline(data=df_pred, aes(xintercept = 50), linetype = "longdash") +
geom_line(data=subset(df_pred, age_3mos <= 200),
aes(x=age_3mos / 4, y = pred2), size = .8,
linetype = "longdash") +
geom_line(data=subset(df_pred, age_3mos >= 200),
aes(x=age_3mos / 4, y = pred1), size = .8,
linetype = "longdash") +
scale_colour_manual(values = c("dodgerblue3", "maroon", "forestgreen")) +
scale_y_continuous(expand = expansion(mult = c(.1, .1))) +
xlab("Age") + ylab("No. events per 100,000 (predicted)") +
theme_bw() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
strip.background = element_blank(),
strip.text = element_text(hjust = 0),
legend.title = element_blank(), legend.position = "none",
axis.text.x = element_text(angle = 45, hjust = 1))
ggsave(here::here("output", "modelling", "figures", paste0("plot_pred_",suffix,start_date,".png")),
dpi = 300, units = "in", width = 6, height = 8)
}
# Run for each outcome
mod(rate_covidcomposite, "COVID unplanned admission/A&E/death", "covidcomp")
mod(rate_covidadmitted, "COVID unplanned admission", "covidadmit")
mod(rate_covidemerg, "COVID A&E", "covidemerg")
mod(rate_coviddeath, "COVID death", "coviddeath")
mod(rate_respcomposite, "Respiratory composite", "respcomp")
mod(rate_respadmitted, "Respiratory admission", "respadmit")
mod(rate_anyadmitted, "All cause unplanned admission", "anyadmit")
}
# Create list of dates
start_dates <- c(as.Date("2022-09-03"), as.Date( "2022-10-15"), as.Date(0:10, origin = "2022-11-26"))
# Run function over all dates
sapply(start_dates, sharp)