|
| 1 | +library(dplyr) |
| 2 | +library(evalcast) |
| 3 | +library(ggplot2) |
| 4 | +library(magrittr) |
| 5 | +library(tidyr) |
| 6 | + |
| 7 | + |
| 8 | +get_truth_data <- function(exclude_geos, ...) { |
| 9 | + download_args <- list(...) |
| 10 | + truth_data <- do.call(download_signal, download_args) |
| 11 | + truth_data %<>% filter(!(.data$geo_value %in% exclude_geos)) |
| 12 | + truth_data <- truth_data %>% |
| 13 | + dplyr::select(.data$geo_value, .data$time_value, .data$value) %>% |
| 14 | + dplyr::rename(target_end_date = .data$time_value) |
| 15 | + return(truth_data %>% tibble()) |
| 16 | +} |
| 17 | + |
| 18 | +get_quantiles_df <- function(predictions_cards, intervals = c(.5, .9), ...) { |
| 19 | + predictions_cards <- predictions_cards %>% |
| 20 | + dplyr::select( |
| 21 | + .data$geo_value, .data$quantile, |
| 22 | + .data$value, .data$forecaster, .data$forecast_date, |
| 23 | + .data$target_end_date |
| 24 | + ) |
| 25 | + |
| 26 | + lower_bounds <- predictions_cards %>% |
| 27 | + select(.data$quantile) %>% |
| 28 | + filter(.data$quantile < 0.5) %>% |
| 29 | + unique() %>% |
| 30 | + pull() |
| 31 | + quantiles_to_plot <- as.integer(sort( |
| 32 | + round(500L * (1 + intervals %o% c(-1L, 1L))) |
| 33 | + )) |
| 34 | + |
| 35 | + quantiles_df <- predictions_cards %>% |
| 36 | + filter(as.integer(round(.data$quantile * 1000)) %in% c(quantiles_to_plot)) %>% |
| 37 | + mutate( |
| 38 | + endpoint_type = if_else(.data$quantile < 0.5, "lower", "upper"), |
| 39 | + alp = if_else(.data$endpoint_type == "lower", |
| 40 | + format(2 * .data$quantile, digits = 3, nsmall = 3), |
| 41 | + format(2 * (1 - .data$quantile), digits = 3, nsmall = 3) |
| 42 | + ), |
| 43 | + interval = forcats::fct_rev( |
| 44 | + paste0((1 - as.numeric(.data$alp)) * 100, "%") |
| 45 | + ) |
| 46 | + ) %>% |
| 47 | + select(-.data$quantile, -.data$alp) %>% |
| 48 | + pivot_wider(names_from = "endpoint_type", values_from = "value") |
| 49 | + |
| 50 | + return(quantiles_df) |
| 51 | +} |
| 52 | + |
| 53 | +get_points_df <- function(predictions_cards) { |
| 54 | + points_df <- predictions_cards %>% |
| 55 | + filter(as.integer(round(.data$quantile * 1000)) == 500L | |
| 56 | + is.na(.data$quantile)) |
| 57 | + if (any(is.na(points_df$quantile))) { |
| 58 | + points_df <- points_df %>% |
| 59 | + pivot_wider(names_from = "quantile", values_from = "value") %>% |
| 60 | + mutate(value = if_else(!is.na(.data$`NA`), .data$`NA`, .data$`0.5`)) %>% |
| 61 | + select(-.data$`0.5`, -.data$`NA`) |
| 62 | + } else { |
| 63 | + points_df <- points_df %>% |
| 64 | + select(-.data$quantile) |
| 65 | + } |
| 66 | + |
| 67 | + return(points_df) |
| 68 | +} |
| 69 | + |
| 70 | +plot_quantiles <- function(g, quantiles_df) { |
| 71 | + n_quantiles <- nlevels(quantiles_df$interval) |
| 72 | + l_quantiles <- levels(quantiles_df$interval) |
| 73 | + |
| 74 | + alp <- c(.4, .2, .1) |
| 75 | + for (qq in n_quantiles:1) { |
| 76 | + g <- g + |
| 77 | + geom_ribbon( |
| 78 | + data = quantiles_df %>% |
| 79 | + filter(.data$interval == l_quantiles[qq]), |
| 80 | + mapping = aes( |
| 81 | + ymin = .data$lower, |
| 82 | + ymax = .data$upper, |
| 83 | + group = interaction(.data$forecast_date, .data$forecaster), |
| 84 | + color = NULL |
| 85 | + ), |
| 86 | + alpha = alp[qq] |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + return(g) |
| 91 | +} |
| 92 | + |
| 93 | +plot_points <- function(g, points_df) { |
| 94 | + g <- g + geom_point( |
| 95 | + data = points_df, |
| 96 | + mapping = aes( |
| 97 | + y = .data$value, |
| 98 | + group = interaction(.data$forecast_date, .data$forecaster) |
| 99 | + ), |
| 100 | + size = 0.125 |
| 101 | + ) |
| 102 | + |
| 103 | + return(g) |
| 104 | +} |
| 105 | + |
| 106 | +plot_state_forecasters <- function(predictions_cards, exclude_geos = c(), start_day = NULL, ncol = 5, offline_signal_dir = NULL) { |
| 107 | + if (nrow(predictions_cards) == 0) { |
| 108 | + return(NULL) |
| 109 | + } |
| 110 | + |
| 111 | + td1 <- get_truth_data(exclude_geos = exclude_geos, data_source = "hhs", signal = "confirmed_admissions_covid_1d", start_day = start_day, geo_type = "state", offline_signal_dir = offline_signal_dir) |
| 112 | + td1$data_source <- "hhs" |
| 113 | + td2 <- get_truth_data(exclude_geos = exclude_geos, data_source = "jhu-csse", signal = "confirmed_7dav_incidence_num", start_day = start_day, geo_type = "state", offline_signal_dir = offline_signal_dir) |
| 114 | + td2$data_source <- "jhu" |
| 115 | + td1.max <- td1 %>% |
| 116 | + group_by(geo_value) %>% |
| 117 | + summarize(max_value = max(value)) |
| 118 | + td2.max <- td2 %>% |
| 119 | + group_by(geo_value) %>% |
| 120 | + summarize(max_value = max(value)) |
| 121 | + td2.max <- td2.max %>% |
| 122 | + left_join(td1.max, by = "geo_value", suffix = c(".2", ".1")) %>% |
| 123 | + mutate(max_ratio = max_value.1 / max_value.2) |
| 124 | + td2 <- td2 %>% |
| 125 | + left_join(td2.max, by = "geo_value") %>% |
| 126 | + mutate(scaled_value = value * max_ratio) |
| 127 | + td1 <- td1 %>% mutate(forecaster = "hhs hosp truth") |
| 128 | + td2 <- td2 %>% mutate(forecaster = "jhu cases truth") |
| 129 | + |
| 130 | + # Setup plot |
| 131 | + g <- ggplot(td1, mapping = aes(x = .data$target_end_date, color = .data$forecaster, fill = .data$forecaster)) |
| 132 | + |
| 133 | + points_df <- get_points_df(predictions_cards) |
| 134 | + g <- plot_points(g, points_df) |
| 135 | + |
| 136 | + quantiles_df <- get_quantiles_df(predictions_cards) |
| 137 | + g <- plot_quantiles(g, quantiles_df) |
| 138 | + |
| 139 | + # Plot truth data by geo |
| 140 | + g <- g + |
| 141 | + geom_line(mapping = aes(y = .data$value)) + |
| 142 | + geom_line(data = td2, mapping = aes(x = .data$target_end_date, y = .data$scaled_value)) + |
| 143 | + facet_wrap(~ .data$geo_value, scales = "free_y", ncol = ncol, drop = TRUE) + |
| 144 | + theme(legend.position = "top", legend.text = element_text(size = 7)) |
| 145 | + |
| 146 | + return(g) |
| 147 | +} |
| 148 | + |
| 149 | +plot_nation_forecasters <- function(predictions_cards, exclude_geos = c(), start_day = NULL, ncol = 5, offline_signal_dir = NULL) { |
| 150 | + if (nrow(predictions_cards) == 0) { |
| 151 | + return(NULL) |
| 152 | + } |
| 153 | + |
| 154 | + td1 <- get_truth_data(exclude_geos = exclude_geos, data_source = "hhs", signal = "confirmed_admissions_covid_1d", start_day = start_day, geo_type = "nation", offline_signal_dir = offline_signal_dir) |
| 155 | + td1$data_source <- "hhs" |
| 156 | + td2 <- get_truth_data(exclude_geos = exclude_geos, data_source = "jhu-csse", signal = "confirmed_7dav_incidence_num", start_day = start_day, geo_type = "nation", offline_signal_dir = offline_signal_dir) |
| 157 | + td2$data_source <- "jhu" |
| 158 | + td1.max <- td1 %>% |
| 159 | + summarize(max_value = max(value)) %>% |
| 160 | + pull(max_value) |
| 161 | + td2.max <- td2 %>% |
| 162 | + summarize(max_value = max(value)) %>% |
| 163 | + pull(max_value) |
| 164 | + td2 <- td2 %>% |
| 165 | + mutate(scaled_value = value * td1.max / td2.max) |
| 166 | + |
| 167 | + # Setup plot |
| 168 | + g <- ggplot(td1, mapping = aes(x = .data$target_end_date)) |
| 169 | + |
| 170 | + quantiles_df <- get_quantiles_df(predictions_cards) |
| 171 | + g <- plot_quantiles(g, quantiles_df) |
| 172 | + |
| 173 | + points_df <- get_points_df(predictions_cards) |
| 174 | + g <- plot_points(g, points_df) |
| 175 | + |
| 176 | + # Plot truth data by geo |
| 177 | + g <- g + |
| 178 | + geom_line(mapping = aes(y = .data$value, color = "confirmed admissions")) + |
| 179 | + geom_line(data = td2, mapping = aes(x = .data$target_end_date, y = .data$scaled_value, color = "7day case sum")) + |
| 180 | + labs(fill = "Reported Signal") + |
| 181 | + theme(legend.position = "top", legend.text = element_text(size = 7)) |
| 182 | + |
| 183 | + return(g) |
| 184 | +} |
0 commit comments