Skip to content

Commit

Permalink
change around add horizons function, assuming last hosp data date is …
Browse files Browse the repository at this point in the history
…present in df
  • Loading branch information
kaitejohnson committed Jul 17, 2024
1 parent b0d5036 commit 27649e2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 32 deletions.
27 changes: 17 additions & 10 deletions _targets_eval_postprocessing.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -329,14 +324,18 @@ 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(
"location" = "state_abbr",
"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
Expand All @@ -359,14 +358,18 @@ 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(
"location" = "state_abbr",
"date" = "reference_date"
)
) |>
add_horizons(max_nowcast_days = max_nowcast_days)
add_horizons()
),
# Repeat for the quantile-based scores
tar_target(
Expand All @@ -387,14 +390,18 @@ 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(
"location" = "state_abbr",
"date" = "reference_date"
)
) |>
add_horizons(max_nowcast_days = max_nowcast_days)
add_horizons()
)
)

Expand Down
1 change: 1 addition & 0 deletions wweval/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 30 additions & 12 deletions wweval/R/add_horizons.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
}
16 changes: 6 additions & 10 deletions wweval/man/add_horizons.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 27649e2

Please sign in to comment.