knitr::opts_chunk$set( collapse = TRUE, comment = "#>", warning = FALSE, message = FALSE, fig.align = "center", out.width = "90%", fig.width = 7, fig.height = 5 )
Let's get started by making an N-BEATS ENSEMBLE. N-BEATS is a powerful algorithm that has shown exceptional results^1^. In a matter of minutes, you'll generate the 4 forecasts shown below. If you'd like to improve your time series forecasting abilities, then please take my High-Performance Time Series Course.
library(tidyverse) library(modeltime) modeltime_forecast_tbl <- read_rds("modeltime_forecast_tbl.rds") modeltime_forecast_tbl %>% plot_modeltime_forecast( .facet_ncol = 2, .facet_scales = "free", .interactive = FALSE )
Load the following libraries.
library(modeltime.gluonts) library(tidymodels) library(tidyverse) library(timetk)
Next, set up the Python Environment with install_gluonts()
. You only need to run this one time, and then you are good to go.
install_gluonts()
Python Environment setup is always fun. Here are a few recommendations if you run into an issue.
Check to make sure Conda is available using reticulate::conda_version()
. If no conda version is returned, then use reticulate::install_miniconda()
to install. Then (re-)run install_gluonts()
.
Check if GluonTS (Python) is available using reticulate::py_module_available("gluonts")
. If this returns TRUE
, then your installation has succeeded in building the environment, but you may have other issues like missing C++ build tools (next).
Windows 10 error: Microsoft Visual C++ is required. Here are the instructions for installing the C++ tools needed.
Other installation issues. Please file a GitHub issue here.
We'll use the m4_hourly
dataset, which contains 4 hourly time series.
data <- m4_hourly %>% select(id, date, value) %>% group_by(id) %>% mutate(value = standardize_vec(value)) %>% ungroup() data
We'll create the forecast region using future_frame()
. We are forecasting 1 week (24x7 timestamps) into the future.
HORIZON <- 24*7 new_data <- data %>% group_by(id) %>% future_frame(.length_out = HORIZON) %>% ungroup() new_data
We'll create a model using the N-BEATS ENSEMBLE algorithm using the nbeats()
function.
gluonts_nbeats_ensemble
.lookback_length
. We can include more sub-models adjusting the bagging_size
. model_fit_nbeats_ensemble <- nbeats( id = "id", freq = "H", prediction_length = HORIZON, lookback_length = c(HORIZON, 4*HORIZON), epochs = 5, num_batches_per_epoch = 15, batch_size = 1 ) %>% set_engine("gluonts_nbeats_ensemble") %>% fit(value ~ date + id, data)
An NBEATS ENSEMBLE is produced.
model_fit_nbeats_ensemble
knitr::include_graphics("nbeats_model.jpg")
With a model in hand, we can simply follow the Modeltime Workflow to generate a forecast for the multiple time series groups.
modeltime_forecast_tbl <- modeltime_table( model_fit_nbeats_ensemble ) %>% modeltime_forecast( new_data = new_data, actual_data = data, keep_data = TRUE ) %>% group_by(id)
We can visualize the forecast with plot_modeltime_forecast()
.
modeltime_forecast_tbl %>% plot_modeltime_forecast( .conf_interval_show = FALSE, .facet_ncol = 2, .facet_scales = "free", .interactive = FALSE )
GluonTS models will need to "serialized" (a fancy word for saved to a directory that contains the recipe for recreating the models). To save the models, use save_gluonts_model()
.
model_fit_nbeats_ensemble %>% save_gluonts_model(path = "nbeats_ensemble_model", overwrite = TRUE)
You can reload the model into R using load_gluonts_model()
.
model_fit_nbeats_ensemble <- load_gluonts_model("nbeats_ensemble_model")
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.
I teach how to build a HPTFS System in my High-Performance Time Series Forecasting Course. By taking my course, you will learn:
Modeltime
- 30+ Models (Prophet, ARIMA, XGBoost, Random Forest, & many more)GluonTS
(Competition Winners)Unlock the High-Performance Time Series Forecasting Course
My Talk on High-Performance Time Series Forecasting
1: N-BEATS: Neural basis expansion analysis for interpretable time series forecasting https://arxiv.org/abs/1905.10437
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.