Skip to content

Commit

Permalink
rename validate() to validate_forecast()
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosbosse committed Dec 16, 2023
1 parent 480189e commit dd1a662
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 39 deletions.
10 changes: 5 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ S3method(score,scoringutils_binary)
S3method(score,scoringutils_point)
S3method(score,scoringutils_quantile)
S3method(score,scoringutils_sample)
S3method(validate,scoringutils_binary)
S3method(validate,scoringutils_point)
S3method(validate,scoringutils_quantile)
S3method(validate,scoringutils_sample)
S3method(validate_forecast,scoringutils_binary)
S3method(validate_forecast,scoringutils_point)
S3method(validate_forecast,scoringutils_quantile)
S3method(validate_forecast,scoringutils_sample)
export(abs_error)
export(add_coverage)
export(add_pairwise_comparison)
Expand Down Expand Up @@ -69,7 +69,7 @@ export(summarize_scores)
export(theme_scoringutils)
export(transform_forecasts)
export(underprediction)
export(validate)
export(validate_forecast)
export(validate_general)
export(wis)
importFrom(Metrics,ae)
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The update introduces breaking changes. If you want to keep using the older vers
- `observed`: numeric, either a scalar or a vector
- `predicted`: numeric, a vector (if `observed` is a scalar) or a matrix (if `observed` is a vector)
- `quantile`: numeric, a vector with quantile-levels. Can alternatively be a matrix of the same shape as `predicted`.
- `check_forecasts()` was replaced by a different workflow. There now is a function, `as_forecast()`, that determines forecast type of the data, constructs a forecasting object and validates it using the function `validate()` (a generic that dispatches the correct method based on the forecast type). Objects of class `forecast_binary`, `forecast_point`, `forecast_sample` and `forecast_quantile` have print methods that fulfill the functionality of `check_forecasts()`.
- `check_forecasts()` was replaced by a different workflow. There now is a function, `as_forecast()`, that determines forecast type of the data, constructs a forecasting object and validates it using the function `validate_forecast()` (a generic that dispatches the correct method based on the forecast type). Objects of class `forecast_binary`, `forecast_point`, `forecast_sample` and `forecast_quantile` have print methods that fulfill the functionality of `check_forecasts()`.
- The functionality for computing pairwise comparisons was now split from `summarise_scores()`. Instead of doing pairwise comparisons as part of summarising scores, a new function, `add_pairwise_comparison()`, was introduced that takes summarised scores as an input and adds pairwise comparisons to it.
- `add_coverage()` was reworked completely. It's new purpose is now to add coverage information to the raw forecast data (essentially fulfilling some of the functionality that was previously covered by `score_quantile()`)
- Support for the interval format was mostly dropped (see PR #525 by @nikosbosse and reviewed by @seabbs)
Expand Down
4 changes: 2 additions & 2 deletions R/get_-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ get_metrics <- function(scores) {
#' the columns that are protected, i.e. those returned by
#' [get_protected_columns()] as well as the names of the metrics that were
#' specified during scoring, if any.
#' @inheritParams validate
#' @inheritParams validate_forecast
#' @param check_conflict Whether or not to check whether there is a conflict
#' between a stored attribute and the inferred forecast unit. When you create
#' a forecast object, the forecast unit is stored as an attribute. If you
Expand Down Expand Up @@ -183,7 +183,7 @@ get_forecast_unit <- function(data, check_conflict = FALSE) {
#' @description Helper function to get the names of all columns in a data frame
#' that are protected columns.
#'
#' @inheritParams validate
#' @inheritParams validate_forecast
#'
#' @return A character vector with the names of protected columns in the data.
#' If data is `NULL` (default) then it returns a list of all columns that are
Expand Down
8 changes: 4 additions & 4 deletions R/score.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ score.default <- function(data, ...) {
#' @rdname score
#' @export
score.scoringutils_binary <- function(data, metrics = metrics_binary, ...) {
data <- validate(data)
data <- validate_forecast(data)
data <- remove_na_observed_predicted(data)
metrics <- validate_metrics(metrics)

Expand All @@ -95,7 +95,7 @@ score.scoringutils_binary <- function(data, metrics = metrics_binary, ...) {
#' @rdname score
#' @export
score.scoringutils_point <- function(data, metrics = metrics_point, ...) {
data <- validate(data)
data <- validate_forecast(data)
data <- remove_na_observed_predicted(data)
metrics <- validate_metrics(metrics)

Expand All @@ -112,7 +112,7 @@ score.scoringutils_point <- function(data, metrics = metrics_point, ...) {
#' @rdname score
#' @export
score.scoringutils_sample <- function(data, metrics = metrics_sample, ...) {
data <- validate(data)
data <- validate_forecast(data)
data <- remove_na_observed_predicted(data)
forecast_unit <- attr(data, "forecast_unit")
metrics <- validate_metrics(metrics)
Expand Down Expand Up @@ -149,7 +149,7 @@ score.scoringutils_sample <- function(data, metrics = metrics_sample, ...) {
#' @rdname score
#' @export
score.scoringutils_quantile <- function(data, metrics = metrics_quantile, ...) {
data <- validate(data)
data <- validate_forecast(data)
data <- remove_na_observed_predicted(data)
forecast_unit <- attr(data, "forecast_unit")
metrics <- validate_metrics(metrics)
Expand Down
17 changes: 10 additions & 7 deletions R/validate.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ as_forecast <- function(data) {
data <- new_scoringutils(data, paste0("scoringutils_", forecast_type))

# validate class
validate(data)
validate_forecast(data)
}


Expand All @@ -48,15 +48,18 @@ as_forecast <- function(data) {
#' @importFrom checkmate assert_data_frame
#' @export
#' @keywords check-forecasts
validate <- function(data, ...) {
UseMethod("validate")
#' @examples
#' forecast <- as_forecast(example_binary)
#' validate_forecast(forecast)
validate_forecast <- function(data, ...) {
UseMethod("validate_forecast")
}


#' @rdname validate
#' @export
#' @keywords check-forecasts
validate.scoringutils_binary <- function(data, ...) {
validate_forecast.scoringutils_binary <- function(data, ...) {
data <- validate_general(data)

columns_correct <- test_columns_not_present(data, c("sample_id", "quantile"))
Expand All @@ -77,7 +80,7 @@ validate.scoringutils_binary <- function(data, ...) {
#' @rdname validate
#' @export
#' @keywords check-forecasts
validate.scoringutils_point <- function(data, ...) {
validate_forecast.scoringutils_point <- function(data, ...) {
data <- validate_general(data)

input_check <- check_input_point(data$observed, data$predicted)
Expand All @@ -91,7 +94,7 @@ validate.scoringutils_point <- function(data, ...) {

#' @rdname validate
#' @export
validate.scoringutils_quantile <- function(data, ...) {
validate_forecast.scoringutils_quantile <- function(data, ...) {
data <- validate_general(data)
assert_numeric(data$quantile, lower = 0, upper = 1)
return(data[])
Expand All @@ -100,7 +103,7 @@ validate.scoringutils_quantile <- function(data, ...) {
#' @rdname validate
#' @export
#' @keywords check-forecasts
validate.scoringutils_sample <- function(data, ...) {
validate_forecast.scoringutils_sample <- function(data, ...) {
data <- validate_general(data)
return(data[])
}
Expand Down
4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ example_quantile %>%

### Scoring forecasts

Forecasts can be easily and quickly scored using the `score()` function. `score()` automatically tries to determine the `forecast_unit`, i.e. the set of columns that uniquely defines a single forecast, by taking all column names of the data into account. However, it is recommended to set the forecast unit manually using `set_forecast_unit()` as this may help to avoid errors, especially when scoringutils is used in automated pipelines. The function `set_forecast_unit()` will simply drop unneeded columns. To verify everything is in order, the function `validate()` should be used. The result of that check can then passed directly into `score()`. `score()` returns unsummarised scores, which in most cases is not what the user wants. Here we make use of additional functions from `scoringutils` to add empirical coverage-levels (`add_coverage()`), and scores relative to a baseline model (here chosen to be the EuroCOVIDhub-ensemble model). See the getting started vignette for more details. Finally we summarise these scores by model and target type.
Forecasts can be easily and quickly scored using the `score()` function. `score()` automatically tries to determine the `forecast_unit`, i.e. the set of columns that uniquely defines a single forecast, by taking all column names of the data into account. However, it is recommended to set the forecast unit manually using `set_forecast_unit()` as this may help to avoid errors, especially when scoringutils is used in automated pipelines. The function `set_forecast_unit()` will simply drop unneeded columns. To verify everything is in order, the function `validate_forecast()` should be used. The result of that check can then passed directly into `score()`. `score()` returns unsummarised scores, which in most cases is not what the user wants. Here we make use of additional functions from `scoringutils` to add empirical coverage-levels (`add_coverage()`), and scores relative to a baseline model (here chosen to be the EuroCOVIDhub-ensemble model). See the getting started vignette for more details. Finally we summarise these scores by model and target type.

```{r score-example}
example_quantile %>%
set_forecast_unit(c("location", "target_end_date", "target_type", "horizon", "model")) %>%
validate() %>%
validate_forecast() %>%
add_coverage() %>%
score() %>%
summarise_scores(
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ column names of the data into account. However, it is recommended to set
the forecast unit manually using `set_forecast_unit()` as this may help
to avoid errors, especially when scoringutils is used in automated
pipelines. The function `set_forecast_unit()` will simply drop unneeded
columns. To verify everything is in order, the function `validate()`
columns. To verify everything is in order, the function `validate_forecast()`
should be used. The result of that check can then passed directly into
`score()`. `score()` returns unsummarised scores, which in most cases is
not what the user wants. Here we make use of additional functions from
Expand All @@ -128,7 +128,7 @@ details. Finally we summarise these scores by model and target type.
``` r
example_quantile %>%
set_forecast_unit(c("location", "target_end_date", "target_type", "horizon", "model")) %>%
validate() %>%
validate_forecast() %>%
add_coverage() %>%
score() %>%
summarise_scores(
Expand Down
4 changes: 4 additions & 0 deletions man/as_forecast.Rd

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

22 changes: 7 additions & 15 deletions man/validate.Rd → man/validate_forecast.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ test_that("run_safely() works as expected", {
# })

# test_that("is_scoringutils_check() is working", {
# checked <- suppressMessages(validate(example_binary))
# checked <- suppressMessages(validate_forecast(example_binary))
# expect_true(is_scoringutils_check(checked))
#
# checked$cleaned_data <- NULL
Expand Down

0 comments on commit dd1a662

Please sign in to comment.