cmu-delphi / epipredict

Tools for building predictive models in epidemiology.
https://cmu-delphi.github.io/epipredict/
Other
8 stars 8 forks source link

Export/scope functions used in canned forecasters #341

Open brookslogan opened 2 weeks ago

brookslogan commented 2 weeks ago

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:

dajmcdon commented 2 weeks ago

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.