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

Forecasting with modeltime.h2o made easy! This short tutorial shows how you can use:
H2O AutoML for forecasting implemented via automl_reg(). This function trains and cross-validates multiple machine learning and deep learning models (XGBoost GBM, GLMs, Random Forest, GBMs...) and then trains two Stacked Ensembled models, one of all the models, and one of only the best models of each kind. Finally, the best model is selected based on a stopping metric. And we take care of all this for you!
Save & Load Models functionality to ensure the persistence of your models.
Load the following libraries:
library(tidymodels) library(modeltime.h2o) library(tidyverse) library(timetk)
Next, we load the walmart_sales_weekly data containing 7 time series and visualize them using the timetk::plot_time_series() function.
data_tbl <- walmart_sales_weekly %>% select(id, Date, Weekly_Sales) data_tbl %>% group_by(id) %>% plot_time_series( .date_var = Date, .value = Weekly_Sales, .facet_ncol = 2, .smooth = F, .interactive = F )
Then, we separate the data with the time_series_split() function and generate a training dataset and a test one.
splits <- time_series_split(data_tbl, assess = "3 month", cumulative = TRUE) recipe_spec <- recipe(Weekly_Sales ~ ., data = training(splits)) %>% step_timeseries_signature(Date) train_tbl <- training(splits) %>% bake(prep(recipe_spec), .) test_tbl <- testing(splits) %>% bake(prep(recipe_spec), .)
In order to correctly use modeltime.h2o, it is necessary to connect to an H2O cluster through the h2o.init() function. You can find more information on how to set up the cluster by typing ?h2o.init or by visiting the official site.
# Initialize H2O h2o.init( nthreads = -1, ip = 'localhost', port = 54321 ) # Optional - Set H2O No Progress to remove progress bars h2o.no_progress()
Now comes the fun part! We define our model specification with the automl_reg() function and pass the arguments through the engine:
model_spec <- automl_reg(mode = 'regression') %>% set_engine( engine = 'h2o', max_runtime_secs = 5, max_runtime_secs_per_model = 3, max_models = 3, nfolds = 5, exclude_algos = c("DeepLearning"), verbosity = NULL, seed = 786 ) model_spec
Next, let's train the model with fit()!
model_fitted <- model_spec %>% fit(Weekly_Sales ~ ., data = train_tbl) model_fitted
The best models are stored in the leaderbord and by default the one with the best metric with which you have decided to sort the leaderbord is selected (this behavior can be controlled with the sort_metric parameter passed through set_engine. For more information see ?h2o.automl. By default, it is sorted by the mean_residual_deviance). To list the models created during training that have finally been stored in the leaderbord you can use the automl_leaderbord function as follows:
automl_leaderboard(model_fitted)
To change the default selected model (remember, the first one sorted according to the selected metric) you can do it with the automl_update_model() function as follows (do not run the following example as the model id name will have changed as there is randomness in the process):
automl_update_model(model_fitted, model_id = "StackedEnsemble_AllModels_AutoML_20210319_204825")
Finally, we predict() on the test dataset:
predict(model_fitted, test_tbl)
Once we have our fitted model, we can follow the Modeltime Workflow:
Add fitted models to a Model Table.
Calibrate the models to a testing set.
Perform Testing Set Forecast Evaluation & Accuracy Evaluation.
Refit the models to Full Dataset & Forecast Forward
First, we create the model table:
modeltime_tbl <- modeltime_table( model_fitted ) modeltime_tbl
Next, we calibrate to the testing set and visualize the forecasts:
modeltime_tbl %>% modeltime_calibrate(test_tbl) %>% modeltime_forecast( new_data = test_tbl, actual_data = data_tbl, keep_data = TRUE ) %>% group_by(id) %>% plot_modeltime_forecast( .facet_ncol = 2, .interactive = FALSE )
Before using refit on our dataset, let's prepare our data. We create data_prepared_tbl which represents the complete dataset (the union of train and test) with the variables created with the recipe named recipe_spec. Subsequently, we create the dataset future_prepared_tbl that represents the dataset with the future data to one year and the required variables.
data_prepared_tbl <- bind_rows(train_tbl, test_tbl) future_tbl <- data_prepared_tbl %>% group_by(id) %>% future_frame(.length_out = "1 year") %>% ungroup() future_prepared_tbl <- bake(prep(recipe_spec), future_tbl)
Finally, we use forecast in our future dataset and visualize the results once we had reffited.
refit_tbl <- modeltime_tbl %>% modeltime_refit(data_prepared_tbl) refit_tbl %>% modeltime_forecast( new_data = future_prepared_tbl, actual_data = data_prepared_tbl, keep_data = TRUE ) %>% group_by(id) %>% plot_modeltime_forecast( .facet_ncol = 2, .interactive = FALSE )
We can likely do better than this if we train longer but really good for a quick example!
H2O 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_h2o_model().
model_fitted %>% save_h2o_model(path = "../model_fitted", overwrite = TRUE)
You can reload the model into R using load_h2o_model().
model_h2o <- load_h2o_model(path = "../model_fitted/")
Finally, once we have saved the specific models that we want to keep, we shutdown the H2O cluster.
h2o.shutdown(prompt = FALSE)
Need to learn high-performance time series forecasting?
Become the forecasting expert for your organization
High-Performance Time Series Course
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)GluonTS (Competition Winners)Become the Time Series Expert for your organization.
Take the High-Performance Time Series Forecasting Course
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.