tsPI: R package for improved prediction intervals for ARIMA and structural time series models with exogenous variables

Package tsPI computes prediction intervals for ARIMA and Gaussian structural time series models by using importance sampling approach with uninformative priors for model parameters, leading to more accurate coverage probabilities in frequentist sense. Instead of sampling the future observations and hidden states of the state space representation of the model, only model parameters are sampled, and the method is based solving the equations corresponding to the conditional coverage probability of the prediction intervals. This makes method relatively fast compared to for example MCMC methods, and standard errors of prediction limits can also be computed straightforwardly.

ARIMA case is based on articles

Structural time series model case is based on a straightforward generalization presented in

Example: 95 % prediction intervals for AR(1) process

library(tsPI)
library(KFAS) #for plug-in intervals

set.seed(12345)
x <- arima.sim(n = 30, model = list(ar = 0.9))
fit <- arima(x, c(1, 0, 0))
model <- SSModel(x ~ SSMarima(ar = fit$coef[1], Q = fit$sigma), H = 0)
model$P1inf[1,1] <- 0
model$a1[1] <- fit$coef[2]
pred_plugin <- predict(model, n.ahead = 10, interval = "prediction")
pred_tspi  <- arima_pi(x, c(1, 0, 0), n_ahead = 10, se_limits = FALSE, nsim = 1000)

ylim <- round(range(c(pred_plugin, pred_tspi, x)) + c(-1, 1))

plot(ts.union(x, pred_plugin, pred_tspi), plot.type = "single",
  col = c(1, 2, 2, 2, 4, 4, 4), pch = c(19, 15, 15, 15, 15, 15, 15),
  lty = c(1, 1, 1, 1, 1), type = "b",
  ylim = ylim, xlab = "time", ylab= "value", axes = FALSE)
axis(1, at = 1:40, labels = 1:40)
axis(2, at = ylim[1]:ylim[2])
legend("topleft", c("observations", "plug-in",  "tsPI"), 
  lty = 1, pch= c(19, 15, 15), col = c(1, 2, 4))

imfs

Installing tsPI

Package is now available at CRAN. The latest development version can be installed from the github using the devtools package:

install.packages("devtools")
library(devtools)
# install_github("helske/tsPI")
install.packages("tsPI", repos = c('https://helske.r-universe.dev', 'https://cloud.r-project.org'))