-
Notifications
You must be signed in to change notification settings - Fork 0
add hub baseline and ensemble functions #11
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
Merged
sbidari
merged 18 commits into
main
from
5-create-a-hub-ensemble-forecasts-given-dataset-type-and-disease
Aug 27, 2025
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
91717ac
add func for baselines
sbidari 2c6dbc4
r cmd check
sbidari 6591fd3
Apply suggestions from code review
sbidari bacfe41
update description
sbidari db90e55
Merge branch '4-create-a-hub-formatted-quantiles-baseline-forecasts-g…
sbidari 82ef81e
missed comma
sbidari fbfc1f8
use forecasttools for file read/write
sbidari f402a1f
add ensembler
sbidari 96d7cf9
docstrings
sbidari 11ddde1
copilot review suggestions
sbidari 294aaf1
changes for easier testing
sbidari c39f5e1
Apply suggestions from code review
sbidari c66df86
Apply suggestions from code review
sbidari deb324e
cleanup, formatting
sbidari c451f2b
add docstring
sbidari c8fc78f
Apply suggestions from code review
sbidari 113f1d2
Apply suggestions from code review
sbidari e432180
use checkmate
sbidari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| # Generated by roxygen2: do not edit by hand | ||
|
|
||
| export(excluded_locations) | ||
| export(generate_hub_baseline) | ||
| export(generate_hub_ensemble) | ||
| export(update_hub_target_data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| library(epipredict) | ||
|
|
||
| #' Checks data latency in the input epi_df | ||
| #' | ||
| #' @param epi_df An epi_df object of time series data. | ||
| #' @param reference_date Date. The reference date for the forecast. | ||
| #' @param desired_max_time_value Date. The most recent date for which | ||
| #' observations are expected. | ||
| #' @param target_label Character. Human-readable label for the target, | ||
| #' e.g., "Hospital Admissions". | ||
| #' @param overlatent_err_thresh Numeric. Proportion threshold of locations | ||
| #' with excess latency to raise an error. Default 0.20 (20%). | ||
| #' @return NULL. Raises a warning if any location has excess latency or | ||
| #' raises an error if proportion of locations with excess latency exceeds threshold. | ||
| check_data_latency <- function( | ||
sbidari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| epi_df, | ||
| reference_date, | ||
| desired_max_time_value, | ||
| target_label, | ||
| overlatent_err_thresh = 0.20 | ||
| ) { | ||
| excess_latency_tbl <- epi_df |> | ||
| tidyr::drop_na(.data$observation) |> | ||
| dplyr::group_by(.data$geo_value) |> | ||
| dplyr::summarize( | ||
| max_time_value = max(.data$time_value), | ||
| .groups = "drop" | ||
| ) |> | ||
| dplyr::mutate( | ||
| excess_latency = pmax( | ||
| as.integer(desired_max_time_value - .data$max_time_value) %/% 7L, | ||
| 0L | ||
| ), | ||
| has_excess_latency = .data$excess_latency > 0L | ||
| ) | ||
|
|
||
| prop_locs_overlatent <- mean(excess_latency_tbl$has_excess_latency) | ||
|
|
||
| if (prop_locs_overlatent > overlatent_err_thresh) { | ||
| cli::cli_abort( | ||
| paste0( | ||
| "{target_label} forecast: More than ", | ||
| "{100 * overlatent_err_thresh}% of locations have excess latency. ", | ||
| "The reference date is {reference_date}, so we desire observations ", | ||
| "at least through {desired_max_time_value}. However, ", | ||
| "{nrow(excess_latency_tbl |> dplyr::filter(.data$has_excess_latency))} ", | ||
| "location{?s} had excess latency." | ||
| ) | ||
| ) | ||
| } else if (prop_locs_overlatent > 0) { | ||
| cli::cli_warn( | ||
| paste0( | ||
| "{target_label} forecast: Some locations have excess latency. ", | ||
| "The reference date is {reference_date}, so we desire observations ", | ||
| "at least through {desired_max_time_value}. However, ", | ||
| "{nrow(excess_latency_tbl |> dplyr::filter(.data$has_excess_latency))} ", | ||
| "location{?s} had excess latency." | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| #' Create a hub formatted baseline forecast for a single target | ||
| #' | ||
| #' @param target_data Data frame of target time series. Must include `date`, | ||
| #' `location`, `target`, and `observation` columns. | ||
| #' @param target_name Character. Name of the target to forecast, | ||
| #' e.g., "wk inc covid hosp". | ||
| #' @param target_label Character. Label for the target, e.g., "Hospital Admissions" | ||
| #' or "Proportion ED Visits". | ||
| #' @param reference_date Date. Reference date for the forecast. | ||
| #' @param desired_max_time_value Date. Most recent date for which observations are expected. | ||
| #' Function will error if there is excess latency; see [check_data_latency()]. | ||
| #' @param rng_seed Integer. Random seed for reproducibility. | ||
| #' @return A data frame of baseline forecasts for the specified target. | ||
| make_baseline_forecast <- function( | ||
| target_data, | ||
| target_name, | ||
| target_label, | ||
| reference_date, | ||
| desired_max_time_value, | ||
| rng_seed | ||
| ) { | ||
| checkmate::assertSubset( | ||
| c("date", "location", "target", "observation"), | ||
| colnames(target_data), | ||
| .var.name = "target_data columns" | ||
| ) | ||
|
|
||
| epi_df <- target_data |> | ||
| dplyr::filter(.data$target == !!target_name) |> | ||
| dplyr::mutate( | ||
| geo_value = forecasttools::us_location_recode( | ||
| .data$location, | ||
| "code", | ||
| "abbr" | ||
| ) | ||
| ) |> | ||
| dplyr::rename( | ||
| time_value = "date" | ||
| ) |> | ||
| dplyr::select(-c("location", "target")) |> | ||
| epiprocess::as_epi_df() | ||
|
|
||
| check_data_latency( | ||
| epi_df, | ||
| reference_date, | ||
| desired_max_time_value, | ||
| target_label | ||
| ) | ||
|
|
||
| args_list <- epipredict::cdc_baseline_args_list(aheads = 1:4, nsims = 1e5) | ||
| preds <- withr::with_rng_version( | ||
| "4.0.0", | ||
| withr::with_seed(rng_seed, { | ||
| fcst <- epipredict::cdc_baseline_forecaster( | ||
| epi_df |> | ||
| dplyr::filter( | ||
| .data$time_value <= desired_max_time_value | ||
| ), | ||
| "observation", | ||
| args_list | ||
| ) | ||
| # advance forecast_date by a week due to data latency and | ||
| # create forecast for horizon -1 | ||
| fcst$predictions |> | ||
| dplyr::mutate( | ||
| forecast_date = reference_date, | ||
| ahead = as.integer(.data$target_date - reference_date) %/% 7L | ||
| ) |> | ||
| # prepare -1 horizon predictions | ||
| dplyr::bind_rows( | ||
| epi_df |> | ||
| tidyr::drop_na(.data$observation) |> | ||
| dplyr::slice_max(.data$time_value) |> | ||
| dplyr::transmute( | ||
| forecast_date = reference_date, | ||
| target_date = reference_date - 7L, | ||
| ahead = -1L, | ||
| geo_value, | ||
| .pred = .data$observation, | ||
| .pred_distn = hardhat::quantile_pred( | ||
| values = matrix( | ||
| rep( | ||
| .data$observation, | ||
| each = length(args_list$quantile_levels) | ||
| ), | ||
| nrow = length(.data$observation), | ||
| ncol = length(args_list$quantile_levels), | ||
| byrow = TRUE | ||
| ), | ||
| quantile_levels = args_list$quantile_levels | ||
| ) | ||
| ) | ||
| ) | ||
| }) | ||
| ) | ||
|
|
||
| preds_formatted <- preds |> | ||
| epipredict::flusight_hub_formatter( | ||
| target = target_name, | ||
| output_type = "quantile" | ||
| ) |> | ||
| tidyr::drop_na(.data$output_type_id) |> | ||
| dplyr::arrange(.data$target, .data$horizon, .data$location) |> | ||
| dplyr::select( | ||
| "reference_date", | ||
| "horizon", | ||
| "target", | ||
| "target_end_date", | ||
| "location", | ||
| "output_type", | ||
| "output_type_id", | ||
| "value" | ||
| ) | ||
| return(preds_formatted) | ||
| } | ||
|
|
||
|
|
||
| #' Generate hub baseline forecasts for a given disease and reference date | ||
| #' | ||
| #' @param base_hub_path Path to the base hub directory. | ||
| #' @param reference_date Reference date (should be a Saturday). | ||
| #' @param disease Disease name ("covid" or "rsv"). | ||
sbidari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #' @return NULL. Writes baseline forecast file to hub's model-output directory. | ||
| #' @export | ||
| generate_hub_baseline <- function( | ||
| base_hub_path, | ||
| reference_date, | ||
| disease | ||
| ) { | ||
| checkmate::assert_scalar(disease) | ||
| checkmate::assert_names(disease, subset.of = c("covid", "rsv")) | ||
| reference_date <- lubridate::as_date(reference_date) | ||
| desired_max_time_value <- reference_date - 7L | ||
| dow_supplied <- lubridate::wday(reference_date, week_start = 7, label = FALSE) | ||
| rng_seed <- as.integer((59460707 + as.numeric(reference_date)) %% 2e9) | ||
| if (dow_supplied != 7) { | ||
| cli::cli_abort( | ||
| message = paste0( | ||
| "Expected `reference_date` to be a Saturday, day number 7 ", | ||
| "of the week, given the `week_start` value of Sunday. ", | ||
| "Got {reference_date}, which is day number ", | ||
| "{dow_supplied} of the week." | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| baseline_model_name <- glue::glue( | ||
| "{stringr::str_to_title(disease)}Hub-baseline" | ||
| ) | ||
| output_dirpath <- fs::path(base_hub_path, "model-output", baseline_model_name) | ||
| if (!fs::dir_exists(output_dirpath)) { | ||
| fs::dir_create(output_dirpath, recurse = TRUE) | ||
| } | ||
|
|
||
| hub_target_data <- hubData::connect_target_timeseries(base_hub_path) |> | ||
| dplyr::collect() |> | ||
| forecasttools::hub_target_data_as_of() | ||
|
|
||
| preds_hosp <- make_baseline_forecast( | ||
| target_data = hub_target_data, | ||
| target_name = glue::glue("wk inc {disease} hosp"), | ||
| target_label = "Hospital Admissions", | ||
| reference_date = reference_date, | ||
| desired_max_time_value = desired_max_time_value, | ||
| rng_seed = rng_seed | ||
| ) | ||
|
|
||
| preds_ed <- make_baseline_forecast( | ||
| target_data = hub_target_data, | ||
| target_name = glue::glue("wk inc {disease} prop ed visits"), | ||
| target_label = "Proportion ED Visits", | ||
| reference_date = reference_date, | ||
| desired_max_time_value = desired_max_time_value, | ||
| rng_seed = rng_seed | ||
| ) | ||
|
|
||
| forecasttools::write_tabular_file( | ||
sbidari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| dplyr::bind_rows(preds_hosp, preds_ed), | ||
| fs::path( | ||
| output_dirpath, | ||
| glue::glue("{reference_date}-{baseline_model_name}"), | ||
| ext = "csv" | ||
| ) | ||
| ) | ||
sbidari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| invisible() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.