knitr::opts_chunk$set( # collapse = TRUE, message = FALSE, warning = FALSE, paged.print = FALSE, comment = "#>", fig.width = 8, fig.height = 4.5, fig.align = 'center', out.width='95%' )
Ensemble Algorithms for Time Series Forecasting with Modeltime
modeltime extension that that implements ensemble forecasting methods including model averaging, weighted averaging, and stacking. Let's go through a guided tour to kick the tires on
We'll perform the simplest type of forecasting: Using a simple average of the forecasted models.
modeltime.ensemble has capabilities for more sophisticated model ensembling using:
Load libraries to complete this short tutorial.
# Time Series ML library(tidymodels) library(modeltime) library(modeltime.ensemble) # Core library(tidyverse) library(timetk) interactive <- FALSE
We'll use the
m750 dataset that comes with
modeltime.ensemble. We can visualize the dataset.
m750 %>% plot_time_series(date, value, .color_var = id, .interactive = interactive)
We'll split into a training and testing set.
splits <- time_series_split(m750, assess = "2 years", cumulative = TRUE) splits %>% tk_time_series_cv_plan() %>% plot_time_series_cv_plan(date, value, .interactive = interactive)
Once the data has been collected, we can move into modeling.
We'll create a Feature Engineering Recipe that can be applied to the data to create features that machine learning models can key in on. This will be most useful for the Elastic Net (Model 3).
recipe_spec <- recipe(value ~ date, training(splits)) %>% step_timeseries_signature(date) %>% step_rm(matches("(.iso$)|(.xts$)")) %>% step_normalize(matches("(index.num$)|(_year$)")) %>% step_dummy(all_nominal()) %>% step_fourier(date, K = 1, period = 12) recipe_spec %>% prep() %>% juice()
First, we'll make an ARIMA model using Auto ARIMA.
model_spec_arima <- arima_reg() %>% set_engine("auto_arima") wflw_fit_arima <- workflow() %>% add_model(model_spec_arima) %>% add_recipe(recipe_spec %>% step_rm(all_predictors(), -date)) %>% fit(training(splits))
Next, we'll make a Prophet Model.
model_spec_prophet <- prophet_reg() %>% set_engine("prophet") wflw_fit_prophet <- workflow() %>% add_model(model_spec_prophet) %>% add_recipe(recipe_spec %>% step_rm(all_predictors(), -date)) %>% fit(training(splits))
Third, we'll make an Elastic Net Model using
model_spec_glmnet <- linear_reg( mixture = 0.9, penalty = 4.36e-6 ) %>% set_engine("glmnet") wflw_fit_glmnet <- workflow() %>% add_model(model_spec_glmnet) %>% add_recipe(recipe_spec %>% step_rm(date)) %>% fit(training(splits))
With the models created, we can can create an Ensemble Average Model using a simple Mean Average.
Create a Modeltime Table using the
m750_models <- modeltime_table( wflw_fit_arima, wflw_fit_prophet, wflw_fit_glmnet ) m750_models
ensemble_average() to turn that Modeltime Table into a Modeltime Ensemble. This is a fitted ensemble specification containing the ingredients to forecast future data and be refitted on data sets using the 3 submodels.
ensemble_fit <- m750_models %>% ensemble_average(type = "mean") ensemble_fit
To forecast, just follow the Modeltime Workflow.
# Calibration calibration_tbl <- modeltime_table( ensemble_fit ) %>% modeltime_calibrate(testing(m750_splits)) # Forecast vs Test Set calibration_tbl %>% modeltime_forecast( new_data = testing(m750_splits), actual_data = m750 ) %>% plot_modeltime_forecast(.interactive = interactive)
Once satisfied with our ensemble model, we can
modeltime_refit() on the full data set and forecast forward gaining the confidence intervals in the process.
refit_tbl <- calibration_tbl %>% modeltime_refit(m750) refit_tbl %>% modeltime_forecast( h = "2 years", actual_data = m750 ) %>% plot_modeltime_forecast(.interactive = interactive)
This was a very short tutorial on the simplest type of forecasting, but there's a lot more to learn.
Become the forecasting expert for your organization
Time series is changing. Businesses now need 10,000+ time series forecasts every day. This is what I call a High-Performance Time Series Forecasting System (HPTSF) - Accurate, Robust, and Scalable Forecasting.
High-Performance Forecasting Systems will save companies by improving accuracy and scalability. Imagine what will happen to your career if you can provide your organization a "High-Performance Time Series Forecasting System" (HPTSF System).
I teach how to build a HPTFS System in my High-Performance Time Series Forecasting Course. You will learn:
Modeltime- 30+ Models (Prophet, ARIMA, XGBoost, Random Forest, & many more)
Become the Time Series Expert for your organization.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.