Prediction intervals for GLMs

Overview

Our implementation of prediction intervals follows that described by Gavin Simpson in two posts on his blog. Whilst what follows is a brief overview, more detail, including discussion on whether or not it makes sense to calculate these intervals, can be found in the following post.

Confidence interval

To calculate prediction intervals we first calculate the confidence interval on the scale of the linear predictor. The upper and lower bounds of this interval, are then fed in to the inverse link function which in turn gives us a confidence interval on the expected response.

Prediction interval

Once we have calculated the confidence interval on the response we feed the upper and lower bounds, in to the quantile function associated with the relevant distribution. The maximum and minimum values of the output are then used as the upper and lower bounds of our prediction interval.

Comparison to a bootstrap approach

Below we compare the prediction intervals from trending with those generated by the ciTools package. ciTools uses a parametric bootstrap approach so the expectation is that trending will produce a more conservative (wider) interval when we allow for uncertainty around the estimate, and a less conservative (narrower) interval when uncertainty is ignored.

The following examples build on those discussed in the ciTools glm vignette:

Example 1 - Poisson

library(ciTools)
#> ciTools version 0.6.0 (C) Institute for Defense Analyses
library(trending)
library(ggplot2)
library(patchwork)
library(MASS)
#> 
#> Attaching package: 'MASS'
#> The following object is masked from 'package:patchwork':
#> 
#>     area
#> The following object is masked from 'package:dplyr':
#> 
#>     select


# generate data
x <- rnorm(100, mean = 0)
y <- rpois(n = 100, lambda = exp(1.5 + 0.5*x))
dat <- data.frame(x = x, y = y)
fit <- glm(y ~ x , family = poisson(link = "log"))

# use ciTools to add prediction interval
dat1 <- add_pi(dat, fit, names = c("lpb", "upb"), alpha = 0.1, nsims = 20000)
#> Warning in add_pi.glm(dat, fit, names = c("lpb", "upb"), alpha = 0.1, nsims =
#> 20000): The response is not continuous, so Prediction Intervals are approximate
head(dat1)
#>              x y     pred lpb upb
#> 1  1.382715924 6 9.011444   4  14
#> 2 -0.503592327 4 3.222868   1   6
#> 3 -0.450384108 7 3.317712   1   7
#> 4  0.379215754 3 5.214739   2   9
#> 5  0.003826034 3 4.249769   1   8
#> 6 -0.124772797 5 3.962066   1   8

# add intervals with trending (no uncertainty in parameters)
poisson_model <- glm_model(y ~ x, family = "poisson")
fitted_model <- fit(poisson_model, dat)
dat2 <- predict(fitted_model, uncertain = FALSE, alpha = 0.1)
head(dat2)
#>   y            x estimate lower_ci upper_ci lower_pi upper_pi
#> 1 6  1.382715924 9.011444 8.228578 9.868793        4       14
#> 2 4 -0.503592327 3.222868 2.887380 3.597336        1        6
#> 3 7 -0.450384108 3.317712 2.980889 3.692593        1        7
#> 4 3  0.379215754 5.214739 4.838089 5.620710        2        9
#> 5 3  0.003826034 4.249769 3.900300 4.630551        1        8
#> 6 5 -0.124772797 3.962066 3.617007 4.340043        1        7

# add intervals with trending (uncertainty in parameters)
dat3 <- predict(fitted_model, alpha = 0.1)
head(dat3)
#>   y            x estimate lower_ci upper_ci lower_pi upper_pi
#> 1 6  1.382715924 9.011444 8.228578 9.868793        4       15
#> 2 4 -0.503592327 3.222868 2.887380 3.597336        0        7
#> 3 7 -0.450384108 3.317712 2.980889 3.692593        0        7
#> 4 3  0.379215754 5.214739 4.838089 5.620710        2       10
#> 5 3  0.003826034 4.249769 3.900300 4.630551        1        8
#> 6 5 -0.124772797 3.962066 3.617007 4.340043        1        8

# plots
p1 <- ggplot(dat1, aes(x, y)) +
  geom_point(size = 1) +
  geom_line(aes(y = pred), size = 1.2) +
  geom_ribbon(aes(ymin = lpb, ymax = upb), alpha = 0.2) +
  geom_ribbon(aes(ymin = `lower_pi`, ymax = `upper_pi`), data = dat2, alpha = 0.4) +
  ggtitle("Poisson regression with prediction intervals and no uncertainty in parameters", 
          subtitle = "Model fit (black line), with bootstrap intervals (gray), parametric intervals (dark gray)") +
  coord_cartesian(ylim=c(0, 30))

p2 <- ggplot(dat1, aes(x, y)) +
  geom_point(size = 1) +
  geom_line(aes(y = pred), size = 1.2) +
  geom_ribbon(aes(ymin = lpb, ymax = upb), alpha = 0.4) +
  geom_ribbon(aes(ymin = `lower_pi`, ymax = `upper_pi`), data = dat3, alpha = 0.2) +
  ggtitle("Poisson regression with prediction intervals and uncertainty in parameters", 
          subtitle = "Model fit (black line), with parametric intervals (gray), bootstrap intervals (dark gray)") +
  coord_cartesian(ylim=c(0, 30))

p1 / p2

Example 2 - Quassipoisson

# generate data
x <- runif(n = 100, min = 0, max = 2)
mu <- exp(1 + x)
y <- rnegbin(n = 100, mu = mu, theta = mu/(5 - 1))
dat <- data.frame(x = x, y = y)
fit <- glm(y ~ x, family = quasipoisson(link = "log"))

# use ciTools to add prediction interval
dat1 <- add_pi(dat, fit, names = c("lpb", "upb"), alpha = 0.1, nsims = 20000)
#> Warning in add_pi.glm(dat, fit, names = c("lpb", "upb"), alpha = 0.1, nsims =
#> 20000): The response is not continuous, so Prediction Intervals are approximate
head(dat1)
#>           x  y      pred lpb upb
#> 1 1.1760362  2  9.078129   1  24
#> 2 1.2336387  6  9.603837   1  25
#> 3 0.1830865  1  3.439979   0  13
#> 4 0.4940388  0  4.661592   0  17
#> 5 1.8738558 15 17.954530   4  39
#> 6 1.2372120  5  9.637434   1  25

# add intervals with trending (no uncertainty in parameters)
quasipoisson_model <- glm_model(y ~ x, family = quasipoisson(link = "log"))
fitted_model <- fit(quasipoisson_model, dat)
dat2 <- predict(fitted_model, uncertain = FALSE, alpha = 0.1)
head(dat2)
#>    y         x  estimate  lower_ci  upper_ci lower_pi upper_pi
#> 1  2 1.1760362  9.078129  7.864774 10.478678        1       24
#> 2  6 1.2336387  9.603837  8.340052 11.059126        1       25
#> 3  1 0.1830865  3.439979  2.444271  4.841304        0       13
#> 4  0 0.4940388  4.661592  3.581514  6.067388        0       16
#> 5 15 1.8738558 17.954530 14.403336 22.381282        4       38
#> 6  5 1.2372120  9.637434  8.369950 11.096856        1       25

# add intervals with trending (uncertainty in parameters)
dat3 <- predict(fitted_model, alpha = 0.1)
head(dat3)
#>    y         x  estimate  lower_ci  upper_ci lower_pi upper_pi
#> 1  2 1.1760362  9.078129  7.864774 10.478678        0       26
#> 2  6 1.2336387  9.603837  8.340052 11.059126        0       27
#> 3  1 0.1830865  3.439979  2.444271  4.841304        0       16
#> 4  0 0.4940388  4.661592  3.581514  6.067388        0       18
#> 5 15 1.8738558 17.954530 14.403336 22.381282        3       45
#> 6  5 1.2372120  9.637434  8.369950 11.096856        0       27

# plots
p3 <- ggplot(dat1, aes(x, y)) +
  geom_point(size = 1) +
  geom_line(aes(y = pred), size = 1.2) +
  geom_ribbon(aes(ymin = lpb, ymax = upb), alpha = 0.2) +
  geom_ribbon(aes(ymin = `lower_pi`, ymax = `upper_pi`), data = dat2, alpha = 0.4) +
  ggtitle("Quasipoisson regression with prediction intervals and no uncertainty in parameters", 
          subtitle = "Model fit (black line), with bootstrap intervals (gray), parametric intervals (dark gray)") +
  coord_cartesian(ylim=c(0, 30))

p4 <- ggplot(dat1, aes(x, y)) +
  geom_point(size = 1) +
  geom_line(aes(y = pred), size = 1.2) +
  geom_ribbon(aes(ymin = lpb, ymax = upb), alpha = 0.4) +
  geom_ribbon(aes(ymin = `lower_pi`, ymax = `upper_pi`), data = dat3, alpha = 0.2) +
  ggtitle("Quasipoisson regression with prediction intervals and uncertainty in parameters", 
          subtitle = "Model fit (black line), with parametric intervals (gray), bootstrap intervals (dark gray)") +
  coord_cartesian(ylim=c(0, 30))

p3 / p4