Skip to contents

The R package conformalForecast provides methods and tools for performing multistep-ahead time series forecasting using conformal prediction methods including classical conformal prediction, adaptive conformal prediction, conformal PID control, and autocorrelated multistep-ahead conformal prediction.

Installation

You can install the development version of conformalForecast from GitHub with:

# install.packages("remotes")
remotes::install_github("xqnwang/conformalForecast")

You can also get the official release version from CRAN with:

install.packages("conformalForecast")

Example

This basic example shows how to perform classical conformal prediction and update the results when new observations become available.

We simulate a series from an AR(2) model, holding back the last five observations to stand in for data that arrives later:

library(conformalForecast)
set.seed(0)
series_all <- arima.sim(n = 1005, list(ar = c(0.8, -0.5)), sd = 1)
series <- head(series_all, 1000)
new_data <- as.numeric(tail(series_all, 5))

Conformal calibration needs a record of past forecast errors. We obtain one with cvforecast(), which refits the model on a rolling forecast origin and stores the out-of-sample errors at every horizon:

far2 <- function(x, h, level) {
  Arima(x, order = c(2, 0, 0)) |>
    forecast(h = h, level = level)
}

fc <- cvforecast(series, forecastfun = far2, h = 3, level = c(80, 95),
                 forward = TRUE, initial = 1, window = 100)

For a base model that returns only point forecasts, use level = NULL in cvforecast() and supply alpha explicitly when calibrating (for example, alpha = 0.1 for 90% conformal intervals).

Now calibrate intervals with split conformal prediction, on a rolling window of 100 calibration scores:

scpfc <- conformal(fc, method = "scp", ncal = 100, rolling = TRUE)

When the five held-back observations arrive, update() extends the fit incrementally. Intervals that were already issued do not change:

scpfc_updated <- update(scpfc, new_data = new_data, forecastfun = far2)

Finally, evaluate the intervals by forecast horizon:

# Interval forecast accuracy
accuracy(scpfc_updated, byhorizon = TRUE)
#>        Winkler_95  MSIS_95
#> CV h=1   5.024650 4.850147
#> CV h=2   6.442104 6.218875
#> CV h=3   6.568085 6.335622

# Mean coverage
coverage(scpfc_updated, level = 95)
#>       h=1       h=2       h=3
#> 0.9577640 0.9613948 0.9488140

# Mean and median interval width
width(scpfc_updated, level = 95, includemedian = TRUE)
#> Mean width:
#>      h=1      h=2      h=3
#> 4.289693 5.570434 5.607451
#>
#> Median width:
#>      h=1      h=2      h=3
#> 4.154276 5.619012 5.404932

Learn more

License

This package is free and open source software, licensed under GPL-3.