tidyverts / tidyverts/fable

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

Open
#379 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
588
Forks
71
Avg merge
1h 58m
Merged PRs (30d)
1

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the fabletools::forecast() call in the provided function and trace how the saved fable.prophet model evaluates the myholidays regressor outside the global environment. Compare the global and local execution paths, then verify that forecasting from a locally defined environment resolves the exogenous variable and produces the expected four-week output.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.