modeltime.gluonts icon indicating copy to clipboard operation
modeltime.gluonts copied to clipboard

Why does my saved calibration data load as environment variable and not, when I save and reload from rds?

Open leonhGeis opened this issue 1 year ago • 0 comments

In order to save my gluonts calibration tbl, I want to be able to reload the saved calibration table from an rds temp file.

As I know, there are convenience functions to save gluonts models, but I rather want to save the calibrated data. This is mainly because, when I refit the model, gluonts is changing the forecast values, as they are probabilistic, every time. To get a model history, I want to save calibration data and the refit table from the ex ante.

I tried to do this, but every time, I want to reload the saved calibration from rds, the forecasting function fails with an error.

I supplied a reprex to illustrate the issue:

library(tidymodels)
library(tidyverse)
library(timetk)
library(reticulate
)
Sys.setenv(GLUONTS_PYTHON = "/YOUR_PATH.../r-miniconda/envs/r-reticulate/bin/python3.8"
)
library(modeltime.gluonts
)
HORIZON <- 24*7
data <- m4_hourly %>%
	select(id, date, value
		) %>%
	group_by(id
		)%>%
	mutate(value = standardize_vec(value)
		) %>%
	ungroup()
new_data <- data %>%
	group_by(id
		) %>%
	future_frame(.length_out = HORIZON
		) %>%
	ungroup()
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)
set.seed(223
)
calibration_tbl <- 
	modeltime_table(
		model_fit_nbeats_ensemble
		) %>%
	modeltime_calibrate(
		new_data = data, id = "id"
		) 
# save calibrated model
artifacts <- list(
	calibration = calibration_tbl   # ---> USING THIS AS FC-INPUT WORKS
)
artifacts %>%
	write_rds("artifacts_list.rds"
	)
artifacts <- read_rds("artifacts_list.rds" # ---> GET SAVED MODEL FAILS
)
# forecast test
set.seed(273
)
forecast <- artifacts$calibration  %>% 
	modeltime_forecast(
		new_data      = new_data,
		actual_data   = data,
		keep_data     = TRUE
	)

leonhGeis avatar Dec 01 '22 11:12 leonhGeis