Getting Started with Modeltime GluonTS

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE, 
  message = FALSE,
  fig.align = "center",
  out.width = "90%",
  fig.width = 7,
  fig.height = 5
)

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

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

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

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
knitr::include_graphics("nbeats_model.jpg")

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 {#references}

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



Try the modeltime.gluonts package in your browser

Any scripts or data that you put into this service are public.

modeltime.gluonts documentation built on Jan. 8, 2021, 2:23 a.m.