for (forecast_date in forecast_dates) {
    cat(paste("\n\n##", forecast_date, " {.tabset .tabset-fade}\n\n"))

    for (target in names(target_variables)) {
      cat("\n\n### ", target, "\n\n")

      date_range <- c(as.Date(report_date) - 7 * 10, 
                       as.Date(report_date) + 7 * 4)

      filter_both <-
        list(paste0("target_variable %in% '", target_variables[[target]], "'")) 
      filter_truth <-
        list(paste0("target_end_date > '", as.Date(report_date) - 7 * 10, "'"), 
             paste0("target_end_date <= '", as.Date(report_date) + 7 * 4, "'"))
      filter_forecasts <- list(paste0("forecast_date == '", as.Date(forecast_date), "'"))

      plot_data <- copy(data)
      plot_data <- plot_data[, true_value := true_value / population * 1e+5]
      plot_data <- plot_data[, prediction := prediction / population * 1e+5]

      plot <- scoringutils::plot_predictions(plot_data,
                                             x = "target_end_date",
                                             facet_formula = ~ location_name,
                                             ncol = 3, 
                                             filter_both = filter_both,
                                             filter_truth = filter_truth,
                                             filter_forecasts = filter_forecasts,
                                             allow_truth_without_pred = TRUE,
                                             # zoom_multiple_data =
                                             #   c(ymin = 0, ymax = 3),
                                             # expand_limits =
                                             #   list(x =
                                             #          max(data$target_end_date),
                                             #        y = 0),
                                             scales = "free") + 
        ggplot2::theme(legend.position = "bottom",
                       strip.placement = "outside", 
                       strip.text = ggplot2::element_text(face="bold", size=9))+
        scale_x_date(limits = c(date_range[1], date_range[2])) + 
        scale_y_continuous("True and predicted values per week per 100,000", labels = scales::comma) +
        xlab("")

      print(plot)
  }
}


epiforecasts/europe-covid-forecast documentation built on Jan. 15, 2025, 8:57 p.m.