Skip to content

Commit a3c0f82

Browse files
author
Kaitlyn Johnson
committed
group by model only
1 parent e04703d commit a3c0f82

File tree

4 files changed

+173
-2
lines changed

4 files changed

+173
-2
lines changed

R/fig_overall_scores.R

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,10 +1142,10 @@ get_plot_avg_rel_skill_by_t <- function(scores_obj,
11421142
) +
11431143
labs(
11441144
x = "",
1145-
y = glue::glue("Relative scaled\nskill ({label})")
1145+
y = glue::glue("Average relative scaled\nskill ({label})")
11461146
) +
11471147
scale_y_continuous(trans = "log10") +
1148-
coord_cartesian(ylim = c(1 / 2.4, 2.4)) +
1148+
coord_cartesian(ylim = c(1 / 3, 3)) +
11491149
theme(
11501150
axis.text.x = element_blank(),
11511151
axis.title.x = element_text(size = 12)
@@ -1163,3 +1163,108 @@ get_plot_avg_rel_skill_by_t <- function(scores_obj,
11631163
}
11641164
return(p)
11651165
}
1166+
1167+
#' Brier/Energy Relative skill averaged by model
1168+
#'
1169+
#' @param scores_obj Scoringutils scores object
1170+
#' @param score_type Character string indicating which score metric to use
1171+
#' @param remove_legend Boolean indicating whether to keep legend, default
1172+
#' is TRUE.
1173+
#' @param add_shape Boolean indicating whether to add the shape legend,
1174+
#' default is FALSE.
1175+
#' @param title Character string indicating title, default is NULL.
1176+
#' @importFrom scoringutils summarise_scores
1177+
#' @importFrom ggplot2 ggplot geom_bar aes geom_hline coord_flip
1178+
#' @importFrom rlang sym
1179+
#' @returns ggplot object
1180+
#' @autoglobal
1181+
get_plot_avg_rel_skill_overall <- function(scores_obj,
1182+
score_type = c("brier_score", "energy_score"),
1183+
remove_legend = TRUE,
1184+
add_shape = FALSE,
1185+
title = NULL) {
1186+
score_type <- rlang::arg_match(score_type)
1187+
plot_components_list <- plot_components()
1188+
if (score_type == "brier_score") {
1189+
label <- "Brier score"
1190+
} else {
1191+
label <- "Energy score"
1192+
}
1193+
1194+
rel_skill <- scores_obj |>
1195+
ungroup() |>
1196+
filter(!is.na(!!sym(score_type))) |>
1197+
scoringutils::get_pairwise_comparisons(
1198+
baseline = "Hub-baseline",
1199+
metric = score_type,
1200+
by = c("nowcast_date", "target_date", "location")
1201+
) |>
1202+
filter(model != "Hub-baseline") |>
1203+
group_by(model) |>
1204+
summarise(scaled_rel_skill = mean(!!sym(glue::glue(
1205+
"{score_type}_scaled_relative_skill"
1206+
)), na.rm = TRUE))
1207+
1208+
p <- ggplot(rel_skill) +
1209+
geom_point(
1210+
aes(
1211+
x = model,
1212+
y = scaled_rel_skill,
1213+
color = model,
1214+
shape = model
1215+
),
1216+
size = 6
1217+
) +
1218+
geom_hline(yintercept = 1, linetype = "dashed", color = "gray50") +
1219+
scale_color_manual(
1220+
name = "Model",
1221+
values = plot_components_list$model_colors
1222+
) +
1223+
scale_shape_manual(
1224+
name = "Model",
1225+
values = plot_components_list$model_shapes
1226+
) +
1227+
get_plot_theme() +
1228+
labs(
1229+
x = "",
1230+
y = glue::glue("Average relative scaled skill\n({label})")
1231+
) +
1232+
scale_y_continuous(trans = "log10") +
1233+
theme(
1234+
axis.text.x = element_blank(),
1235+
axis.ticks.x = element_blank()
1236+
) +
1237+
coord_cartesian(ylim = c(1 / 1.3, 1.3)) +
1238+
guides(
1239+
color = guide_legend(
1240+
title.position = "top",
1241+
nrow = 1
1242+
),
1243+
shape = guide_legend(
1244+
title.position = "top",
1245+
nrow = 1
1246+
)
1247+
)
1248+
1249+
if (isTRUE(remove_legend)) {
1250+
p <- p + guides(
1251+
fill = "none",
1252+
color = "none",
1253+
shape = "none"
1254+
)
1255+
}
1256+
1257+
if (isTRUE(add_shape)) {
1258+
p <- p + guides(
1259+
shape = guide_legend(
1260+
title.position = "top",
1261+
nrow = 1
1262+
)
1263+
)
1264+
}
1265+
1266+
if (!is.null(title)) {
1267+
p <- p + ggtitle(glue::glue("{title}"))
1268+
}
1269+
return(p)
1270+
}

R/globals.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ utils::globalVariables(c(
131131
"model", # <get_plot_avg_rel_skill_by_t>
132132
"nowcast_date", # <get_plot_avg_rel_skill_by_t>
133133
"scaled_rel_skill", # <get_plot_avg_rel_skill_by_t>
134+
"model", # <get_plot_avg_rel_skill_overall>
135+
"nowcast_date", # <get_plot_avg_rel_skill_overall>
136+
"scaled_rel_skill", # <get_plot_avg_rel_skill_overall>
134137
"model_id", # <prepare_submission_data>
135138
"location", # <prepare_submission_data>
136139
"nowcast_date", # <prepare_submission_data>

man/get_plot_avg_rel_skill_overall.Rd

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

targets/fig_overall_targets.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,21 @@ fig_overall_targets <- list(
366366

367367
# Supplemental figures that are related---------------------------------
368368
## US minus CA ---------------------------------------------------------
369+
tar_target(
370+
name = plot_avg_overall_rel_skill_brier,
371+
command = get_plot_avg_rel_skill_overall(
372+
scores_obj = su_scores_ep,
373+
score_type = "brier_score",
374+
title = "US minus CA"
375+
)
376+
),
377+
tar_target(
378+
name = plot_avg_overall_rel_skill_energy,
379+
command = get_plot_avg_rel_skill_overall(
380+
scores_obj = su_scores_ep,
381+
score_type = "energy_score"
382+
)
383+
),
369384
tar_target(
370385
name = plot_avg_rel_skill_brier_by_loc,
371386
command = get_plot_avg_rel_skill_by_loc(
@@ -397,6 +412,21 @@ fig_overall_targets <- list(
397412
)
398413
),
399414
## CA ---------------------------------------------------------
415+
tar_target(
416+
name = plot_avg_overall_rel_skill_brier_ca,
417+
command = get_plot_avg_rel_skill_overall(
418+
scores_obj = su_scores_ca,
419+
score_type = "brier_score",
420+
title = "US minus CA"
421+
)
422+
),
423+
tar_target(
424+
name = plot_avg_overall_rel_skill_energy_ca,
425+
command = get_plot_avg_rel_skill_overall(
426+
scores_obj = su_scores_ca,
427+
score_type = "energy_score"
428+
)
429+
),
400430
tar_target(
401431
name = plot_avg_rel_skill_brier_by_loc_ca,
402432
command = get_plot_avg_rel_skill_by_loc(

0 commit comments

Comments
 (0)