Skip to content

Commit

Permalink
Force output of pit() to be a data.table
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosbosse authored and seabbs committed Feb 19, 2024
1 parent b860863 commit 7b81152
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion R/pit.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 16 additions & 7 deletions tests/testthat/test-pit.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit 7b81152

Please sign in to comment.