From 7b81152ae5f883842b4dd564a227876eef97ab9e Mon Sep 17 00:00:00 2001 From: nikosbosse Date: Tue, 13 Feb 2024 06:51:23 -0600 Subject: [PATCH] Force output of pit() to be a data.table --- R/pit.R | 2 +- tests/testthat/test-pit.R | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/R/pit.R b/R/pit.R index b4ecd5b71..99b8cd780 100644 --- a/R/pit.R +++ b/R/pit.R @@ -202,7 +202,7 @@ pit <- function(data, ), by = c(get_forecast_unit(quantile_coverage)) ] - return(quantile_coverage[]) + return(as.data.table(quantile_coverage)[]) } # if prediction type is not quantile, calculate PIT values based on samples diff --git a/tests/testthat/test-pit.R b/tests/testthat/test-pit.R index adc57987d..19f1538e5 100644 --- a/tests/testthat/test-pit.R +++ b/tests/testthat/test-pit.R @@ -48,16 +48,25 @@ test_that("pit_sample() function works for continuous observed and predicted", { }) test_that("pit function works for continuous integer and quantile data", { - pit1 <- suppressMessages(pit(example_quantile, by = "model")) - pit2 <- suppressMessages(pit(example_continuous, + pit_quantile <- suppressMessages(pit(example_quantile, by = "model")) + pit_continuous <- suppressMessages(pit(example_continuous, by = c("model", "target_type") )) - pit3 <- suppressMessages(pit(example_integer, + pit_integer <- suppressMessages(pit(example_integer, by = c("model", "location") )) - expect_equal(names(pit1), c("model", "quantile_level", "pit_value")) - expect_equal(names(pit2), c("model", "target_type", "pit_value")) - expect_equal(names(pit3), c("model", "location", "pit_value")) -}) + expect_equal(names(pit_quantile), c("model", "quantile_level", "pit_value")) + expect_equal(names(pit_continuous), c("model", "target_type", "pit_value")) + expect_equal(names(pit_integer), c("model", "location", "pit_value")) + + # check printing works + testthat::expect_output(print(pit_quantile)) + testthat::expect_output(print(pit_continuous)) + testthat::expect_output(print(pit_integer)) + # check class is correct + expect_s3_class(pit_quantile, c("data.table", "data.frame"), exact = TRUE) + expect_s3_class(pit_continuous, c("data.table", "data.frame"), exact = TRUE) + expect_s3_class(pit_integer, c("data.table", "data.frame"), exact = TRUE) +})