I am stuck with the fable package and prophet. I am hoping that developer or an expert in forecasting/modeling still hangs around the community. I cannot figure out why I cannot use a fable model I trained outside of its global environment.
library(config)
library(DBI)
library(odbc)
library(tibble)
library(dplyr)
library(readr)
library(feasts)
#> Loading required package: fabletools
library(fable)
library(prophet)
#> Loading required package: Rcpp
#> Loading required package: rlang
library(fable.prophet)
#> Attaching package: 'fable.prophet'
#> The following object is masked from 'package:prophet':
#> prophet
myprepfunction <- function() {
ts2 <- readRDS(url("https://github.com/lbui30/reprexdata/blob/main/ts2.RDS?raw=true"))
testmodel <- readRDS(url("https://github.com/lbui30/reprexdata/blob/main/testmodel.RDS?raw=true"))
sampledata <- ts2 %>% select(!any_of(c("ds"))) %>% filter(station_id %in% c("04"))
# simplifying for reprex....
# myholidays <- create_holidays(sampledata)
myholidays <- readRDS(url("https://github.com/lbui30/reprexdata/blob/main/myholidays.RDS?raw=true"))
myforcast <- testmodel %>%
forecast(h = "4 weeks")
return(myforcast)
}
forecast = myprepfunction()
#> Error in `mutate()`:
#> ! Problem while computing `prophetb = (function (object, ...) ...`.
#> Caused by error in `value[[3L]]()`:
#> ! object 'myholidays' not found
#> Unable to compute required variables from provided `new_data`.
#> Does your model require extra variables to produce forecasts?
#> Backtrace:
#> ▆
#> 1. ├─global myprepfunction()
#> 2. │ └─testmodel %>% forecast(h = "4 weeks")
#> 3. ├─fabletools::forecast(., h = "4 weeks")
#> 4. ├─fabletools:::forecast.mdl_df(., h = "4 weeks")
#> 5. │ └─dplyr::mutate_at(...)
#> 6. │ ├─dplyr::mutate(.tbl, !!!funs)
#> 7. │ └─dplyr:::mutate.data.frame(.tbl, !!!funs)
#> 8. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), caller_env = caller_env())
#> 9. │ ├─base::withCallingHandlers(...)
#> 10. │ └─mask$eval_all_mutate(quo)
#> 11. ├─fabletools (local) `<fn>`(...)
#> 12. └─fabletools:::forecast.lst_mdl(...)
#> 13. └─fabletools:::mapply_maybe_parallel(...)
#> 14. └─base::mapply(FUN = .f, ..., MoreArgs = MoreArgs, SIMPLIFY = SIMPLIFY)
#> 15. ├─fabletools (local) `<fn>`(dots[[1L]][[1L]], dots[[2L]][[1L]], h = "4 weeks", point_forecast = `<named list>`)
#> 16. └─fabletools:::forecast.mdl_ts(...)
#> 17. └─base::tryCatch(...)
#> 18. └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#> 19. ├─base (local) tryCatchOne(...)
#> 20. │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#> 21. └─base (local) tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
#> 22. └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#> 23. └─value[[3L]](cond)
#> 24. └─rlang::abort(...)
But When run my code line by line outside of the function... there are no errors
ts2 <- readRDS(url("https://github.com/lbui30/reprexdata/blob/main/ts2.RDS?raw=true"))
testmodel <- readRDS(url("https://github.com/lbui30/reprexdata/blob/main/testmodel.RDS?raw=true"))
sampledata <- ts2 %>% select(!any_of(c("ds"))) %>% filter(station_id %in% c("04"))
myholidays <- readRDS(url("https://github.com/lbui30/reprexdata/blob/main/myholidays.RDS?raw=true"))
myforcast <- testmodel %>%
forecast(h = "4 weeks")
I know from reading fpp3 and the prophet documentation that exogenous regressors are tricky. However, in both cases I am providing the extragenous regressor upfront in the same environment as the model.
Does anyone have any idea what is going on or why this is happening?
If it helps this is the code I used to train the model:
ts2 <- readRDS(url("https://github.com/lbui30/reprexdata/blob/main/ts2.RDS?raw=true"))
myholidays <- readRDS(url("https://github.com/lbui30/reprexdata/blob/main/myholidays.RDS?raw=true"))
testtraindata <- ts2 %>% select(!any_of(c("ds"))) %>% filter(station_id %in% c("04"))
testmodel <- model(.safely=TRUE,.data = testtraindata,
prophetb = prophet(callvolume ~ holiday(myholidays, prior_scale = 30))
)
saveRDS(ts2,"ts2.RDS")
saveRDS(myholidays,"myholidays.RDS")
saveRDS(testmodel,"testmodel.RDS")
Full reprex can be found here: My full reproducible code · GitHub