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

How can I reuse a fable model after fitting the model especially when used in a *new environment*? #379

Open
lbui30 opened this issue Nov 28, 2022 · 2 comments

Comments

@lbui30
Copy link

lbui30 commented Nov 28, 2022

I have just recently started using fable and fable.prophet when I have run into a very odd situation. As you can see from the code below, the model runs fine when everything is in the global environment but fails to detect or use the exogenous regressors when the code is in a local environment (like a function):

This code works:

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")
head(myforcast)
#> # A fable: 4 x 6 [7D]
#> # Key:     battalion, station_id, .model [1]
#>   battalion station_id .model   incident_date   callvolume .mean
#>   <chr>     <chr>      <chr>    <date>              <dist> <dbl>
#> 1 5         04         prophetb 2022-11-20    sample[5000]  47.8
#> 2 5         04         prophetb 2022-11-27    sample[5000]  37.1
#> 3 5         04         prophetb 2022-12-04    sample[5000]  47.6
#> 4 5         04         prophetb 2022-12-11    sample[5000]  47.5

But this code doesn't work:

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(...)

P.S. While the full context of my problem can be found here, I am new to the r-community so I am not sure if my post has shown up yet: https://community.rstudio.com/t/why-wont-my-prophet-fable-model-with-holidays-forecast-in-a-new-r-environment/153767/1

@mitchelloharawild
Copy link
Member

Thanks for reporting this issue - it should work but perhaps there is a problem that needs fixing here. I'll investigate further when I have the time.

@lbui30
Copy link
Author

lbui30 commented Nov 28, 2022

Thanks for the quick reply mitchelloharawild. As soon as the Rstudio post works I will let you know. Also, I suspect the bundle package might be fixed, but I am still in the process of getting it working correctly. https://github.com/rstudio/bundle.

@mitchelloharawild if it does sound like something needs fixing, please let me know as I don't mind reaching out to the prophet package developers if the issue is on their end. :)

Keep you updated,

Lbui

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

2 participants