|
| 1 | +abbr_to_fips <- function(abbr) { |
| 2 | + fi <- dplyr::left_join( |
| 3 | + tibble::tibble(abbr = tolower(abbr)), |
| 4 | + state_census, by = "abbr") %>% |
| 5 | + dplyr::mutate(fips = as.character(fips), fips = case_when( |
| 6 | + fips == "0" ~ "US", |
| 7 | + nchar(fips) < 2L ~ paste0("0", fips), |
| 8 | + TRUE ~ fips |
| 9 | + )) %>% |
| 10 | + pull(.data$fips) |
| 11 | + names(fi) <- NULL |
| 12 | + fi |
| 13 | +} |
| 14 | + |
| 15 | +#' Format predictions for submission to FluSight forecast Hub |
| 16 | +#' |
| 17 | +#' This function converts predictions from any of the included forecasters into |
| 18 | +#' a format (nearly) ready for submission to the 2023-24 |
| 19 | +#' [FluSight-forecast-hub](https://github.com/cdcepi/FluSight-forecast-hub). |
| 20 | +#' See there for documentation of the required columns. Currently, only |
| 21 | +#' "quantile" forcasts are supported, but the intention is to support both |
| 22 | +#' "quantile" and "pmf". For this reason, adding the `output_type` column should |
| 23 | +#' be done via the `...` argument. See the examples below. The specific required |
| 24 | +#' format for this forecast task is [here](https://github.com/cdcepi/FluSight-forecast-hub/blob/main/model-output/README.md). |
| 25 | +#' |
| 26 | +#' @param object a data.frame of predictions or an object of class |
| 27 | +#' `canned_epipred` as created by, e.g., [arx_forecaster()] |
| 28 | +#' @param ... <[`dynamic-dots`][rlang::dyn-dots]> Name = value pairs of constant |
| 29 | +#' columns (or mutations) to perform to the results. See examples. |
| 30 | +#' @param .fcast_period Control whether the `horizon` should represent days or |
| 31 | +#' weeks. Depending on whether the forecaster output has target dates |
| 32 | +#' from [layer_add_target_date()] or not, we may need to compute the horizon |
| 33 | +#' and/or the `target_end_date` from the other available columns in the predictions. |
| 34 | +#' When both `ahead` and `target_date` are available, this is ignored. If only |
| 35 | +#' `ahead` or `aheads` exists, then the target date may need to be multiplied |
| 36 | +#' if the `ahead` represents weekly forecasts. Alternatively, if only, the |
| 37 | +#' `target_date` is available, then the `horizon` will be in days, unless |
| 38 | +#' this argument is `"weekly"`. Note that these can be adjusted later by the |
| 39 | +#' `...` argument. |
| 40 | +#' |
| 41 | +#' @return A [tibble::tibble]. If `...` is empty, the result will contain the |
| 42 | +#' columns `reference_date`, `horizon`, `target_end_date`, `location`, |
| 43 | +#' `output_type_id`, and `value`. The `...` can perform mutations on any of |
| 44 | +#' these. |
| 45 | +#' @export |
| 46 | +#' |
| 47 | +#' @examples |
| 48 | +#' library(dplyr) |
| 49 | +#' weekly_deaths <- case_death_rate_subset %>% |
| 50 | +#' select(geo_value, time_value, death_rate) %>% |
| 51 | +#' left_join(state_census %>% select(pop, abbr), by = c("geo_value" = "abbr")) %>% |
| 52 | +#' mutate(deaths = pmax(death_rate / 1e5 * pop * 7, 0)) %>% |
| 53 | +#' select(-pop, -death_rate) %>% |
| 54 | +#' group_by(geo_value) %>% |
| 55 | +#' epi_slide(~ sum(.$deaths), before = 6, new_col_name = "deaths") %>% |
| 56 | +#' ungroup() %>% |
| 57 | +#' filter(weekdays(time_value) == "Saturday") |
| 58 | +#' |
| 59 | +#' cdc <- cdc_baseline_forecaster(weekly_deaths, "deaths") |
| 60 | +#' flusight_hub_formatter(cdc) |
| 61 | +#' flusight_hub_formatter(cdc, target = "wk inc covid deaths") |
| 62 | +#' flusight_hub_formatter(cdc, target = paste(horizon, "wk inc covid deaths")) |
| 63 | +#' flusight_hub_formatter(cdc, target = "wk inc covid deaths", output_type = "quantile") |
| 64 | +flusight_hub_formatter <- function( |
| 65 | + object, ..., |
| 66 | + .fcast_period = c("daily", "weekly")) { |
| 67 | + UseMethod("flusight_hub_formatter") |
| 68 | +} |
| 69 | + |
| 70 | +#' @export |
| 71 | +flusight_hub_formatter.canned_epipred <- function( |
| 72 | + object, ..., |
| 73 | + .fcast_period = c("daily", "weekly")) { |
| 74 | + flusight_hub_formatter(object$predictions, ..., .fcast_period = .fcast_period) |
| 75 | +} |
| 76 | + |
| 77 | +#' @export |
| 78 | +flusight_hub_formatter.data.frame <- function( |
| 79 | + object, ..., |
| 80 | + .fcast_period = c("daily", "weekly")) { |
| 81 | + required_names <- c(".pred", ".pred_distn", "forecast_date", "geo_value") |
| 82 | + optional_names <- c("ahead", "target_date") |
| 83 | + hardhat::validate_column_names(object, required_names) |
| 84 | + if (!any(optional_names %in% names(object))) { |
| 85 | + cli::cli_abort("At least one of {.val {optional_names}} must be present.") |
| 86 | + } |
| 87 | + |
| 88 | + dots <- enquos(..., .named = TRUE) |
| 89 | + names <- names(dots) |
| 90 | + |
| 91 | + object <- object %>% |
| 92 | + # combine the predictions and the distribution |
| 93 | + dplyr::mutate(.pred_distn = nested_quantiles(.pred_distn)) %>% |
| 94 | + dplyr::rowwise() %>% |
| 95 | + dplyr::mutate( |
| 96 | + .pred_distn = list(add_row(.pred_distn, q = .pred, tau = NA)), |
| 97 | + .pred = NULL |
| 98 | + ) %>% |
| 99 | + tidyr::unnest(.pred_distn) %>% |
| 100 | + # now we create the correct column names |
| 101 | + dplyr::rename( |
| 102 | + value = q, |
| 103 | + output_type_id = tau, |
| 104 | + reference_date = forecast_date |
| 105 | + ) %>% |
| 106 | + # convert to fips codes, and add any constant cols passed in ... |
| 107 | + dplyr::mutate(location = abbr_to_fips(tolower(geo_value)), geo_value = NULL) |
| 108 | + |
| 109 | + # create target_end_date / horizon, depending on what is available |
| 110 | + pp <- ifelse(match.arg(.fcast_period) == "daily", 1L, 7L) |
| 111 | + has_ahead <- charmatch("ahead", names(object)) |
| 112 | + if ("target_date" %in% names(object) && !is.na(has_ahead)) { |
| 113 | + object <- object %>% |
| 114 | + dplyr::rename( |
| 115 | + target_end_date = target_date, |
| 116 | + horizon = !!names(object)[has_ahead] |
| 117 | + ) |
| 118 | + } else if (!is.na(has_ahead)) { # ahead present, not target date |
| 119 | + object <- object %>% |
| 120 | + dplyr::rename(horizon = !!names(object)[has_ahead]) %>% |
| 121 | + dplyr::mutate(target_end_date = horizon * pp + reference_date) |
| 122 | + } else { # target_date present, not ahead |
| 123 | + object <- object %>% |
| 124 | + dplyr::rename(target_end_date = target_date) %>% |
| 125 | + dplyr::mutate(horizon = as.integer((target_end_date - reference_date)) / pp) |
| 126 | + } |
| 127 | + object %>% dplyr::relocate( |
| 128 | + reference_date, horizon, target_end_date, location, output_type_id, value |
| 129 | + ) %>% |
| 130 | + dplyr::mutate(!!!dots) |
| 131 | +} |
0 commit comments