Skip to content

Commit 94c89e0

Browse files
committed
Warn when neff ratios exceed 1
1 parent a30a706 commit 94c89e0

3 files changed

Lines changed: 11 additions & 3 deletions

File tree

NEWS.md

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

3+
* `mcmc_neff()` and `mcmc_neff_hist()` now warn when any neff ratios are greater than 1, as this is unusual and may indicate a problem with the model or sampler.
34
* Use `rlang::warn()` and `rlang::inform()` for selected PPC user messages instead of base `warning()` and `message()`.
45
* Standardize input validation errors in `ppc_km_overlay()` and interpolation helpers to use `rlang::abort()` for consistent error handling.
56
* Fix assignment-in-call bug in `mcmc_rank_ecdf()` (#).

R/mcmc-diagnostics.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,12 @@ validate_neff_ratio <- function(x) {
658658
if (any(x < 0, na.rm = TRUE)) {
659659
abort("All neff ratios must be positive.")
660660
}
661+
if (any(x > 1, na.rm = TRUE)) {
662+
warn(paste0(
663+
"Some neff ratios are greater than 1. ",
664+
"This is unusual and may indicate a problem with your model or MCMC sampler."
665+
))
666+
}
661667
x
662668
}
663669

tests/testthat/test-mcmc-diagnostics.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ test_that("rhat and neff plots return a ggplot object", {
1919
rhat <- setNames(runif(5, 1, 1.5), paste0("alpha[", 1:5, "]"))
2020
expect_gg(mcmc_rhat(rhat))
2121

22-
# doesn't error with ratios > 1 (not common but can happen)
23-
expect_gg(mcmc_neff(ratio = c(0.5, 1, 1.25)))
24-
expect_gg(mcmc_neff(ratio = c(0.5, 1, 2)))
22+
# doesn't error with ratios > 1 (not common but can happen), but does warn
23+
expect_warning(mcmc_neff(ratio = c(0.5, 1, 1.25)), "greater than 1")
24+
expect_warning(mcmc_neff(ratio = c(0.5, 1, 2)), "greater than 1")
2525
})
2626

2727
test_that("rhat and neff plot functions throw correct errors & warnings", {
@@ -34,6 +34,7 @@ test_that("rhat and neff plot functions throw correct errors & warnings", {
3434

3535
# need ratios between 0 and 1
3636
expect_error(mcmc_neff(c(-1, 0.5, 0.7)), "must be positive")
37+
expect_warning(mcmc_neff(c(0.5, 1.1, 1.5)), "greater than 1")
3738

3839
# drop NAs and warn
3940
expect_warning(mcmc_rhat(c(1, 1, NA)), "Dropped 1 NAs")

0 commit comments

Comments
 (0)