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

blend_predictions does not support gam models with a custom formula #193

Closed
era127 opened this issue Mar 12, 2023 · 4 comments · Fixed by #196
Closed

blend_predictions does not support gam models with a custom formula #193

era127 opened this issue Mar 12, 2023 · 4 comments · Fixed by #196
Labels
bug an unexpected problem or unintended behavior

Comments

@era127
Copy link

era127 commented Mar 12, 2023

It does not seem to be possible to use a gam model with workflowset.

When using a gam model in a workflowset, the custom formula is specified using the formula argument of
workflowsets::update_workflow_model()

However stack_workflow() only uses extract_spec_parsnip() and extract_preprocessor() so it never saves the custom formula into the candidate.

library(magrittr)
# generate data
x <- data.frame(date = seq(from = as.POSIXct("2000-01-01"), to = as.POSIXct("2020-01-01"), by = "DSTday"))
x <- cbind(x, x = runif(n = NROW(x)), weekday = lubridate::wday(x$date))
x$y <- dplyr::lead(x$x, n = 1) + sin(as.numeric(x$weekday)) / 10
#
# recipe
p <- recipes::recipe(x, formula = y ~ date + x + weekday) %>%
  recipes::update_role(date, new_role = "date")
#
# sliding resampling
r <- rsample::sliding_period(data = x, index = date, period = "month", lookback = 120, assess_stop = 12, step = 12)
# workflow sets for lm and gam formula
m.lm <- parsnip::linear_reg()
m.gam <- parsnip::gen_additive_mod(mode = "regression")
ws <- workflowsets::workflow_set(preproc = list(p = p), models = list(lm = m.lm, gam = m.gam), cross = T) %>%
  workflowsets::update_workflow_model(id = "p_gam", spec = m.gam, formula = y ~ s(x) + s(weekday, bs = "cc", k = 7))
wfs <- workflowsets::workflow_map(
  object = ws,
  fn = "fit_resamples",
  resamples = r,
  control = tune::control_resamples(save_pred = T, save_workflow = T)
)

# stacking
s <- stacks::stacks() %>%
  stacks::add_candidates(wfs) %>%
  stacks::blend_predictions(penalty = 1e-8, mixture = 0, times = 2)

stacks::fit_members(s)
# Error in { :
#     task 2 failed - "`fit()` must be used with GAM models (due to its use of formulas)."
@simonpcouch
Copy link
Collaborator

Thank you for the issue! This is indeed a bug. I think we're due for a good push on mgcv docs and infrastructure, hopefully soon than later—a fix for this will come then.

@simonpcouch simonpcouch added the bug an unexpected problem or unintended behavior label Mar 13, 2023
@era127
Copy link
Author

era127 commented Mar 14, 2023

I was able to work around the issue by removing the call to stack_workflow(). That function appears to just create a 'minimal' workflow from an existing workflow, and I think it drops objects for the formula which are required.

@simonpcouch
Copy link
Collaborator

This is now addressed in the dev version of stacks! Thanks for bringing this up.

@github-actions
Copy link

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants