Skip to content

Commit fce2074

Browse files
authored
Merge pull request #539 from utkarshpawade/fix/allow-nas-prepare-mcmc-array-250
Allow NAs in prepare_mcmc_array
2 parents 9d6a95b + 10a2919 commit fce2074

6 files changed

Lines changed: 279 additions & 4 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+
* `prepare_mcmc_array()` now warns instead of erroring on `NA`s in the input.
34
* Fixed `validate_chain_list()` colnames check to compare all chains, not just the first two.
45
* Added test verifying `legend_move("none")` behaves equivalently to `legend_none()`.
56
* Added singleton-dimension edge-case tests for exported `_data()` functions.

R/helpers-mcmc.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ prepare_mcmc_array <- function(x,
2929
abort("Arrays should have 2 or 3 dimensions. See help('MCMC-overview').")
3030
}
3131
if (anyNA(x)) {
32-
abort("NAs not allowed in 'x'.")
32+
warn(
33+
"NAs found in 'x'. These are passed through as-is and may affect the resulting plots."
34+
)
3335
}
3436

3537
if (rlang::is_quosures(pars)) {

tests/testthat/_snaps/mcmc-traces/mcmc-trace-na-parameter.svg

Lines changed: 112 additions & 0 deletions
Loading

tests/testthat/_snaps/mcmc-traces/mcmc-trace-partial-na-parameter.svg

Lines changed: 130 additions & 0 deletions
Loading

tests/testthat/test-helpers-mcmc.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,12 @@ test_that("transformations recycled properly if not a named list", {
252252

253253

254254
# prepare_mcmc_array ------------------------------------------------------
255-
test_that("prepare_mcmc_array errors if NAs", {
256-
arr[1,1,1] <- NA
257-
expect_error(prepare_mcmc_array(arr), "NAs not allowed")
255+
test_that("prepare_mcmc_array warns but does not error if NAs", {
256+
arr_na <- arr
257+
arr_na[1, 1, 1] <- NA
258+
expect_warning(out <- prepare_mcmc_array(arr_na), "NAs found in 'x'")
259+
expect_s3_class(out, "mcmc_array")
260+
expect_true(anyNA(out))
258261
})
259262
test_that("prepare_mcmc_array processes non-array input types correctly", {
260263
# errors are mostly covered by tests of the many internal functions above

tests/testthat/test-mcmc-traces.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,33 @@ test_that("mcmc_trace renders correctly", {
165165
vdiffr::expect_doppelganger("mcmc_trace (iter1 offset)", p_iter1)
166166
})
167167

168+
# https://github.com/stan-dev/bayesplot/issues/250
169+
test_that("mcmc_trace renders correctly with NAs in draws", {
170+
testthat::skip_on_cran()
171+
testthat::skip_if_not_installed("vdiffr")
172+
skip_on_r_oldrel()
173+
174+
set.seed(250)
175+
draws_full_na <- array(
176+
rnorm(500 * 4 * 2),
177+
dim = c(500, 4, 2),
178+
dimnames = list(NULL, NULL, c("theta[1,3]", "theta[2,3]"))
179+
)
180+
draws_full_na[, , "theta[2,3]"] <- NA
181+
182+
draws_partial_na <- draws_full_na
183+
draws_partial_na[, , "theta[2,3]"] <- rnorm(500 * 4)
184+
draws_partial_na[10:100, , "theta[2,3]"] <- NA
185+
186+
suppressWarnings({
187+
p_full_na <- mcmc_trace(draws_full_na)
188+
p_partial_na <- mcmc_trace(draws_partial_na)
189+
})
190+
191+
vdiffr::expect_doppelganger("mcmc_trace (NA parameter)", p_full_na)
192+
vdiffr::expect_doppelganger("mcmc_trace (partial NA parameter)", p_partial_na)
193+
})
194+
168195
test_that("mcmc_rank_overlay renders correctly", {
169196
testthat::skip_on_cran()
170197
testthat::skip_if_not_installed("vdiffr")

0 commit comments

Comments
 (0)