Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
leonhGeis opened this issue Dec 1, 2022 · 0 comments

Comments

@leonhGeis
Copy link

leonhGeis commented Dec 1, 2022

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
	)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant