I have some time series data including four different locations. There is an intervention at a certain point in time (different in each location).
d <- structure(list(location = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L),
time = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L), iv = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L), outcome = c(27L,
23L, 13L, 31L, 27L, 31L, 27L, 24L, 31L, 20L, 13L, 14L, 12L,
14L, 12L, 16L, 18L, 14L, 20L, 15L, 9L, 15L, 13L, 24L, 13L,
12L, 14L, 17L, 12L, 9L, 22L, 21L, 22L, 30L, 28L, 28L, 32L,
30L, 51L, 42L, 43L, 32L, 45L, 39L, 43L, 26L, 22L, 25L, 14L,
21L, 22L, 17L, 8L, 12L, 14L, 13L, 14L, 11L, 7L, 6L, 20L,
24L, 22L, 27L, 27L, 22L, 23L, 27L, 27L, 24L, 26L, 35L, 32L,
26L, 22L, 29L, 26L, 38L, 24L, 15L, 13L, 15L, 9L, 12L, 9L,
4L, 8L, 7L, 8L, 4L, 37L, 22L, 27L, 24L, 33L, 20L, 28L, 26L,
23L, 21L, 29L, 28L, 26L, 24L, 31L, 27L, 24L, 24L, 18L, 18L,
24L, 19L, 24L, 27L, 30L, 13L, 23L, 15L, 13L, 16L)), class = "data.frame", row.names = c(NA,
-120L))
I am trying to fit a model that allows both a different step change and slope change in each location. Currently I have a model that allows the step change:
m <- glmer(outcome ~ time + (1 + iv|location), family = 'poisson', data = d)
d$p <- predict(m, newdata = d, type = 'response')
par(mfrow = c(4, 1), mar = c(1, 0, 0, 0), oma = c(5, 5, 0, 0))
for(i in 1:4) {
plot(1, type = 'n', xlim = c(0, 30), ylim = c(0, 50), axes = F, xlab = NA, ylab = NA)
with(d[d$location == i,], {
rect(0, 0, 31, 50)
points(time, outcome)
lines(time, p)
if(i == 4) axis(1, pos = 0)
axis(2, las = 2, pos = 0)
text(28, 50 * 0.9, paste0('Location ', i), font = 2)
})
}
mtext('Time period', side = 1, outer = T, line = 2.7, cex = 0.8)
mtext('Event count', side = 2, outer = T, line = 3, cex = 0.8)
In particular, you can see the slopes for location 2 are poorly modelled:
How would you modify the glmer
formula so it includes a change-in-slope?
glm(outcome ~ time*iv*location, data = d, family = poisson)
will give you different breaks and slopes for each location, but I don't know if that's an appropriate model. There are a few folks here who could answer, but more over at CV (plus the ones here who could answer also hang out at CV). $\endgroup$