These are the full instructions to get
modeltime.gluonts installed, the python dependencies installed, and to connect R and Python together so you can forecast with amazing accuracy.
There are 2 key components to installing
Download the R-Package,
modeltime.gluonts. This installs the R-Bindings, which allows you to interface with GluonTS.
Set up the Python Environment so
modeltime.gluonts can connect to the
gluonts python package.
Modeltime GluonTS is maintained on GitHub and can be installed using:
We’ve provided a helper function,
install_gluonts() to prepare and bind to a python environment containing
gluonts and the required python packages.
modeltime.gluonts, the package will include this environment in it’s search process.
# GluonTS Installation - Run 1st time install_gluonts(fresh_install = FALSE, include_pytorch = FALSE)
TRUE, this removes any previous “r-gluonts” environments, which can help in the case of errors. Caution: If you have added packages to this environment after a prior install, these packages will be removed.
TRUE, will install the optional Pytorch and Pytorch Lightning dependencies for use with the “torch” engine, which is available in models such as
Restart your R session (if in RStudio, close and re-open). Then try this example.
library(modeltime.gluonts) library(tidymodels) library(tidyverse) # Fit a GluonTS DeepAR Model model_fit_deepar <- deep_ar( id = "id", freq = "M", prediction_length = 24, lookback_length = 48, epochs = 5 ) %>% set_engine("gluonts_deepar") %>% fit(value ~ ., training(m750_splits)) # Forecast with 95% Confidence Interval modeltime_table( model_fit_deepar ) %>% modeltime_calibrate(new_data = testing(m750_splits)) %>% modeltime_forecast( new_data = testing(m750_splits), actual_data = m750, conf_interval = 0.95 ) %>% plot_modeltime_forecast(.interactive = FALSE)
Modeltime GluonTS 0.3.0 introduced new features that are only available in GluonTS >= 0.8.0. To incorporate these features, we now need to upgrade GluonTS.
Next, we recommend running
fresh_install = TRUE. This will completely replace your previous “r-gluonts” python environment with a new one. It’s designed to help reduce the likelihood of errors during the upgrading process.
install_gluonts(fresh_install = TRUE)
Optionally, you can add the Pytorch Backend (new feature) with:
install_gluonts(fresh_install = TRUE, include_pytorch = TRUE)
Python Environment setup is always fun. Here are a few recommendations if you run into an issue.
Check to make sure Conda or Miniconda is available using
reticulate::conda_version(). If no conda version is returned, then use
reticulate::install_miniconda() to install Miniconda (recommended vs full Aniconda). Then (re-)run
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.
modeltime.gluonts >= 0.3.0 has the following minimum python environment requirements. We provide a helper function
install_gluonts() to help set these up.
Optional: Dependencies for Pytorch:
Test Environment: These package versions are tested on Windows, Mac, and Linux systems and can be used to set up an environment:
That’s it. If you have these inside a Conda or Virtual Environment, then you can use
There are 2 main ways to connect an python environment to
Default GluonTS Environment Setup: Most proven. Modeltime GluonTS creates an ‘r-gluonts’ environment with python package versions that were tested.
Custom GluonTS Environment Setup: Most flexible. You create the environment, you ensure that dependency requirements are met, and you define how to connect to it.
This process uses the Conda Package Manager to set up a new conda environment called “r-gluonts”. It’s quick and easy, and most importantly the package versions that are selected all work together (I test them when developing). And, you can modify it once created.
This downloads the “GluonTS Stack” that the
modeltime.gluonts package is tested using and places it into a Conda environment named “r-gluonts”. When you run
library("modeltime.gluonts"), it attempts to use the “r-gluonts” environment.
You can modify this environment, adding more python libraries as needed.
Each time you load
library(modeltime.gluonts), the package will bind to the “r-gluonts” python environment by default. If found, it will automatically bind to this environment.
Make sure that the environment has been changed to the default ‘r-gluonts’ environment. We can see that
r-gluonts environment is being used in the python path.
It’s quite possible you may have a Virtual Environment or different Conda Environment that you would prefer to use. This is possible by setting a System Environment Variable named ‘GLUONTS_PYTHON’ before running
You can create an environment containing the python packages needed.
This locates the path to the python executable for the ‘my_gluonts_env’ environment that we just created.
Set the system environment variable named ‘GLUONTS_PYTHON’ with the path to the python executable. Once this is set, loading
library(modeltime.gluonts) will use this path to activate the environment.
Sys.setenv(GLUONTS_PYTHON = my_gluonts_env_python_path)
Verify it’s been set.
Sys.getenv("GLUONTS_PYTHON") #> "/Users/mdancho/Library/r-miniconda/envs/my_gluonts_env/bin/python"
library(modeltime.gluonts) now binds to the custom environment.
Make sure that the environment has been changed to the default reticulate environment. We can see that environment is being used. If setting your python environment was done properly, you should see
my_gluonts_env in the Python Path.
Troubleshooting: If the incorrect environment is shown, simply restart your R Session,