Getting Started with Modeltime GluonTS

Making an N-Beats Ensemble

Let’s get started by making an N-BEATS ENSEMBLE. N-BEATS is a powerful algorithm that has shown exceptional results1. 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.

Libraries

Load the following libraries.

library(modeltime.gluonts)
library(tidymodels)
library(tidyverse)
library(timetk)

Installation

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()

Troubleshooting Installation

Python Environment setup is always fun. Here are a few recommendations if you run into an issue.

Time Series Data

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
#> # A tibble: 3,060 x 3
#>    id    date                  value
#>    <fct> <dttm>                <dbl>
#>  1 H10   2015-07-01 12:00:00  1.98  
#>  2 H10   2015-07-01 13:00:00  1.95  
#>  3 H10   2015-07-01 14:00:00  1.78  
#>  4 H10   2015-07-01 15:00:00  1.62  
#>  5 H10   2015-07-01 16:00:00  1.33  
#>  6 H10   2015-07-01 17:00:00  1.16  
#>  7 H10   2015-07-01 18:00:00  0.682 
#>  8 H10   2015-07-01 19:00:00  0.0876
#>  9 H10   2015-07-01 20:00:00 -0.252 
#> 10 H10   2015-07-01 21:00:00 -0.591 
#> # … with 3,050 more rows

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
#> # A tibble: 672 x 2
#>    id    date               
#>    <fct> <dttm>             
#>  1 H10   2015-07-30 16:00:00
#>  2 H10   2015-07-30 17:00:00
#>  3 H10   2015-07-30 18:00:00
#>  4 H10   2015-07-30 19:00:00
#>  5 H10   2015-07-30 20:00:00
#>  6 H10   2015-07-30 21:00:00
#>  7 H10   2015-07-30 22:00:00
#>  8 H10   2015-07-30 23:00:00
#>  9 H10   2015-07-31 00:00:00
#> 10 H10   2015-07-31 01:00:00
#> # … with 662 more rows

Making an N-Beats Ensemble Model

We’ll create a model using the N-BEATS ENSEMBLE algorithm using the nbeats() function.

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

Forecasting

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
  )

Saving and Loading Models

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")

Learning More

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:

Unlock the High-Performance Time Series Forecasting Course

My Talk on High-Performance Time Series Forecasting

References

1: N-BEATS: Neural basis expansion analysis for interpretable time series forecasting https://arxiv.org/abs/1905.10437