Skip to content

Commit

Permalink
Issue #530 - Clarify workflow in examples (#877)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosbosse authored Jul 30, 2024
1 parent 54fb844 commit 6f5bc1b
Show file tree
Hide file tree
Showing 22 changed files with 140 additions and 74 deletions.
18 changes: 9 additions & 9 deletions R/convenience-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,13 @@ transform_forecasts <- function(forecast,
#' @keywords check-forecasts
#' @importFrom checkmate assert_numeric assert_number
#' @examples
#' library(magrittr) # pipe operator
#' log_shift(1:10)
#' log_shift(0:9, offset = 1)
#'
#' transform_forecasts(
#' as_forecast_quantile(example_quantile)[observed > 0, ],
#' fun = log_shift,
#' offset = 1
#' )
#' example_quantile[observed > 0, ] %>%
#' as_forecast_quantile() %>%
#' transform_forecasts(fun = log_shift, offset = 1)

log_shift <- function(x, offset = 0, base = exp(1)) {

Expand Down Expand Up @@ -270,10 +269,11 @@ log_shift <- function(x, offset = 0, base = exp(1)) {
#' @export
#' @keywords data-handling
#' @examples
#' set_forecast_unit(
#' example_quantile,
#' c("location", "target_end_date", "target_type", "horizon", "model")
#' )
#' library(magrittr) # pipe operator
#' example_quantile %>%
#' set_forecast_unit(
#' c("location", "target_end_date", "target_type", "horizon", "model")
#' )
set_forecast_unit <- function(data, forecast_unit) {
data <- ensure_data.table(data)
assert_subset(forecast_unit, names(data), empty.ok = FALSE)
Expand Down
13 changes: 6 additions & 7 deletions R/correlations.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
#' @export
#' @keywords scoring
#' @examples
#' scores <- score(as_forecast_quantile(example_quantile))
#' library(magrittr) # pipe operator
#'
#' scores <- example_quantile %>%
#' as_forecast_quantile() %>%
#' score()
#'
#' get_correlations(scores)
get_correlations <- function(scores,
metrics = get_metrics(scores),
Expand All @@ -40,9 +45,3 @@ get_correlations <- function(scores,

return(correlations[])
}

# helper function to obtain lower triangle of matrix
get_lower_tri <- function(cormat) {
cormat[lower.tri(cormat)] <- NA
return(cormat)
}
8 changes: 4 additions & 4 deletions R/default-scoring-rules.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ select_metrics <- function(metrics, select = NULL, exclude = NULL) {
#' metrics_quantile(),
#' "interval_coverage_70" = interval_coverage_70
#' )
#' score(
#' as_forecast_quantile(example_quantile),
#' metrics = updated_metrics
#' )
#'
#' library(magrittr) # pipe operator
#'
#' example_quantile %>%
#' as_forecast_quantile() %>%
#' score(metrics = updated_metrics)
customise_metric <- function(metric, ...) {
assert_function(metric)
dots <- list(...)
Expand Down
8 changes: 4 additions & 4 deletions R/get_-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,10 @@ get_coverage <- function(forecast, by = "model") {
#' data.table::setDTthreads(2) # restricts number of cores used on CRAN
#' }
#'
#' get_forecast_counts(
#' as_forecast_quantile(example_quantile),
#' by = c("model", "target_type")
#' )
#' library(magrittr) # pipe operator
#' example_quantile %>%
#' as_forecast_quantile() %>%
#' get_forecast_counts(by = c("model", "target_type"))
get_forecast_counts <- function(forecast,
by = get_forecast_unit(forecast),
collapse = c("quantile_level", "sample_id")) {
Expand Down
7 changes: 6 additions & 1 deletion R/pairwise-comparisons.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@
#' data.table::setDTthreads(2) # restricts number of cores used on CRAN
#' }
#'
#' scores <- score(as_forecast_quantile(example_quantile))
#' library(magrittr) # pipe operator
#'
#' scores <- example_quantile %>%
#' as_forecast_quantile() %>%
#' score()
#'
#' pairwise <- get_pairwise_comparisons(scores, by = "target_type")
#' pairwise2 <- get_pairwise_comparisons(
#' scores, by = "target_type", baseline = "EuroCOVIDhub-baseline"
Expand Down
51 changes: 38 additions & 13 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
#' @export
#' @examples
#' library(ggplot2)
#' scores <- score(as_forecast_quantile(example_quantile))
#' library(magrittr) # pipe operator
#' scores <- example_quantile %>%
#' as_forecast_quantile %>%
#' score()
#' scores <- summarise_scores(scores, by = c("model", "target_type"))
#'
#' plot_wis(scores,
Expand Down Expand Up @@ -111,7 +114,10 @@ plot_wis <- function(scores,
#' @importFrom checkmate assert_subset
#' @export
#' @examples
#' scores <- score(as_forecast_quantile(example_quantile))
#' library(magrittr) # pipe operator
#' scores <- example_quantile %>%
#' as_forecast_quantile %>%
#' score()
#' scores <- summarise_scores(scores, by = c("model", "target_type"))
#' scores <- summarise_scores(
#' scores, by = c("model", "target_type"),
Expand Down Expand Up @@ -309,7 +315,10 @@ plot_quantile_coverage <- function(coverage,
#' @export
#' @examples
#' library(ggplot2)
#' scores <- score(as_forecast_quantile(example_quantile))
#' library(magrittr) # pipe operator
#' scores <- example_quantile %>%
#' as_forecast_quantile %>%
#' score()
#' pairwise <- get_pairwise_comparisons(scores, by = "target_type")
#' plot_pairwise_comparisons(pairwise, type = "mean_scores_ratio") +
#' facet_wrap(~target_type)
Expand Down Expand Up @@ -444,6 +453,7 @@ plot_pairwise_comparisons <- function(comparison_result,
#' \dontshow{
#' data.table::setDTthreads(2) # restricts number of cores used on CRAN
#' }
#' library(magrittr) # pipe operator
#'
#' # PIT histogram in vector based format
#' observed <- rnorm(30, mean = 1:30)
Expand All @@ -452,11 +462,15 @@ plot_pairwise_comparisons <- function(comparison_result,
#' plot_pit(pit)
#'
#' # quantile-based pit
#' pit <- get_pit(as_forecast_quantile(example_quantile), by = "model")
#' pit <- example_quantile %>%
#' as_forecast_quantile() %>%
#' get_pit(by = "model")
#' plot_pit(pit, breaks = seq(0.1, 1, 0.1))
#'
#' # sample-based pit
#' pit <- get_pit(as_forecast_sample(example_sample_discrete), by = "model")
#' pit <- example_sample_discrete %>%
#' as_forecast_sample %>%
#' get_pit(by = "model")
#' plot_pit(pit)
#' @importFrom ggplot2 ggplot aes xlab ylab geom_histogram stat theme_light after_stat
#' @importFrom checkmate assert check_set_equal check_number
Expand Down Expand Up @@ -576,10 +590,10 @@ plot_pit <- function(pit,
#' @export
#' @examples
#' library(ggplot2)
#' forecast_counts <- get_forecast_counts(
#' as_forecast_quantile(example_quantile),
#' by = c("model", "target_type", "target_end_date")
#' )
#' library(magrittr) # pipe operator
#' forecast_counts <- example_quantile %>%
#' as_forecast_quantile %>%
#' get_forecast_counts(by = c("model", "target_type", "target_end_date"))
#' plot_forecast_counts(
#' forecast_counts, x = "target_end_date", show_counts = FALSE
#' ) +
Expand Down Expand Up @@ -647,10 +661,13 @@ plot_forecast_counts <- function(forecast_counts,
#' @export
#' @return A ggplot object with a visualisation of correlations between metrics
#' @examples
#' scores <- score(as_forecast_quantile(example_quantile))
#' correlations <- get_correlations(
#' summarise_scores(scores)
#' )
#' library(magrittr) # pipe operator
#' scores <- example_quantile %>%
#' as_forecast_quantile %>%
#' score()
#' correlations <- scores %>%
#' summarise_scores() %>%
#' get_correlations()
#' plot_correlations(correlations, digits = 2)

plot_correlations <- function(correlations, digits = NULL) {
Expand Down Expand Up @@ -715,6 +732,14 @@ plot_correlations <- function(correlations, digits = NULL) {
return(plot)
}


# helper function to obtain lower triangle of matrix
get_lower_tri <- function(cormat) {
cormat[lower.tri(cormat)] <- NA
return(cormat)
}


#' @title Scoringutils ggplot2 theme
#'
#' @description
Expand Down
4 changes: 3 additions & 1 deletion R/summarise_scores.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
#' data.table::setDTthreads(2) # restricts number of cores used on CRAN
#' }
#' library(magrittr) # pipe operator
#' scores <- score(as_forecast_sample(example_sample_continuous))
#' scores <- example_sample_continuous %>%
#' as_forecast_sample() %>%
#' score()
#'
#' # get scores by model
#' summarise_scores(scores, by = "model")
Expand Down
5 changes: 4 additions & 1 deletion R/utils_data_handling.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
#' @keywords data-handling
#' @export
#' @examples
#' sample_to_quantile(as_forecast_sample(example_sample_discrete))
#' library(magrittr) # pipe operator
#' example_sample_discrete %>%
#' as_forecast_sample() %>%
#' sample_to_quantile()
sample_to_quantile <- function(forecast,
quantile_level = c(0.05, 0.25, 0.5, 0.75, 0.95),
type = 7) {
Expand Down
8 changes: 4 additions & 4 deletions man/customise_metric.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/get_correlations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/get_forecast_counts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/get_pairwise_comparisons.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions man/log_shift.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions man/plot_correlations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/plot_forecast_counts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/plot_heatmap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/plot_pairwise_comparisons.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions man/plot_pit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6f5bc1b

Please sign in to comment.