From 1f3d4ce5da9d2e37103f1ac50f18fd7c48432792 Mon Sep 17 00:00:00 2001 From: Utkarsh Date: Mon, 13 Apr 2026 15:52:06 +0530 Subject: [PATCH 1/3] allow NAs in prepare_mcmc_array --- NEWS.md | 1 + R/helpers-mcmc.R | 4 +++- tests/testthat/test-helpers-mcmc.R | 9 ++++++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index fceb6a8f..b319f9ae 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # bayesplot (development version) +* `prepare_mcmc_array()` now warns instead of erroring on `NA`s in the input. * `ppc_ecdf_overlay()`, `ppc_ecdf_overlay_grouped()`, and `ppd_ecdf_overlay()` now always use `geom_step()`. The `discrete` argument is deprecated. * Fixed missing `drop = FALSE` in `nuts_params.CmdStanMCMC()`. * Replace `apply()` with `storage.mode()` for integer-to-numeric matrix conversion in `validate_predictions()`. diff --git a/R/helpers-mcmc.R b/R/helpers-mcmc.R index 0f3970d9..eb0b9de1 100644 --- a/R/helpers-mcmc.R +++ b/R/helpers-mcmc.R @@ -29,7 +29,9 @@ prepare_mcmc_array <- function(x, abort("Arrays should have 2 or 3 dimensions. See help('MCMC-overview').") } if (anyNA(x)) { - abort("NAs not allowed in 'x'.") + warn( + "NAs were found in 'x'. `prepare_mcmc_array()` does not remove them; some plots may render with missing values dropped, while summary functions (e.g. intervals, densities, diagnostics) may produce misleading results or error. Consider removing NAs before plotting or summarizing." + ) } if (rlang::is_quosures(pars)) { diff --git a/tests/testthat/test-helpers-mcmc.R b/tests/testthat/test-helpers-mcmc.R index 409b0b99..6e538480 100644 --- a/tests/testthat/test-helpers-mcmc.R +++ b/tests/testthat/test-helpers-mcmc.R @@ -240,9 +240,12 @@ test_that("transformations recycled properly if not a named list", { # prepare_mcmc_array ------------------------------------------------------ -test_that("prepare_mcmc_array errors if NAs", { - arr[1,1,1] <- NA - expect_error(prepare_mcmc_array(arr), "NAs not allowed") +test_that("prepare_mcmc_array warns but does not error if NAs", { + arr_na <- arr + arr_na[1,1,1] <- NA + expect_warning(out <- prepare_mcmc_array(arr_na), "NAs were found") + expect_s3_class(out, "mcmc_array") + expect_true(anyNA(out)) }) test_that("prepare_mcmc_array processes non-array input types correctly", { # errors are mostly covered by tests of the many internal functions above From 19ceac549ef02e62f821bafb9ba816a56b36e774 Mon Sep 17 00:00:00 2001 From: Utkarsh Date: Thu, 16 Apr 2026 11:01:40 +0530 Subject: [PATCH 2/3] shorten NA warning and add vdiffr tests for NA draws --- R/helpers-mcmc.R | 2 +- .../mcmc-traces/mcmc-trace-na-parameter.svg | 112 +++++++++++++++ .../mcmc-trace-partial-na-parameter.svg | 130 ++++++++++++++++++ tests/testthat/test-mcmc-traces.R | 27 ++++ 4 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/_snaps/mcmc-traces/mcmc-trace-na-parameter.svg create mode 100644 tests/testthat/_snaps/mcmc-traces/mcmc-trace-partial-na-parameter.svg diff --git a/R/helpers-mcmc.R b/R/helpers-mcmc.R index eb0b9de1..6e8140fd 100644 --- a/R/helpers-mcmc.R +++ b/R/helpers-mcmc.R @@ -30,7 +30,7 @@ prepare_mcmc_array <- function(x, } if (anyNA(x)) { warn( - "NAs were found in 'x'. `prepare_mcmc_array()` does not remove them; some plots may render with missing values dropped, while summary functions (e.g. intervals, densities, diagnostics) may produce misleading results or error. Consider removing NAs before plotting or summarizing." + "NAs found in 'x'. These are passed through as-is and may affect the resulting plots." ) } diff --git a/tests/testthat/_snaps/mcmc-traces/mcmc-trace-na-parameter.svg b/tests/testthat/_snaps/mcmc-traces/mcmc-trace-na-parameter.svg new file mode 100644 index 00000000..d272a8c2 --- /dev/null +++ b/tests/testthat/_snaps/mcmc-traces/mcmc-trace-na-parameter.svg @@ -0,0 +1,112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +theta[1,3] + + + + + + + + + +theta[2,3] + + + + + + + + + +0 +100 +200 +300 +400 +500 + + + + + + +0 +100 +200 +300 +400 +500 + +-3 +-2 +-1 +0 +1 +2 + + + + + + +Chain + + + + +1 +2 +3 +4 +mcmc_trace (NA parameter) + + diff --git a/tests/testthat/_snaps/mcmc-traces/mcmc-trace-partial-na-parameter.svg b/tests/testthat/_snaps/mcmc-traces/mcmc-trace-partial-na-parameter.svg new file mode 100644 index 00000000..31f9c7c3 --- /dev/null +++ b/tests/testthat/_snaps/mcmc-traces/mcmc-trace-partial-na-parameter.svg @@ -0,0 +1,130 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +theta[1,3] + + + + + + + + + +theta[2,3] + + + + + + + + + +0 +100 +200 +300 +400 +500 + + + + + + + +0 +100 +200 +300 +400 +500 + +-2 +0 +2 +4 + + + + + +-3 +-2 +-1 +0 +1 +2 + + + + + + +Chain + + + + +1 +2 +3 +4 +mcmc_trace (partial NA parameter) + + diff --git a/tests/testthat/test-mcmc-traces.R b/tests/testthat/test-mcmc-traces.R index 113e1ef8..5b8715f4 100644 --- a/tests/testthat/test-mcmc-traces.R +++ b/tests/testthat/test-mcmc-traces.R @@ -165,6 +165,33 @@ test_that("mcmc_trace renders correctly", { vdiffr::expect_doppelganger("mcmc_trace (iter1 offset)", p_iter1) }) +# https://github.com/stan-dev/bayesplot/issues/250 +test_that("mcmc_trace renders correctly with NAs in draws", { + testthat::skip_on_cran() + testthat::skip_if_not_installed("vdiffr") + skip_on_r_oldrel() + + set.seed(250) + draws_full_na <- array( + rnorm(500 * 4 * 2), + dim = c(500, 4, 2), + dimnames = list(NULL, NULL, c("theta[1,3]", "theta[2,3]")) + ) + draws_full_na[, , "theta[2,3]"] <- NA + + draws_partial_na <- draws_full_na + draws_partial_na[, , "theta[2,3]"] <- rnorm(500 * 4) + draws_partial_na[10:100, , "theta[2,3]"] <- NA + + suppressWarnings({ + p_full_na <- mcmc_trace(draws_full_na) + p_partial_na <- mcmc_trace(draws_partial_na) + }) + + vdiffr::expect_doppelganger("mcmc_trace (NA parameter)", p_full_na) + vdiffr::expect_doppelganger("mcmc_trace (partial NA parameter)", p_partial_na) +}) + test_that("mcmc_rank_overlay renders correctly", { testthat::skip_on_cran() testthat::skip_if_not_installed("vdiffr") From 10a2919089f111a2add9fc18d013f5a6f34488f0 Mon Sep 17 00:00:00 2001 From: Utkarsh Date: Thu, 16 Apr 2026 11:19:12 +0530 Subject: [PATCH 3/3] fix stale NA warning regex in prepare_mcmc_array test --- tests/testthat/test-helpers-mcmc.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-helpers-mcmc.R b/tests/testthat/test-helpers-mcmc.R index f0ba5408..e70937dc 100644 --- a/tests/testthat/test-helpers-mcmc.R +++ b/tests/testthat/test-helpers-mcmc.R @@ -254,8 +254,8 @@ test_that("transformations recycled properly if not a named list", { # prepare_mcmc_array ------------------------------------------------------ test_that("prepare_mcmc_array warns but does not error if NAs", { arr_na <- arr - arr_na[1,1,1] <- NA - expect_warning(out <- prepare_mcmc_array(arr_na), "NAs were found") + arr_na[1, 1, 1] <- NA + expect_warning(out <- prepare_mcmc_array(arr_na), "NAs found in 'x'") expect_s3_class(out, "mcmc_array") expect_true(anyNA(out)) })