Allow NAs in prepare_mcmc_array#539
Conversation
There was a problem hiding this comment.
Pull request overview
Updates prepare_mcmc_array() to tolerate NA values (common in ragged/padded draws) so MCMC plotting workflows can proceed, while still surfacing the presence of missing values to users.
Changes:
- Demote the
anyNA(x)hard error inprepare_mcmc_array()to a warning. - Update the corresponding unit test to expect a warning and successful return.
- Add a NEWS entry documenting the behavior change.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
R/helpers-mcmc.R |
Changes prepare_mcmc_array() NA handling from aborting to warning. |
tests/testthat/test-helpers-mcmc.R |
Adjusts tests to assert warning + returned mcmc_array containing NAs. |
NEWS.md |
Documents the new warning behavior for prepare_mcmc_array(). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #539 +/- ##
=======================================
Coverage 99.15% 99.15%
=======================================
Files 35 35
Lines 5920 5922 +2
=======================================
+ Hits 5870 5872 +2
Misses 50 50 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
6323413 to
1f3d4ce
Compare
|
Thanks, I'm not necessarily opposed to this change, but I'd be curious to see an actual example where this is useful. |
jgabry
left a comment
There was a problem hiding this comment.
Thanks, this is good. I made one suggestion about the warning message. Also, can you add a test for downstream plotting behavior? I think it would be helpful to add a vdiffr visual test for mcmc_trace using something similar to the example you gave. I would test two things:
- the version you had where one plot is empty (e.g.
draws[, , "theta[2,3]"] <- NA) - a version where only some rows are NA (e.g.
draws[10:100, , "theta[2,3]"] <- NA)
In the first case there should be a missing plot, in the second case there should be a missing chunk of the trace plot but the rest should be plotted.
| 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." | ||
| ) |
There was a problem hiding this comment.
I think we should shorten this wall of text to something like:
"NAs found in 'x'. These are passed through as-is and may affect the resulting plots."

Fixes #250
prepare_mcmc_array()previously aborted on anyNAin the input, blocking plotting workflows where parameters are stored as ragged arrays padded withNA(common with cmdstanr draws). The hard error is demoted to a warning so plots likemcmc_trace,mcmc_dens, etc. work directly on such inputs. StricterNAchecks elsewhere (helpers-ppc.Rvalidators,drop_NAs_and_warnin diagnostics) are left intact, those guard summary computations where silently droppingNAs would mislead.Changes
R/helpers-mcmc.R:abort()→warn()onanyNA(x).tests/testthat/test-helpers-mcmc.R: updated test to assert the warning and successful return.