diff --git a/_targets_eval_postprocessing.R b/_targets_eval_postprocessing.R index 8a6bffdc..410bce14 100644 --- a/_targets_eval_postprocessing.R +++ b/_targets_eval_postprocessing.R @@ -302,13 +302,8 @@ head_to_head_targets <- list( # forecast dates with sufficient wastewater for both ww model and hosp only # model. Then join the convergence df tar_target( - name = max_nowcast_days, - command = as.numeric( - max(all_ww_hosp_quantiles$forecast_date) - - max(all_ww_hosp_quantiles$date[!is.na( - all_ww_hosp_quantiles$calib_data - )]) - ) + name = last_hosp_data_date_map, + command = get_last_hosp_data_date_map(all_hosp_model_quantiles) ), tar_target( name = hosp_quantiles_filtered, @@ -329,6 +324,10 @@ head_to_head_targets <- list( "forecast_date" ) ) |> + dplyr::left_join( + last_hosp_data_date_map, + by = c("location", "forecast_date") + ) |> dplyr::left_join( epidemic_phases, by = c( @@ -336,7 +335,7 @@ head_to_head_targets <- list( "date" = "reference_date" ) ) |> - add_horizons(max_nowcast_days = max_nowcast_days) + add_horizons() ), # Do the same thing for the sampled scores, combining ww and hosp under # the status quo scenario, filtering to the locations and forecast dates @@ -359,6 +358,10 @@ head_to_head_targets <- list( "forecast_date" ) ) |> + dplyr::left_join( + last_hosp_data_date_map, + by = c("location", "forecast_date") + ) |> dplyr::left_join( epidemic_phases, by = c( @@ -366,7 +369,7 @@ head_to_head_targets <- list( "date" = "reference_date" ) ) |> - add_horizons(max_nowcast_days = max_nowcast_days) + add_horizons() ), # Repeat for the quantile-based scores tar_target( @@ -387,6 +390,10 @@ head_to_head_targets <- list( "forecast_date" ) ) |> + dplyr::left_join( + last_hosp_data_date_map, + by = c("location", "forecast_date") + ) |> dplyr::left_join( epidemic_phases, by = c( @@ -394,7 +401,7 @@ head_to_head_targets <- list( "date" = "reference_date" ) ) |> - add_horizons(max_nowcast_days = max_nowcast_days) + add_horizons() ) ) diff --git a/wweval/NAMESPACE b/wweval/NAMESPACE index 647e3e2c..f4272eef 100644 --- a/wweval/NAMESPACE +++ b/wweval/NAMESPACE @@ -27,6 +27,7 @@ export(get_hosp_values) export(get_input_hosp_data) export(get_input_ww_data) export(get_last_hosp_data_date) +export(get_last_hosp_data_date_map) export(get_model_draws_w_data) export(get_model_path) export(get_n_states_improved_plot) diff --git a/wweval/R/add_horizons.R b/wweval/R/add_horizons.R index 7bb44090..15c4d6da 100644 --- a/wweval/R/add_horizons.R +++ b/wweval/R/add_horizons.R @@ -2,31 +2,27 @@ #' #' @description #' This function takes in a tibble of scores on the nowcasts/forecasts -#' containing the columns `date` and `forecast_date` and adds the following -#' columns: `horizon_days` (an integer) and `horizon` (a string to be used for -#' categorical grouping of horizons) +#' containing the columns `date`, `forecast_date`, and `last_hosp_data_date`, +#' and adds the following columns: `horizon_days` (an integer) and +#' `horizon` (a string to be used for categorical grouping of horizons) #' #' #' @param df A tibble containing either forecasts or scores (or both) -#' and the following required columns: `date`and `forecast_date` -#' @param max_nowcast_days An integer indicating the expected maximum number -#' of weeks where date < forecast date. If the output contains values beyond -#' this threshold, an error will be thrown which may indicate that the scores -#' contain scores for calibrated data. +#' and the following required columns: `date`,`forecast_date`, +#' `last_hosp_data_date` #' #' @return a tibble containing the same columns as `df` plus #' `horizon_days` and `horizon` #' @export -add_horizons <- function(df, - max_nowcast_days) { +add_horizons <- function(df) { df_w_horizons <- df |> dplyr::mutate( horizon_days = as.numeric(date - forecast_date) ) |> dplyr::mutate( horizon = dplyr::case_when( - horizon_days <= -!!max_nowcast_days ~ "calibration", - horizon_days > -!!max_nowcast_days & horizon_days <= 0 ~ "nowcast", + date <= last_hosp_data_date & horizon_days <= 0 ~ "calibration", + date > last_hosp_data_date & horizon_days <= 0 ~ "nowcast", horizon_days > 0 & horizon_days <= 7 ~ "1 wk", horizon_days > 7 & horizon_days <= 14 ~ "2 wks", horizon_days > 14 & horizon_days <= 21 ~ "3 wks", @@ -37,3 +33,25 @@ add_horizons <- function(df, return(df_w_horizons) } + +#' Get a map of the location, forecast date, and last hospital admissions data +#' date +#' +#' @param df A tibble containing the following columns: `forecast_date`, +#' `location`,`date`, `calib_data` +#' +#' @return A tibble with that maps the unique combinations of `location` and +#' `forecast` date to the last hospital admissions data date +#' `last_hosp_data_date` +#' @export +get_last_hosp_data_date_map <- function(df) { + map <- df |> + dplyr::group_by(forecast_date, location) |> + dplyr::filter(!is.na(calib_data)) |> + dplyr::summarise( + last_hosp_data_date = max(date) + ) |> + dplyr::ungroup() + + return(map) +} diff --git a/wweval/man/add_horizons.Rd b/wweval/man/add_horizons.Rd index 005a213c..40660a3d 100644 --- a/wweval/man/add_horizons.Rd +++ b/wweval/man/add_horizons.Rd @@ -4,16 +4,12 @@ \alias{add_horizons} \title{Add columns indicating the horizon to the scores} \usage{ -add_horizons(df, max_nowcast_days) +add_horizons(df) } \arguments{ \item{df}{A tibble containing either forecasts or scores (or both) -and the following required columns: \code{date}and \code{forecast_date}} - -\item{max_nowcast_days}{An integer indicating the expected maximum number -of weeks where date < forecast date. If the output contains values beyond -this threshold, an error will be thrown which may indicate that the scores -contain scores for calibrated data.} +and the following required columns: \code{date},\code{forecast_date}, +\code{last_hosp_data_date}} } \value{ a tibble containing the same columns as \code{df} plus @@ -21,7 +17,7 @@ a tibble containing the same columns as \code{df} plus } \description{ This function takes in a tibble of scores on the nowcasts/forecasts -containing the columns \code{date} and \code{forecast_date} and adds the following -columns: \code{horizon_days} (an integer) and \code{horizon} (a string to be used for -categorical grouping of horizons) +containing the columns \code{date}, \code{forecast_date}, and \code{last_hosp_data_date}, +and adds the following columns: \code{horizon_days} (an integer) and +\code{horizon} (a string to be used for categorical grouping of horizons) }