Skip to content

Allow NAs in prepare_mcmc_array#539

Open
utkarshpawade wants to merge 1 commit intostan-dev:masterfrom
utkarshpawade:fix/allow-nas-prepare-mcmc-array-250
Open

Allow NAs in prepare_mcmc_array#539
utkarshpawade wants to merge 1 commit intostan-dev:masterfrom
utkarshpawade:fix/allow-nas-prepare-mcmc-array-250

Conversation

@utkarshpawade
Copy link
Copy Markdown
Contributor

Fixes #250
prepare_mcmc_array() previously aborted on any NA in the input, blocking plotting workflows where parameters are stored as ragged arrays padded with NA (common with cmdstanr draws). The hard error is demoted to a warning so plots like mcmc_trace, mcmc_dens, etc. work directly on such inputs. Stricter NA checks elsewhere (helpers-ppc.R validators, drop_NAs_and_warn in diagnostics) are left intact, those guard summary computations where silently dropping NAs would mislead.

Changes

  • R/helpers-mcmc.R: abort()warn() on anyNA(x).
  • tests/testthat/test-helpers-mcmc.R: updated test to assert the warning and successful return.

Copilot AI review requested due to automatic review settings April 13, 2026 10:26
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in prepare_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.

Comment thread R/helpers-mcmc.R Outdated
Comment thread tests/testthat/test-helpers-mcmc.R
Comment thread NEWS.md
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.15%. Comparing base (44c3e5c) to head (1f3d4ce).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@utkarshpawade utkarshpawade force-pushed the fix/allow-nas-prepare-mcmc-array-250 branch from 6323413 to 1f3d4ce Compare April 13, 2026 10:44
@jgabry
Copy link
Copy Markdown
Member

jgabry commented Apr 14, 2026

Thanks, I'm not necessarily opposed to this change, but I'd be curious to see an actual example where this is useful.

@utkarshpawade
Copy link
Copy Markdown
Contributor Author

set.seed(1)
iter <- 500; chains <- 4
draws <- array(
  rnorm(iter * chains * 6),
  dim = c(iter, chains, 6),
  dimnames = list(NULL, NULL, c("theta[1,1]","theta[1,2]","theta[1,3]",
                                "theta[2,1]","theta[2,2]","theta[2,3]"))
)
# group 2 has fewer observations, its third column is NA-padded
draws[, , "theta[2,3]"] <- NA

mcmc_trace(draws, pars = c("theta[1,3]", "theta[2,3]"))

On master this errors with NAs not allowed in 'x'.. With the changes it renders, valid parameter traces normally, NA-only parameter is an empty panel:
image

Copy link
Copy Markdown
Member

@jgabry jgabry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread R/helpers-mcmc.R
Comment on lines +32 to +34
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."
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Requiring no NAs in prepare_mcmc_array

4 participants