-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.Rmd
293 lines (243 loc) · 10.1 KB
/
index.Rmd
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
---
title: "Interrupted Times Series Regression"
author: "Chuck Cleland"
date: '`r format(Sys.time(), "%B %d, %Y")`'
output:
html_document:
toc: true
toc_float:
collapsed: false
toc_depth: 4
df_print: paged
theme: simplex
highlight: textmate
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE, comment=NA, message=FALSE, warning=FALSE, fig.align = 'center', fig.width = 12, fig.height = 10)
options(width=110)
```
This illustration is based on the following paper and its supplementary material:
Bernal, J. L., Cummins, S., & Gasparrini, A. (2017). Interrupted time series regression for the evaluation of public health interventions: a tutorial. *International journal of epidemiology*, 46(1), 348-355.
<https://academic.oup.com/ije/article/46/1/348/2622842>
# Load Required R Packages
```{r}
library(lmtest)
library(vcd)
library(Epi)
library(tsModel)
library(splines)
library(tidyverse)
```
# Read in Comma-Delimited Data
```{r}
data <- read.csv("sicily.csv")
data %>%
knitr::kable()
```
This dataset includes the following variables:
* **year**
* **month**
* **time** = elapsed time since the start of the study
* **aces** = count of acute coronary episodes in Sicily per month (the outcome)
* **smokban** = smoking ban (the intervention) coded 0 before intervention, 1 after
* **pop** = the population of Sicily (in 10000s)
* **stdpop** = age standardised population
# Descriptive Analysis
Examining the data is an important first step.
Looking at the pre-intervention trend can give an indication of how stable the trend is over time, whether a linear model is likely to be appropriate, and whether there appears to be a seasonal trend.
## Scatter plot
```{r}
data$rate <- with(data, aces/stdpop*10^5)
plot(data$rate,type="n",ylim=c(00,300),xlab="Year", ylab="Std rate x 10,000", bty="l",xaxt="n")
rect(36,0,60,300,col=grey(0.9),border=F)
points(data$rate[data$smokban==0],cex=0.7)
axis(1,at=0:5*12,labels=F)
axis(1,at=0:4*12+6,tick=F,labels=2002:2006)
title("Sicily, 2002-2006")
```
## Summary Statistics
```{r}
data %>%
gather(Variable, Value) %>%
group_by(Variable) %>%
summarize(n = n(),
Mean = mean(Value),
SD = sd(Value),
Median = median(Value),
IQR = IQR(Value),
Min = min(Value),
Max = max(Value)) %>%
knitr::kable()
```
## Acute Coronary Event Rates Before and After Smoking Ban
```{r}
data %>%
mutate(Period = case_when(smokban == 0 ~ "1. Before Ban",
smokban == 1 ~ "2. After Ban")) %>%
select(Period, aces, rate) %>%
gather(Variable, Value, -Period) %>%
group_by(Period, Variable) %>%
summarize(n = n(),
Mean = mean(Value),
SD = sd(Value),
Median = median(Value),
IQR = IQR(Value),
Min = min(Value),
Max = max(Value)) %>%
knitr::kable()
```
# Poisson Regression Model
In step 2 (main paper) we chose a step change model and we also used a Poisson model as we are using count data.
In order to do this we model the count data directly (rather than the rate which doesn't follow a Poisson distribution), using the population (log transformed) as an offset variable in order to transform back to rates.
```{r}
model1 <- glm(aces ~ offset(log(stdpop)) + smokban + time, family=poisson, data)
summary(model1)
summary(model1)$dispersion
round(ci.lin(model1,Exp=T),3)
```
* The exponentiated intercept is the ACE rate in December 2001, and in this example is about 200 per 10,000.
* The exponentiated smokban effect is a rate ratio, and in this example the ban multiplied the ACE rate by 0.89 (a reduction).
* The expected ACE rate after the ban would be 0.89 * 200 = 178 per 10,000.
## Add predictions from the Poisson model to the graph
```{r}
datanew <- data.frame(stdpop = mean(data$stdpop),
smokban = rep(c(0, 1), c(360, 240)),
time = 1:600/10,
month = rep(1:120/10, 5))
pred1 <- predict(model1, type="response", datanew) / mean(data$stdpop) * 10^5
plot(data$rate, type="n", ylim=c(0,300), xlab="Year", ylab="Std rate x 10,000", bty="l", xaxt="n")
rect(36, 0, 60, 300, col=grey(0.9), border=F)
points(data$rate,cex=0.7)
axis(1, at=0:5*12, labels=F)
axis(1, at=0:4*12+6, tick=F, labels=2002:2006)
lines((1:600/10), pred1, col=2)
title("Sicily, 2002-2006")
```
## To plot the counterfactual scenario we create a data frame as if smokban (the intervention) had never been implemented
```{r}
datanew <- data.frame(stdpop=mean(data$stdpop),smokban=0,time=1:600/10,
month=rep(1:120/10,5))
pred1b <- predict(model1, datanew, type="response") / mean(data$stdpop) * 10^5
plot(data$rate, type="n", ylim=c(0,300), xlab="Year", ylab="Std rate x 10,000", bty="l", xaxt="n")
rect(36, 0, 60, 300, col=grey(0.9), border=F)
points(data$rate,cex=0.7)
axis(1, at=0:5*12, labels=F)
axis(1, at=0:4*12+6, tick=F, labels=2002:2006)
lines((1:600/10), pred1, col=2)
lines(datanew$time, pred1b, col=2, lty=2)
title("Sicily, 2002-2006")
```
## Return the data frame to the scenario including the intervention
```{r}
datanew <- data.frame(stdpop = mean(data$stdpop),
smokban = rep(c(0, 1), c(360, 240)),
time = 1:600/10,
month = rep(1:120/10, 5))
```
# Methodological Issues
## Overdispersion: Quasi-Poisson model
In the model above we have not allowed for overdispersion - in order to do this we can use a quasipoisson model, which allows the variance to be proportional rather than equal to the mean.
```{r}
model2 <- glm(aces ~ offset(log(stdpop)) + smokban + time,
family=quasipoisson, data)
summary(model2)
summary(model2)$dispersion
round(ci.lin(model2,Exp=T),3)
```
## Model checking and autocorrelation
### Check the residuals by plotting against time
```{r}
res2 <- residuals(model2,type="deviance")
plot(data$time, res2, ylim=c(-5,10), pch=19, cex=0.7, col=grey(0.6), main="Residuals over time", ylab="Deviance residuals", xlab="Date")
abline(h=0, lty=2, lwd=2)
```
### Further check for autocorrelation by examining the autocorrelation and partial autocorrelation functions
```{r}
acf(res2)
pacf(res2)
```
## Adjusting for seasonality
There are various ways of adjusting for seasonality - here we use harmonic terms specifying the number of sin and cosine pairs to include (in this case 2) and the length of the period (12 months)
```{r}
model3 <- glm(aces ~ offset(log(stdpop)) + smokban + time + harmonic(month,2,12),
family=quasipoisson, data)
summary(model3)
summary(model3)$dispersion
round(ci.lin(model3,Exp=T),3)
```
### EFFECTS
```{r}
ci.lin(model3,Exp=T)["smokban",5:7]
```
* Taking into account seasonality of the ACE rate, the smoking ban effect was a rate ratio of 0.885 (slightly bigger reduction)
### TREND
```{r}
exp(coef(model3)["time"]*12)
```
* Taking into account seasonality of the ACE rate, the ACE rate was being multiplied by about 1.07 each year, a long-term temporal trend.
### We again check the model and autocorrelation functions
```{r}
res3 <- residuals(model3,type="deviance")
plot(res3,ylim=c(-5,10),pch=19,cex=0.7,col=grey(0.6),main="Residuals over time",
ylab="Deviance residuals",xlab="Date")
abline(h=0,lty=2,lwd=2)
acf(res3)
pacf(res3)
```
### Predict and plot of the seasonally adjusted model
```{r}
pred3 <- predict(model3, type="response", datanew) / mean(data$stdpop) * 10^5
plot(data$rate, type="n", ylim=c(120,300), xlab="Year", ylab="Std rate x 10,000", bty="l", xaxt="n")
rect(36, 120, 60, 300, col=grey(0.9), border=F)
points(data$rate, cex=0.7)
axis(1, at=0:5*12, labels=F)
axis(1, at=0:4*12+6, tick=F, labels=2002:2006)
lines(1:600/10, pred3, col=2)
title("Sicily, 2002-2006")
```
It is sometimes difficult to clearly see the change graphically in the seasonally adjusted model, therefore it can be useful to plot a straight line representing a 'deseasonalised' trend this can be done by predicting all the observations for the same month, in this case we use June.
```{r}
pred3b <- predict(model3, type="response", transform(datanew,month=6)) / mean(data$stdpop) * 10^5
```
### This can then be added to the plot as a dashed line
```{r}
plot(data$rate, type="n", ylim=c(120,300), xlab="Year", ylab="Std rate x 10,000", bty="l", xaxt="n")
rect(36, 120, 60, 300, col=grey(0.9), border=F)
points(data$rate, cex=0.7)
axis(1, at=0:5*12, labels=F)
axis(1, at=0:4*12+6, tick=F, labels=2002:2006)
lines(1:600/10, pred3, col=2)
lines(1:600/10, pred3b, col=2, lty=2)
title("Sicily, 2002-2006")
```
# Additional Material
## Add a change-in-slope
## We parameterize it as an interaction between time and the ban indicator
```{r}
model4 <- glm(aces ~ offset(log(stdpop)) + smokban*time + harmonic(month,2,12),
family=quasipoisson, data)
summary(model4)
round(ci.lin(model4,Exp=T),3)
```
## Predict and plot the 'deseasonalised' trend and compare it with the step-change only model
```{r}
pred4b <- predict(model4, type="response", transform(datanew, month=6)) / mean(data$stdpop) * 10^5
plot(data$rate, type="n", ylim=c(120,300), xlab="Year", ylab="Std rate x 10,000", bty="l", xaxt="n")
rect(36, 120, 60, 300, col=grey(0.9), border=F)
points(data$rate, cex=0.7)
axis(1, at=0:5*12, labels=F)
axis(1, at=0:4*12+6, tick=F, labels=2002:2006)
lines(1:600/10, pred3b, col=2)
lines(1:600/10, pred4b, col=4)
title("Sicily, 2002-2006")
legend("topleft",
c("Step-change only", "Step-change + change-in-slope"),
lty=1, col=c(2,4), inset=0.05, bty="n", cex=0.7)
```
Test if the change-in-slope improve the fit the selected test here is an F-test, which accounts for the overdispersion, while in other cases a likelihood ratio or wald test can be applied
```{r}
anova(model3, model4, test="F") %>%
knitr::kable()
```
* Not surprisingly, the p-value is similar to that of the interaction term.
* The ban reduced the ACE rate, but did not change the longer-term temporal trend in the rate (it continued to grow over time).