hubverse-org / hubVis

Plotting methods for hub model outputs
https://hubverse-org.github.io/hubVis/
Other
3 stars 0 forks source link

`plot_step_ahead_model_output` returns error for model outputs that include PMF output type with `chr` `output_type_id` #18

Closed eahowerton closed 8 months ago

eahowerton commented 8 months ago

The example hub used in the hubEnsembles vignette/manuscript includes mean, median, quantile, and PMF output types. The PMF output types have output_type_id = c("large decrease", "decrease", "stable", "increase", "large increase"), which forces the entire output_type_id column to be chr. Example hub data: https://github.com/Infectious-Disease-Modeling-Hubs/hubEnsembles/tree/software-manuscript/inst/example-data/example-simple-forecast-hub

I tried to use this model output (filtered to only quantile output type) in plot_step_ahead_model_output(), and I received the following error: Error in x - y : non-numeric argument to binary operator. I traced this back to the step in the function where the interval ribbons are set up (I think the issue was because the function is trying to do something with "0.25" instead of 0.25).

Reproducible example

hub_path <- system.file("example-data/example-simple-forecast-hub",
                        package = "hubEnsembles")
model_outputs <- hubUtils::connect_hub(hub_path) |>
  dplyr::collect()
target_data_path <- file.path(hub_path, "target-data", "covid-hospitalizations.csv")
target_data <- read.csv(target_data_path) |>
    dplyr::mutate(time_idx = `as.Date(time_idx))

This code gives the error.

hubVis::plot_step_ahead_model_output(model_output_data = model_outputs |>
                                                  dplyr::filter(location == "US",
                                                                output_type %in% c("quantile"),
                                                                origin_date == "2022-12-12") |>
                                                  dplyr::mutate(target_date =  origin_date + horizon),
                                              truth_data = target_data |>
                                                  dplyr::filter(location == "US",
                                                                time_idx >= "2022-11-01",
                                                                time_idx <= "2023-03-01"),
                                              facet = "model_id", 
                                              facet_nrow = 1, 
                                              interactive = FALSE,
                                              intervals = 0.5,
                                              one_color = "black",
                                              pal_color = NULL, 
                                              show_legend = FALSE, 
                                              use_median_as_point = TRUE,)

Adding dplyr::mutate(output_type_id = as.double(output_type_id)) solves the problem.

hubVis::plot_step_ahead_model_output(model_output_data = model_outputs |>
                                                  dplyr::filter(location == "US",
                                                                output_type %in% c("quantile"),
                                                                origin_date == "2022-12-12") |>
                                                  dplyr::mutate(target_date =  origin_date + horizon, 
                                                       output_type_id = as.double(output_type_id)),
                                              truth_data = target_data |>
                                                  dplyr::filter(location == "US",
                                                                time_idx >= "2022-11-01",
                                                                time_idx <= "2023-03-01"),
                                              facet = "model_id", 
                                              facet_nrow = 1, 
                                              interactive = FALSE,
                                              intervals = 0.5,
                                              one_color = "black",
                                              pal_color = NULL, 
                                              show_legend = FALSE, 
                                              use_median_as_point = TRUE,)