Skip to content

Commit 33e0a3e

Browse files
committed
Move PIT validation to ppc_loo_pit_data() entry point using validate_pit()
1 parent 66785dc commit 33e0a3e

3 files changed

Lines changed: 14 additions & 36 deletions

File tree

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# bayesplot (development version)
22

3-
* Fix `.kde_correction()` crash on empty vector after removing infinite PIT values by adding guards for insufficient finite values and all-NA convolution results.
3+
* Validate user-provided `pit` values in `ppc_loo_pit_data()` using `validate_pit()` to reject invalid inputs (non-numeric, out of range, NAs) at the entry point instead of in internal helpers.
44
* Eliminate redundant data processing in `mcmc_areas_data()` by reusing the prepared MCMC array for both interval and density computation.
55
* Validate equal chain lengths in `validate_df_with_chain()`, reject missing
66
chain labels, and renumber data-frame chain labels internally when converting

R/ppc-loo.R

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ ppc_loo_pit_data <-
302302
boundary_correction = TRUE,
303303
grid_len = 512) {
304304
if (!is.null(pit)) {
305-
stopifnot(is.numeric(pit), is_vector_or_1Darray(pit))
305+
pit <- validate_pit(pit)
306306
inform("'pit' specified so ignoring 'y','yrep','lw' if specified.")
307307
} else {
308308
suggested_package("rstantools")
@@ -795,18 +795,6 @@ ppc_loo_ribbon <-
795795
# Generate boundary corrected values via a linear convolution using a
796796
# 1-D Gaussian window filter. This method uses the "reflection method"
797797
# to estimate these pvalues and helps speed up the code
798-
if (any(is.infinite(x))) {
799-
warn(paste(
800-
"Ignored", sum(is.infinite(x)),
801-
"Non-finite PIT values are invalid for KDE boundary correction method"
802-
))
803-
x <- x[is.finite(x)]
804-
}
805-
806-
if (length(x) < 2) {
807-
abort("Not enough finite PIT values for KDE boundary correction.")
808-
}
809-
810798
if (grid_len < 100) {
811799
grid_len <- 100
812800
}

tests/testthat/test-ppc-loo.R

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,22 @@ test_that("ppc_loo_pit_overlay works with boundary_correction=FALSE", {
5959
expect_gg(p1)
6060
})
6161

62-
test_that(".kde_correction warns when PIT values are non-finite", {
63-
set.seed(123)
64-
pit_vals <- c(stats::runif(500), Inf)
65-
expect_warning(
66-
out <- .kde_correction(pit_vals, bw = "nrd0", grid_len = 128),
67-
"Non-finite PIT values are invalid"
62+
test_that("ppc_loo_pit_data validates user-provided pit values", {
63+
expect_error(
64+
ppc_loo_pit_data(pit = c(0.5, Inf)),
65+
"between 0 and 1"
6866
)
69-
expect_type(out, "list")
70-
expect_true(all(c("xs", "bc_pvals") %in% names(out)))
71-
expect_equal(length(out$xs), 128)
72-
expect_equal(length(out$bc_pvals), 128)
73-
})
74-
75-
test_that(".kde_correction errors when all PIT values are infinite", {
76-
pit_vals <- c(Inf, -Inf, Inf)
7767
expect_error(
78-
.kde_correction(pit_vals, bw = "nrd0", grid_len = 128),
79-
"Not enough finite PIT values"
68+
ppc_loo_pit_data(pit = c(-1, 0.5)),
69+
"between 0 and 1"
70+
)
71+
expect_error(
72+
ppc_loo_pit_data(pit = c(0.5, NA)),
73+
"NAs not allowed"
8074
)
81-
})
82-
83-
test_that(".kde_correction errors when only one finite PIT value remains", {
84-
pit_vals <- c(Inf, 0.5, -Inf)
8575
expect_error(
86-
.kde_correction(pit_vals, bw = "nrd0", grid_len = 128),
87-
"Not enough finite PIT values"
76+
ppc_loo_pit_data(pit = "not numeric"),
77+
"is.numeric"
8878
)
8979
})
9080

0 commit comments

Comments
 (0)