Why won't my Prophet / Fable model with holidays forecast in a new R environment?

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

Temporary connection problem.?Just checked and the file is there and importable. There are holidays in the model, is myholidays an addition?

I am wondering this too.... the only way I was able to get it to work was to "write" myholidays as a global variable by doing this in my code. It's just weird that it cannot see it in the parent environment but can see it in the global? Could the type of environment (parent/global) matter?

I was hoping that the bundle package would fix things, but no such luck. only <<- seems to fix it.

makepredictions <- function(myfable,myholidays,data){
myholidays <<- myholidays
}

I see these holidays

testmodel[3]$prophetb[[1]]$fit$model$holidays[2] |> unique()
holiday
1               New.Years.Day
2   Martin.Luther.King.Jr.Day
3        Washingtons.Birthday
4                Memorial.Day
5            Independence.Day
6                   Labor.Day
7                Columbus.Day
8                Veterans.Day
9                Thanksgiving
10              Christmas.Day
71                  DragonCon
79                    PGATour
85                SportsEvent
87               MusicMIdtown
92                NewYearsEve
101               MinorSports

What additional holidays are in myholidays?

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.