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

Export/scope functions used in canned forecasters #341

Open
brookslogan opened this issue Jun 18, 2024 · 1 comment
Open

Export/scope functions used in canned forecasters #341

brookslogan opened this issue Jun 18, 2024 · 1 comment

Comments

@brookslogan
Copy link
Contributor

Suppose we want to tweak arx_forecaster(). We probably can't just preprocess the inputs or postprocess the outputs; we want to modify the epi_workflow involved. It seems natural to want to copy-paste its implementation, maybe the arx_fcast_workflow() implementation, and maybe maybe the arx_args_list() implementation. One could potentially do without the latter two with the epi_workflow modification functions, but we don't use those much / at all in our own code/examples, so it might actually feel easier / more natural to do even more copy-pasting rather than use them.

One problem/friction with the above:

library(epipredict)
#> Loading required package: epiprocess
#> 
#> Attaching package: 'epiprocess'
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> Loading required package: parsnip
# copy-paste arx_forecaster:
arx_forecaster2 <- function (epi_data, outcome, predictors = outcome, trainer = parsnip::linear_reg(), 
  args_list = arx_args_list()) {
  if (!is_regression(trainer)) {
      cli::cli_abort("`trainer` must be a {.pkg parsnip} model of mode 'regression'.")
  }
  wf <- arx_fcast_epi_workflow(epi_data, outcome, predictors, 
      trainer, args_list)
  wf <- generics::fit(wf, epi_data)
  preds <- forecast(wf, fill_locf = TRUE, n_recent = args_list$nafill_buffer, 
      forecast_date = args_list$forecast_date %||% max(epi_data$time_value)) %>% 
      tibble::as_tibble() %>% dplyr::select(-time_value)
  structure(list(predictions = preds, epi_workflow = wf, metadata = list(training = attr(epi_data, 
      "metadata"), forecast_created = Sys.time())), class = c("arx_fcast", 
      "canned_epipred"))
}

# copy-paste examples from ?arx_forecaster:
jhu <- case_death_rate_subset %>%
  dplyr::filter(time_value >= as.Date("2021-12-01"))

out <- arx_forecaster2(
  jhu, "death_rate",
  c("case_rate", "death_rate")
)
#> Error in is_regression(trainer): could not find function "is_regression"

Created on 2024-06-18 with reprex v2.0.2

is_regression is a internal (non-exported) function from epipredict. There may be other internal functions involved, or imported, non-exported functions from other packages, that can cause similar issues. It may be helpful to:

  • Export all currently-internal functions used by canned forecasters/workflows/argslists.
  • Use :: or re-export external functions used by canned forecaster/workflows/argslists.
@dajmcdon
Copy link
Contributor

I think that the intention here is to use a combination of arx_forecast_workflow() and the various functions that modify the workflow (adjust_*(), update(), etc).

Do you have a use case where you tried to do this and couldn't? Or can we try to do it? It would make a good vignette or precipitate the creation of additional functions to allow the flexibility you need.

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