Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #750 - Introduce a forecast_type argument in validate_forecast() #751

Merged
merged 6 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions R/forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,10 @@ as_forecast.default <- function(data,
data <- set_forecast_unit(data, forecast_unit)
}

# find forecast type
desired <- forecast_type
# assert forecast type is as expected
assert_forecast_type(data, desired = forecast_type)
forecast_type <- get_forecast_type(data)

if (!is.null(desired) && desired != forecast_type) {
#nolint start: object_usage_linter keyword_quote_linter
cli_abort(
c(
"!" = "Forecast type determined by scoringutils based on input:
{.val {forecast_type}}.",
"i" = "Desired forecast type: {.val {desired}}."
)
)
#nolint end
}

# produce warning if old format is suspected
# old quantile format
if (forecast_type == "point" && "quantile" %in% colnames(data)) {
Expand Down Expand Up @@ -194,15 +182,15 @@ as_forecast.default <- function(data,
#' @examples
#' forecast <- as_forecast(example_binary)
#' validate_forecast(forecast)
validate_forecast <- function(data, ...) {
validate_forecast <- function(data, forecast_type = NULL, ...) {
UseMethod("validate_forecast")
}


#' @importFrom cli cli_abort
#' @export
#' @keywords check-forecasts
validate_forecast.default <- function(data, ...) {
validate_forecast.default <- function(data, forecast_type = NULL, ...) {
cli_abort(
c(
"!" = "The input needs to be a forecast object.",
Expand All @@ -215,8 +203,9 @@ validate_forecast.default <- function(data, ...) {
#' @export
#' @importFrom cli cli_abort
#' @keywords check-forecasts
validate_forecast.forecast_binary <- function(data, ...) {
validate_forecast.forecast_binary <- function(data, forecast_type = NULL, ...) {
data <- validate_general(data)
assert_forecast_type(data, actual = "binary", desired = forecast_type)

columns_correct <- test_columns_not_present(
data, c("sample_id", "quantile_level")
Expand Down Expand Up @@ -248,8 +237,9 @@ validate_forecast.forecast_binary <- function(data, ...) {
#' @export
#' @importFrom cli cli_abort
#' @keywords check-forecasts
validate_forecast.forecast_point <- function(data, ...) {
validate_forecast.forecast_point <- function(data, forecast_type = NULL, ...) {
data <- validate_general(data)
assert_forecast_type(data, actual = "point", desired = forecast_type)
#nolint start: keyword_quote_linter object_usage_linter
input_check <- check_input_point(data$observed, data$predicted)
if (!is.logical(input_check)) {
Expand All @@ -268,8 +258,10 @@ validate_forecast.forecast_point <- function(data, ...) {
#' @export
#' @rdname validate_forecast
#' @keywords check-forecasts
validate_forecast.forecast_quantile <- function(data, ...) {
validate_forecast.forecast_quantile <- function(data,
forecast_type = NULL, ...) {
data <- validate_general(data)
assert_forecast_type(data, actual = "quantile", desired = forecast_type)
assert_numeric(data$quantile_level, lower = 0, upper = 1)
return(data[])
}
Expand All @@ -278,8 +270,9 @@ validate_forecast.forecast_quantile <- function(data, ...) {
#' @export
#' @rdname validate_forecast
#' @keywords check-forecasts
validate_forecast.forecast_sample <- function(data, ...) {
validate_forecast.forecast_sample <- function(data, forecast_type = NULL, ...) {
data <- validate_general(data)
assert_forecast_type(data, actual = "sample", desired = forecast_type)
return(data[])
}

Expand Down
27 changes: 27 additions & 0 deletions R/get_-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,33 @@ test_forecast_type_is_quantile <- function(data) {
}


#' Assert that forecast type is as expected
#' @param data A forecast object as produced by [as_forecast()].
#' @param actual The actual forecast type of the data
#' @param desired The desired forecast type of the data
#' @inherit document_assert_functions return
#' @importFrom cli cli_abort
#' @importFrom checkmate assert_character
#' @keywords internal_input_check
assert_forecast_type <- function(data,
actual = get_forecast_type(data),
desired = NULL) {
assert_character(desired, null.ok = TRUE)
if (!is.null(desired) && desired != actual) {
#nolint start: object_usage_linter keyword_quote_linter
cli_abort(
c(
"!" = "Forecast type determined by scoringutils based on input:
{.val {actual}}.",
"i" = "Desired forecast type: {.val {desired}}."
)
)
#nolint end
}
return(invisible(NULL))
}


#' @title Get type of a vector or matrix of observed values or predictions
#'
#' @description Internal helper function to get the type of a vector (usually
Expand Down
23 changes: 23 additions & 0 deletions man/assert_forecast_type.Rd

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

11 changes: 8 additions & 3 deletions man/validate_forecast.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test-forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,13 @@ test_that("validate_forecast.forecast_point() works as expected", {
"Input looks like a point forecast, but found the following issue"
)
})

test_that("validate_forecast() complains if the forecast type is wrong", {
test <- na.omit(data.table::copy(example_point))
test <- as_forecast(test)
expect_error(
validate_forecast(test, forecast_type = "quantile"),
"Forecast type determined by scoringutils based on input:"
)
})

Loading