From ba80f5b78da3df47c95e7881c5823fd92e3ad2d5 Mon Sep 17 00:00:00 2001 From: nikosbosse Date: Tue, 20 Feb 2024 18:25:20 -0600 Subject: [PATCH] Update tests --- man/as_forecast.Rd | 39 +++++++++++++++++++++++++++++-- tests/testthat/test-as_forecast.R | 31 ++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/man/as_forecast.Rd b/man/as_forecast.Rd index 8f8b23939..dbcbf6e74 100644 --- a/man/as_forecast.Rd +++ b/man/as_forecast.Rd @@ -7,12 +7,43 @@ \usage{ as_forecast(data, ...) -\method{as_forecast}{default}(data, ...) +\method{as_forecast}{default}( + data, + observed = NULL, + predicted = NULL, + model = NULL, + forecast_unit = NULL, + quantile_level = NULL, + sample_id = NULL, + ... +) } \arguments{ \item{data}{A data.frame or data.table with predicted and observed values.} \item{...}{additional arguments} + +\item{observed}{(optional) Name of the column in \code{data} that contains the +observed values. This column will be renamed to "observed".} + +\item{predicted}{(optional) Name of the column in \code{data} that contains the +predicted values. This column will be renamed to "predicted".} + +\item{model}{(optional) Name of the column in \code{data} that contains the names +of the models/forecasters that generated the predicted values. +This column will be renamed to "model".} + +\item{forecast_unit}{(optional) Name of the columns in \code{data} (after +renaming) that denote the unit of a single forecast. +See \code{\link[=get_forecast_unit]{get_forecast_unit()}} for details.} + +\item{quantile_level}{(optional) Name of the column in \code{data} that contains +the quantile level of the predicted values. This column will be renamed to +"quantile_level". Only applicable to quantile-based forecasts.} + +\item{sample_id}{(optional) Name of the column in \code{data} that contains the +sample id. This column will be renamed to "sample_id". Only applicable to +sample-based forecasts.} } \value{ Depending on the forecast type, an object of class @@ -68,6 +99,10 @@ For more information see the vignettes and the example data \examples{ as_forecast(example_binary) -as_forecast(example_quantile) +as_forecast( + example_quantile, + forecast_unit = c("model", "target_type", "target_end_date", + "horizon", "location") +) } \keyword{check-forecasts} diff --git a/tests/testthat/test-as_forecast.R b/tests/testthat/test-as_forecast.R index df9ca9a95..158d26d86 100644 --- a/tests/testthat/test-as_forecast.R +++ b/tests/testthat/test-as_forecast.R @@ -7,6 +7,37 @@ test_that("Running `as_forecast()` twice returns the same object", { ) }) +test_that("as_forecast() works as expected", { + test <- na.omit(data.table::copy(example_quantile)) + expect_s3_class(as_forecast(test), "forecast_quantile") + + # expect error when arguments are not correct + expect_error(as_forecast(test, observed = 3), "Must be of type 'character'") + expect_error(as_forecast(test, quantile_level = c("1", "2")), "Must have length 1") + expect_error(as_forecast(test, observed = "missing"), "Must be a subset of") + + # expect no condition with columns already present + expect_no_condition( + as_forecast(test, observed = "observed", predicted = "predicted", + forecast_unit = c("location", "model", "target_type", + "target_end_date", "horizon"), + quantile_level = "quantile_level") + ) + + # additional test with renaming the model column + test <- na.omit(data.table::copy(example_continuous)) + setnames(test, old = c("observed", "predicted", "sample_id", "model"), + new = c("obs", "pred", "sample", "mod")) + expect_no_condition( + as_forecast(test, + observed = "obs", predicted = "pred", model = "mod", + forecast_unit = c("location", "model", "target_type", + "target_end_date", "horizon"), + sample_id = "sample") + ) +}) + + test_that("is_forecast() works as expected", { ex_binary <- suppressMessages(as_forecast(example_binary)) ex_point <- suppressMessages(as_forecast(example_point))