Stacked generalization (Machine Learning model stacking) + conformal prediction for forecasting with ahead::mlf
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Introduction
In this post, I’ll show how to use ahead::mlf
, a function from R package ahead that does Conformalized Forecasting. ahead::mlf
uses Machine Leaning models (any of them) and time series lags in an autoregressive way, as workhorses for univariate probabilistic time series forecasting.
This function differs from ahead::dynrmf
by the fact that it doesn’t automatically select time series lags lags and by the availability of a simple stacked generalization functionality. I first discussed a stacked generalization method for forecasting in this 2018 document, at page 79.
In the context of ahead::mlf
, the algorithm described in https://www.researchgate.net/publication/379643443_Conformalized_predictive_simulations_for_univariate_time_series for time series conformal prediction is enriched: the predictions obtained on the calibration set are used as covariates for test set predictions. It’s important to choose a model that doesn’t overfit much here, as there are already a lot of information provided to the calibration set by this algorithm. I choose the Elastic Net.
What We’ll Cover
- Installing the package
- Run the examples
1 – Installing the package
options(repos = c( techtonique = "https://r-packages.techtonique.net", CRAN = "https://cloud.r-project.org" )) install.packages("ahead") install.packages("glmnet")
2 – Run the examples
At this stage, it’s worth trying out other Elastic Net implementations (e.g, just glmnet::glmnet
), in particular because, due to glmnet::cv.glmnet
’s implementation, my cross-validation is kind of backward looking. Well, use time series cross-validation to reduce forecasting volatility.
(res1 <- ahead::mlf(AirPassengers, h=25L, lags=20L, fit_func=glmnet::cv.glmnet, stack=FALSE)) (res2 <- ahead::mlf(AirPassengers, h=25L, lags=20L, fit_func=glmnet::cv.glmnet, stack=TRUE)) (res3 <- ahead::mlf(USAccDeaths, h=25L, lags=20L, fit_func=glmnet::cv.glmnet, stack=TRUE)) (res4 <- ahead::mlf(USAccDeaths, h=25L, lags=20L, fit_func=glmnet::cv.glmnet, stack=FALSE)) par(mfrow=c(1, 2)) plot(res1, main="Conformal ML without stacking") plot(res2, main="Conformal ML with stacking")
par(mfrow=c(1, 2)) plot(res3, main="Conformal ML with stacking") plot(res4, main="Conformal ML without stacking")
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.