diff --git a/NEWS.md b/NEWS.md index 75d62cd8..7bd1ebdc 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. * Fixed `validate_chain_list()` colnames check to compare all chains, not just the first two. * Added test verifying `legend_move("none")` behaves equivalently to `legend_none()`. * Added singleton-dimension edge-case tests for exported `_data()` functions. diff --git a/R/helpers-mcmc.R b/R/helpers-mcmc.R index 179ec020..3a8bac49 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 found in 'x'. These are passed through as-is and may affect the resulting plots." + ) } if (rlang::is_quosures(pars)) { 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-helpers-mcmc.R b/tests/testthat/test-helpers-mcmc.R index 84347f23..e70937dc 100644 --- a/tests/testthat/test-helpers-mcmc.R +++ b/tests/testthat/test-helpers-mcmc.R @@ -252,9 +252,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 found in 'x'") + 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 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")