Unified interface and conformal prediction (calibrated prediction intervals) for R package forecast (and affiliates)

R-bloggers 2024-11-24

[This article was first published on T. Moudiki's Webpage - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In the popular R package forecast, there are 2 different types of interfaces:

  • A direct interface for functions like forecast::thetaf doing fitting and inference simultaneously
nile.fcast <- forecast::thetaf(Nile)plot(nile.fcast)
  • An interface for fitting first and then forecasting, like forecast::ets, where you need to use, in addition forecast::forecast:
fit <- forecast::ets(USAccDeaths)plot(forecast::forecast(fit))

In this post, I describe how to obtain probabilistic forecasts from R package forecast – and packages that follow a similar philosophy such as forecastHybrid, ahead, etc. –, by using a unified interface (ahead::genericforecast). Then, I present ahead::conformalize, a function that allows to obtain forecasts using the method described in Conformalized predictive simulations for univariate time series (more details can be found in these slides).

0 - Packages

utils::install.packages(c("remotes", "e1071", "forecast", "glmnet"))remotes::install_github("Techtonique/ahead")library(ahead)library(forecast)y <- fdeaths #AirPassengers #Nile #mdeaths #fdeaths #USAccDeathsh <- 25L

1 - Generic forecaster (unified interface)

1 - 1 - Using default parameters

par(mfrow=c(2, 2))plot(ahead::genericforecast(FUN=forecast::thetaf, y, h))plot(ahead::genericforecast(FUN=forecast::meanf, y, h))plot(ahead::genericforecast(FUN=forecast::rwf, y, h))plot(ahead::genericforecast(FUN=forecast::ets, y, h))par(mfrow=c(2, 2))plot(ahead::genericforecast(FUN=forecast::tbats, y, h))plot(ahead::genericforecast(FUN=HoltWinters, y, h))plot(ahead::genericforecast(FUN=forecast::Arima, y, h))plot(ahead::genericforecast(FUN=ahead::dynrmf, y, h))

xxx

xxx

1 - 2 - Using additional parameters

par(mfrow=c(2, 2))plot(ahead::genericforecast(FUN=ahead::dynrmf, y=y, h=h,                             fit_func=e1071::svm, predict_func=predict))plot(ahead::genericforecast(FUN=ahead::dynrmf, y=y, h=h,                             fit_func=glmnet::cv.glmnet, predict_func=predict))plot(ahead::genericforecast(FUN=forecast::tbats, y=y, h=h,                             use.box.cox = TRUE, use.trend=FALSE))plot(ahead::genericforecast(FUN=forecast::rwf,                             y=y, h=h, lambda=1.1))

xxx

2 - Conformal prediction

2 - 1 - Using default parameters

y <- USAccDeathspar(mfrow=c(3, 2))obj <- ahead::conformalize(FUN=forecast::thetaf, y, h); plot(obj)obj <- ahead::conformalize(FUN=forecast::meanf, y, h); plot(obj)obj <- ahead::conformalize(FUN=forecast::rwf, y, h); plot(obj)obj <- ahead::conformalize(FUN=forecast::ets, y, h); plot(obj)par(mfrow=c(2, 2))obj <- ahead::conformalize(FUN=forecast::auto.arima, y, h); plot(obj)obj <- ahead::conformalize(FUN=forecast::tbats, y, h); plot(obj)obj <- ahead::conformalize(FUN=HoltWinters, y, h); plot(obj)obj <- ahead::conformalize(FUN=forecast::Arima, y, h); plot(obj)

xxx

xxx

2 - 2 - Using additional parameters

y <- AirPassengerspar(mfrow=c(2, 2))obj <- ahead::conformalize(FUN=forecast::thetaf, y, h); plot(obj)obj <- ahead::conformalize(FUN=forecast::rwf, y=y, h=h, drift=TRUE); plot(obj)obj <- ahead::conformalize(FUN=HoltWinters, y=y, h=h, seasonal = "mult"); plot(obj)obj <- ahead::conformalize(FUN=ahead::dynrmf, y=y, h=h, fit_func=glmnet::cv.glmnet, predict_func=predict); plot(obj)

xxx

2 - 3 - Using other simulation methods (conformal prediction-based)

y <- fdeathspar(mfrow=c(3, 2))obj <- ahead::conformalize(FUN=forecast::thetaf, y=y, h=h, method="block-bootstrap"); plot(obj)obj <- ahead::conformalize(FUN=forecast::rwf, y=y, h=h, drift=TRUE, method="bootstrap"); plot(obj)obj <- ahead::conformalize(FUN=forecast::ets, y, h, method="kde"); plot(obj)obj <- ahead::conformalize(FUN=forecast::tbats, y=y, h=h, method="surrogate"); plot(obj)obj <- ahead::conformalize(FUN=HoltWinters, y=y, h=h, seasonal = "mult", method="block-bootstrap"); plot(obj)obj <- ahead::conformalize(FUN=ahead::dynrmf, y=y, h=h, fit_func=glmnet::cv.glmnet,                            predict_func=predict, method="surrogate"); plot(obj)

xxx

To leave a comment for the author, please follow the link and comment on their blog: T. Moudiki's Webpage - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Continue reading: Unified interface and conformal prediction (calibrated prediction intervals) for R package forecast (and affiliates)