diff --git a/NEWS.md b/NEWS.md index a3104d27d..3b7bbe170 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,8 +8,10 @@ This minor update addresses comments made by review from the Journal of Statisti - For clarity, the output column in `avail_forecasts()` was renamed from "Number forecasts" to "count". - `available_forecasts()` now also displays combinations where there are 0 forecasts, instead of silently dropping corresponding rows. - `plot_avail_forecasts()` has been deprecated in favour of an S3 method for `plot()`. An alias is still available, but will be removed in the future. +- remove hard-coded rounding value for `correlation()`. Previously, the function always rounded correlations to two digits. Instead, a new argument, `digits` was introduced and the default set to 0, meaning that no rounding takes place. - the deprecated `..density..` was replaced with `after_stat(density)` in ggplot calls. - files ending in ".Rda" were renamed to ".rds" where appropriate when used together with `saveRDS()` or readRDS()`. +- add documentation for the return value of `summarise_scores()`. # scoringutils 1.2.1 diff --git a/R/correlations.R b/R/correlations.R index 5b8233576..2033a8a00 100644 --- a/R/correlations.R +++ b/R/correlations.R @@ -7,6 +7,8 @@ #' @param metrics A character vector with the metrics to show. If set to #' `NULL` (default), all metrics present in `scores` will #' be shown +#' @param digits A number indicating how many decimal places the result should +#' be rounded to. By default (`digits = NULL`) no rounding takes place. #' @inheritParams pairwise_comparison #' @return A data.table with correlations for the different metrics #' @importFrom data.table setDT @@ -15,9 +17,10 @@ #' @keywords scoring #' @examples #' scores <- score(example_quantile) -#' correlation(scores) +#' correlation(scores, digits = 2) correlation <- function(scores, - metrics = NULL) { + metrics = NULL, + digits = NULL) { metrics <- check_metrics(metrics) # check metrics are present @@ -43,7 +46,11 @@ correlation <- function(scores, df <- df[, .SD, .SDcols = names(df) %in% metrics] # define correlation matrix - cor_mat <- round(cor(as.matrix(df)), 2) + cor_mat <- cor(as.matrix(df)) + + if (!is.null(digits)) { + cor_mat <- round(cor_mat, digits) + } correlations <- setDT(as.data.frame((cor_mat)), keep.rownames = TRUE diff --git a/R/plot.R b/R/plot.R index cf6607b0c..ce565ef42 100644 --- a/R/plot.R +++ b/R/plot.R @@ -1065,7 +1065,8 @@ plot_avail_forecasts <- function(available_forecasts, #' @examples #' scores <- score(example_quantile) #' correlations <- correlation( -#' summarise_scores(scores) +#' summarise_scores(scores), +#' digits = 2 #' ) #' plot_correlation(correlations) diff --git a/R/summarise_scores.R b/R/summarise_scores.R index 5283a8061..d3c1847c2 100644 --- a/R/summarise_scores.R +++ b/R/summarise_scores.R @@ -41,6 +41,9 @@ #' @param ... additional parameters that can be passed to the summary function #' provided to `fun`. For more information see the documentation of the #' respective function. +#' @return a data.table with summarised scores. Scores are summarised according +#' to the names of the columns of the original data specified in `by` or +#' `across` using the `fun` passed to `summarise_scores()`. #' @examples #' data.table::setDTthreads(1) # only needed to avoid issues on CRAN #' library(magrittr) # pipe operator diff --git a/inst/manuscript/R/00-standalone-Figure-replication.R b/inst/manuscript/R/00-standalone-Figure-replication.R index f6d801697..3b1b66b9c 100644 --- a/inst/manuscript/R/00-standalone-Figure-replication.R +++ b/inst/manuscript/R/00-standalone-Figure-replication.R @@ -654,7 +654,7 @@ p1 / p2 + correlations <- example_quantile |> score() |> summarise_scores() |> - correlation() + correlation(digits = 2) correlations |> glimpse() diff --git a/inst/manuscript/manuscript.Rmd b/inst/manuscript/manuscript.Rmd index b0b14f728..a07a7300d 100644 --- a/inst/manuscript/manuscript.Rmd +++ b/inst/manuscript/manuscript.Rmd @@ -637,7 +637,7 @@ It may sometimes be interesting to see how different scores correlate with each correlations <- example_quantile |> score() |> summarise_scores() |> - correlation() + correlation(digits = 2) correlations |> glimpse() diff --git a/man/correlation.Rd b/man/correlation.Rd index 77de3a469..59bda9e42 100644 --- a/man/correlation.Rd +++ b/man/correlation.Rd @@ -4,7 +4,7 @@ \alias{correlation} \title{Correlation Between Metrics} \usage{ -correlation(scores, metrics = NULL) +correlation(scores, metrics = NULL, digits = NULL) } \arguments{ \item{scores}{A data.table of scores as produced by \code{\link[=score]{score()}}.} @@ -12,6 +12,9 @@ correlation(scores, metrics = NULL) \item{metrics}{A character vector with the metrics to show. If set to \code{NULL} (default), all metrics present in \code{scores} will be shown} + +\item{digits}{A number indicating how many decimal places the result should +be rounded to. By default (\code{digits = NULL}) no rounding takes place.} } \value{ A data.table with correlations for the different metrics @@ -22,6 +25,6 @@ scores as produced by \code{\link[=score]{score()}}. } \examples{ scores <- score(example_quantile) -correlation(scores) +correlation(scores, digits = 2) } \keyword{scoring} diff --git a/man/plot_correlation.Rd b/man/plot_correlation.Rd index 390c90f20..0bade90cb 100644 --- a/man/plot_correlation.Rd +++ b/man/plot_correlation.Rd @@ -20,7 +20,8 @@ Plots a heatmap of correlations between different metrics \examples{ scores <- score(example_quantile) correlations <- correlation( - summarise_scores(scores) + summarise_scores(scores), + digits = 2 ) plot_correlation(correlations) } diff --git a/man/summarise_scores.Rd b/man/summarise_scores.Rd index ed63cf1af..811f9b2df 100644 --- a/man/summarise_scores.Rd +++ b/man/summarise_scores.Rd @@ -77,6 +77,11 @@ respect to a baseline model.} provided to \code{fun}. For more information see the documentation of the respective function.} } +\value{ +a data.table with summarised scores. Scores are summarised according +to the names of the columns of the original data specified in \code{by} or +\code{across} using the \code{fun} passed to \code{summarise_scores()}. +} \description{ Summarise scores as produced by \code{\link[=score]{score()}} } diff --git a/tests/testthat/test-plot_correlation.R b/tests/testthat/test-plot_correlation.R index 9d6280d6e..be29faf3b 100644 --- a/tests/testthat/test-plot_correlation.R +++ b/tests/testthat/test-plot_correlation.R @@ -1,5 +1,5 @@ test_that("plot_correlation() works as expected", { - correlations <- correlation(summarise_scores(scores)) + correlations <- correlation(summarise_scores(scores), digits = 2) p <- plot_correlation(correlations) expect_s3_class(p, "ggplot") skip_on_cran() diff --git a/vignettes/scoringutils.Rmd b/vignettes/scoringutils.Rmd index b4dd0bf16..662bb7552 100644 --- a/vignettes/scoringutils.Rmd +++ b/vignettes/scoringutils.Rmd @@ -375,7 +375,7 @@ Visualising correlations: example_quantile %>% score() %>% summarise_scores() %>% - correlation() %>% + correlation(digits = 2) %>% plot_correlation() ```