Skip to content
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

Figure 2 rough draft components #100

Merged
merged 22 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8483111
fix function arg name
kaitejohnson Jul 11, 2024
ec80e66
draft ms fig2 in scratch
kaitejohnson Jul 11, 2024
c40db0c
Merge branch 'prod' into 84-fig2-components
kaitejohnson Jul 11, 2024
a8e718b
add fig2 hosp_t
kaitejohnson Jul 11, 2024
80a7e53
add bare bones functionality to make fig 2
kaitejohnson Jul 11, 2024
1f5e726
remove the scratch file
kaitejohnson Jul 11, 2024
4f087b6
separate plots so that every panel is an individual plot, to make it …
kaitejohnson Jul 14, 2024
005739d
add forecast comparison quantile figures
kaitejohnson Jul 14, 2024
7b20727
add forecast comparison plot
kaitejohnson Jul 15, 2024
8ee93e2
add the remainder of fig 2 components
kaitejohnson Jul 15, 2024
48ac09b
Update wweval/R/manuscript_figs.R
kaitejohnson Jul 16, 2024
13f7e5c
Update wweval/R/manuscript_figs.R
kaitejohnson Jul 16, 2024
5b438b8
create separate functions and add horizons upstream of figures
kaitejohnson Jul 16, 2024
e5e153b
fix typos in targets and in new functions
kaitejohnson Jul 16, 2024
53fd5f0
change from weekly to daily horizons
kaitejohnson Jul 16, 2024
d746465
make into single function to add horizons to scores and quantiles
kaitejohnson Jul 17, 2024
481be3e
Merge branch 'prod' into 84-fig2-components
kaitejohnson Jul 17, 2024
c68f05a
Update wweval/R/add_horizons.R
kaitejohnson Jul 17, 2024
b0d5036
Update wweval/R/add_horizons.R
kaitejohnson Jul 17, 2024
27649e2
change around add horizons function, assuming last hosp data date is …
kaitejohnson Jul 17, 2024
09834d1
forgot to add documentation
kaitejohnson Jul 17, 2024
f8429fb
Update wweval/R/add_horizons.R
kaitejohnson Jul 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 128 additions & 7 deletions _targets_eval_postprocessing.R
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ head_to_head_targets <- list(
# Get the full set of quantiles, filtered down to only states and
# forecast dates with sufficient wastewater for both ww model and hosp only
# model. Then join the convergence df
tar_target(
name = last_hosp_data_date_map,
command = get_last_hosp_data_date_map(all_hosp_model_quantiles)
),
tar_target(
name = hosp_quantiles_filtered,
command = dplyr::bind_rows(
Expand All @@ -320,13 +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()
),
# 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 @@ -349,13 +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()
),
# Repeat for the quantile-based scores
tar_target(
Expand All @@ -368,23 +382,129 @@ head_to_head_targets <- list(
dplyr::left_join(table_of_loc_dates_w_ww,
by = c("location", "forecast_date")
) |>
dplyr::filter(
isTRUE(ww_sufficient)
) |>
dplyr::filter(ww_sufficient) |>
dplyr::left_join(
convergence_df,
by = c(
"location",
"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()
)
)

# Manuscript figures------------------------------------------------
# Note that these are just the components of the figures, not the full
# ggarranged, properly formatted figures, and currently require
# specification for the figure components that are examples.
manuscript_figures <- list(
tar_target(
name = fig2_hosp_t_1,
command = make_fig2_hosp_t(
hosp_quantiles_filtered,
loc_to_plot = c("MA"),
date_to_plot = "2024-01-15"
)
),
tar_target(
name = fig2_hosp_t_2,
command = make_fig2_hosp_t(
hosp_quantiles_filtered,
loc_to_plot = c("AL"),
date_to_plot = "2024-01-15"
)
),
tar_target(
name = fig2_hosp_t_3,
command = make_fig2_hosp_t(
hosp_quantiles_filtered,
loc_to_plot = c("WA"),
date_to_plot = "2024-01-15"
)
),
tar_target(
name = fig2_ct_1,
command = make_fig2_ct(
all_ww_quantiles_sq,
loc_to_plot = "MA",
date_to_plot = "2024-01-15"
)
),
tar_target(
name = fig2_ct_2,
command = make_fig2_ct(
all_ww_quantiles_sq,
loc_to_plot = "AL",
date_to_plot = "2024-01-15"
)
),
tar_target(
name = fig2_ct_3,
command = make_fig2_ct(
all_ww_quantiles_sq,
loc_to_plot = "WA",
date_to_plot = "2024-01-15"
)
dylanhmorris marked this conversation as resolved.
Show resolved Hide resolved
),
tar_target(
name = fig2_forecast_comparison_nowcast1,
command = make_hosp_forecast_comp_fig(
hosp_quantiles_filtered,
loc_to_plot = "MA",
horizon_to_plot = "nowcast"
)
),
tar_target(
name = fig2_forecast_comparison_1wk1,
command = make_hosp_forecast_comp_fig(
hosp_quantiles_filtered,
loc_to_plot = "MA",
horizon_to_plot = "1 wk"
)
),
tar_target(
name = fig2_forecast_comparison_4wks1,
command = make_hosp_forecast_comp_fig(
hosp_quantiles_filtered,
loc_to_plot = "MA",
horizon_to_plot = "4 wks"
)
),
tar_target(
name = fig2_crps_underlay_nowcast1,
command = make_crps_underlay_fig(
scores_filtered,
loc_to_plot = "MA",
horizon_to_plot = "nowcast"
)
),
tar_target(
name = fig2_crps_underlay_1wk1,
command = make_crps_underlay_fig(
scores_filtered,
loc_to_plot = "MA",
horizon_to_plot = "1 wk"
)
),
tar_target(
name = fig2_crps_underlay_4wks1,
command = make_crps_underlay_fig(
scores_filtered,
loc_to_plot = "MA",
horizon_to_plot = "4 wks"
)
)
)

Expand Down Expand Up @@ -783,11 +903,12 @@ hub_comparison_plots <- list(



# Run the targets pipeline
# Run the targets pipeline----------------------------------------------------
list(
upstream_targets,
combined_targets,
head_to_head_targets,
manuscript_figures,
scenario_targets,
hub_targets,
hub_comparison_plots
Expand Down
6 changes: 6 additions & 0 deletions wweval/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(add_horizons)
export(add_time_indexing)
export(clean_ww_data)
export(combine_outputs)
Expand All @@ -26,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 All @@ -49,7 +51,11 @@ export(get_ww_data_indices)
export(get_ww_data_sizes)
export(get_ww_values)
export(make_baseline_score_table)
export(make_crps_underlay_fig)
export(make_df)
export(make_fig2_ct)
export(make_fig2_hosp_t)
export(make_hosp_forecast_comp_fig)
export(sample_model)
export(save_table)
export(score_hub_submissions)
Expand Down
59 changes: 59 additions & 0 deletions wweval/R/add_horizons.R
dylanhmorris marked this conversation as resolved.
Show resolved Hide resolved
dylanhmorris marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#' Add columns indicating the horizon to the scores
#'
#' @description
#' This function takes in a tibble of scores on the nowcasts/forecasts
#' 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`,`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) {
df_w_horizons <- df |>
dplyr::mutate(
horizon_days = as.numeric(date - forecast_date)
) |>
dplyr::mutate(
horizon = dplyr::case_when(
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",
horizon_days > 21 & horizon_days <= 28 ~ "4 wks",
TRUE ~ NA_character_
)
)

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`.
#' `calib_data` should be `NA` for any dates for which there was
#' not hospital admissions data to fit to.
#'
#' @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)
}
Loading
Loading