generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis_enzymeRx.R
216 lines (184 loc) · 8.89 KB
/
analysis_enzymeRx.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
212
213
214
215
### INFO
# project: Project #27: The effect of COVID-19 on pancreatic cancer diagnosis and care
# subproject: NICE Quality statement 4: Pancreatic enzyme replacement therapy
# author: Agz Leman
# 28 Feb 2022
# Plots monthly rates
###
## library
library(tidyverse)
library(here)
library(MASS)
###
#download and prep the data
###
ERx_Rates <- read_csv(here::here("output", "measures", "measure_enzymeRx_rate.csv"))
####when using downloaded data
#ERx_Rates <- read.csv("C:/Users/al0025/OneDrive - University of Surrey/OldHomeDrive/al0025/Documents/OpenSafely/ERx/Output release/Archive/measure_enzymeRx_rate.csv")
#ERx_Rates$date <- as.Date(ERx_Rates$date, format = "%Y-%m-%d")
###### cut data that is after March
cut_date2 <- "2022-03-01"
a <- which(ERx_Rates$date > as.Date(cut_date2, format = "%Y-%m-%d"))
ERx_Rates <- ERx_Rates[-a,]
# calc rate per 100
ERx_Rates$rate <- ERx_Rates$enzyme_replace / ERx_Rates$population * 100
###
# plot monthly number of Rxs
###
p <- ggplot(data = ERx_Rates,
aes(date, enzyme_replace)) +
geom_line()+
geom_point()+
scale_x_date(date_breaks = "2 month",
date_labels = "%Y-%m")+
labs(title = "Patients receiving enzyme replacement",
x = "Time", y = "Number of patients")+
theme_bw()+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
start <- "2020-03-01"
p <- p + geom_vline(xintercept=as.Date(start, format="%Y-%m-%d"), size=0.3, colour="red")
guidel <- "2018-02-01"
p <- p + geom_vline(xintercept=as.Date(guidel, format="%Y-%m-%d"), size=0.3, colour="blue")
# save
ggsave(
plot= p, dpi=800,width = 20,height = 10, units = "cm",
filename="ERx_number.png", path=here::here("output"),
)
###
# plot monthly rates per 100
###
p <- ggplot(data = ERx_Rates,
aes(date, rate)) +
geom_line()+
geom_point()+
scale_x_date(date_breaks = "2 month",
date_labels = "%Y-%m")+
labs(title = "Patients receiving enzyme replacement",
x = "Time", y = "Rate per 100 patients diagnosed")+
theme_bw()+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
start <- "2020-03-01"
p <- p + geom_vline(xintercept=as.Date(start, format="%Y-%m-%d"), size=0.3, colour="red")
guideli <- "2018-02-01"
p <- p + geom_vline(xintercept=as.Date(guideli, format="%Y-%m-%d"), size=0.3, colour="blue")
QS <- "2018-12-01"
p <- p + geom_vline(xintercept=as.Date(QS, format="%Y-%m-%d"), size=0.3, colour="darkgreen")
p <- p + geom_text(aes(x=as.Date(QS, format="%Y-%m-%d"), y=25),
color = "darkgreen",label="Quality\nstandard", angle = 90, size = 3)
# save
ggsave(
plot= p, dpi=800,width = 20,height = 10, units = "cm",
filename="ERx_rates.png", path=here::here("output"),
)
########## model the data
model_data <- subset(ERx_Rates, select=c("rate","date"))
model_data$lockdown <- 0
model_data$guideline <- 0
# periods
start <- "2020-03-01"
guideli <- "2018-02-01"
model_data2 <- model_data[1:dim(model_data)[1],]
# censor the analysis - cut two months at the end
#model_data2 <- model_data[1:(dim(ERx_Rates)[1]-2),]
model_data2$time <- as.numeric(c(1:dim(model_data2)[1]))
model_data_no_covid <- model_data2
model_data2$guideline[model_data2$date>guideli & model_data2$date<=start]<-1
model_data2$lockdown[model_data2$date>start]<-1
model <- glm(rate ~ time
+ lockdown + lockdown*time
#+ guideline + guideline*time
, data=model_data2)
model_data2$predicted <- predict(model,type="response",model_data2)
model_data2$predicted_no_covid <- predict(model,type="response",model_data_no_covid)
ilink <- family(model)$linkinv
model_data2 <- bind_cols(model_data2, setNames(as_tibble(predict(model, model_data2, se.fit = TRUE)[1:2]),
c('fit_link','se_link')))
model_data2 <- mutate(model_data2,
pred = ilink(fit_link),
upr = ilink(fit_link + (2 * se_link)),
lwr = ilink(fit_link - (2 * se_link)))
model_data2 <- bind_cols(model_data2, setNames(as_tibble(predict(model, model_data_no_covid, se.fit = TRUE)[1:2]),
c('fit_link_noCov','se_link_noCov')))
model_data2 <- mutate(model_data2,
pred_noCov = ilink(fit_link_noCov),
upr_noCov = ilink(fit_link_noCov + (2 * se_link_noCov)),
lwr_noCov = ilink(fit_link_noCov - (2 * se_link_noCov)))
p <- ggplot(data = model_data,aes(date, rate, color = "Recorded data", lty="Recorded data")) +
geom_line()+
geom_point()+
scale_x_date(date_breaks = "3 month",
date_labels = "%Y-%m")+
labs(title = "Patients receiving enzyme replacement \n Region: whole England",
x = "", y = "Rate per 100 patients with \nunresectable pancreatic cancer")+
theme_bw()+
theme(axis.text.x = element_text(angle = 45, hjust = 1), legend.position="bottom")
start <- "2020-03-01"
p <- p + geom_vline(xintercept=as.Date(start, format="%Y-%m-%d"), size=0.3, colour="red")
p <- p + geom_text(aes(x=as.Date(start, format="%Y-%m-%d")+25, y=40),
color = "red",label="Lockdown", angle = 90, size = 3)
guideli <- "2018-02-01"
p <- p + geom_vline(xintercept=as.Date(guideli, format="%Y-%m-%d"), size=0.3, colour="black")
p <- p + geom_text(aes(x=as.Date(guideli, format="%Y-%m-%d"), y=40),
color = "black",label="National\nguidelines", angle = 90, size = 3)
QS <- "2018-12-01"
p <- p + geom_vline(xintercept=as.Date(QS, format="%Y-%m-%d"), size=0.3, colour="darkgreen")
p <- p + geom_text(aes(x=as.Date(QS, format="%Y-%m-%d"), y=40),
color = "darkgreen",label="Quality\nstandard", angle = 90, size = 3)
p<-p+geom_line(data=model_data2, aes(y=predicted, color = "Model with COVID-19", lty="Model with COVID-19"), size=0.5)
#p<-p+geom_ribbon(data=model_data2, aes(ymin = lwr, ymax = upr), fill = "grey30", alpha = 0.1)
p<-p+geom_line(data=model_data2, aes(y=predicted_no_covid, color = "Model", lty="Model"), size=0.5)
p<-p+geom_ribbon(data=model_data2, aes(ymin = lwr_noCov, ymax = upr_noCov),color = "red",
lty=0, fill = "red", alpha = 0.1)
p <- p + labs(caption="OpenSafely-TPP May 2022")
p <- p + theme(plot.caption = element_text(size=8))
p <- p + theme(plot.title = element_text(size = 10))
p <- p + scale_color_manual(name = NULL, values = c("Model" = "red", "Recorded data" = "black",
"Model with COVID-19" = "blue"),guide="none")
p <- p + scale_linetype_manual(name = NULL, values = c("Model" = "solid", "Recorded data" = "solid",
"Model with COVID-19" = "dashed"),guide="none")
p <- p + scale_fill_manual(name = NULL, values = c("Model" = "red", "Recorded data" = "white",
"Model with COVID-19" = "white"),guide="none")
p <- p + guides(colour = guide_legend(override.aes = list(linetype=c(1,2,1),fill=c("red",NA,NA),
shape = c(NA, NA, 16))))
ggsave(
plot= p, dpi=800,width = 20,height = 10, units = "cm",
filename="model_rates.png", path=here::here("output"),
)
####
# plot by region
####
Region <- read_csv(here::here("output", "measures", "measure_ExByRegion_rate.csv"))
###### cut data that is after March
a <- which(Region$date > as.Date(cut_date2, format = "%Y-%m-%d"))
Region <- Region[-a,]
Region$rate <- Region$enzyme_replace / Region$population * 100
p <- ggplot(data = Region,
aes(date, rate, color = region, lty = region)) +
geom_line()+
#geom_point(color = "region")+
scale_x_date(date_breaks = "3 month",
date_labels = "%Y-%m")+
labs(title = "Patients receiving enzyme replacement \n by Region in England",
x = "", y = "Rate per 100 patients with \nunresectable pancreatic cancer")+
theme_bw()+
theme(axis.text.x = element_text(angle = 45, hjust = 1), legend.position="bottom")
p <- p + labs(caption="OpenSafely-TPP May 2022")
p <- p + theme(plot.caption = element_text(size=8))
p <- p + theme(plot.title = element_text(size = 10))
start <- "2020-03-01"
p <- p + geom_vline(xintercept=as.Date(start, format="%Y-%m-%d"), size=0.3, colour="red")
p <- p + geom_text(aes(x=as.Date(start, format="%Y-%m-%d")+25, y=25),
color = "red",label="Lockdown", angle = 90, size = 3)
guideli <- "2018-02-01"
p <- p + geom_vline(xintercept=as.Date(guideli, format="%Y-%m-%d"), size=0.3, colour="black")
p <- p + geom_text(aes(x=as.Date(guideli, format="%Y-%m-%d"), y=25),
color = "black",label="National\nguidelines", angle = 90, size = 3)
QS <- "2018-12-01"
p <- p + geom_vline(xintercept=as.Date(QS, format="%Y-%m-%d"), size=0.3, colour="darkgreen")
p <- p + geom_text(aes(x=as.Date(QS, format="%Y-%m-%d"), y=25),
color = "darkgreen",label="Quality\nstandard", angle = 90, size = 3)
# save
ggsave(
plot= p, dpi=800,width = 20,height = 10, units = "cm",
filename="Region.png", path=here::here("output"),
)