diff --git a/NEWS.md b/NEWS.md index c8f7d131..7525658e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # bayesplot (development version) +* Fixed missing `drop = FALSE` in `nuts_params.CmdStanMCMC()`. * Replace `apply()` with `storage.mode()` for integer-to-numeric matrix conversion in `validate_predictions()`. * Fixed `is_chain_list()` to correctly reject empty lists instead of silently returning `TRUE`. * Added unit tests for `mcmc_areas_ridges_data()`, `mcmc_parcoord_data()`, and `mcmc_trace_data()`. diff --git a/R/bayesplot-extractors.R b/R/bayesplot-extractors.R index 19c6df88..79f7966e 100644 --- a/R/bayesplot-extractors.R +++ b/R/bayesplot-extractors.R @@ -175,7 +175,7 @@ nuts_params.list <- function(object, pars = NULL, ...) { nuts_params.CmdStanMCMC <- function(object, pars = NULL, ...) { arr <- object$sampler_diagnostics() if (!is.null(pars)) { - arr <- arr[,, pars] + arr <- arr[,, pars, drop = FALSE] } out <- reshape2::melt(arr) colnames(out)[colnames(out) == "variable"] <- "parameter" diff --git a/tests/testthat/test-extractors.R b/tests/testthat/test-extractors.R index 53e9862c..355e1418 100644 --- a/tests/testthat/test-extractors.R +++ b/tests/testthat/test-extractors.R @@ -136,4 +136,12 @@ test_that("cmdstanr methods work", { ratio <- neff_ratio(fit) expect_named(head(ratio, 4), c("alpha", "beta[1]", "beta[2]", "beta[3]")) expect_true(all(ratio > 0)) + + # https://github.com/stan-dev/bayesplot/pull/535 + np_one <- nuts_params(fit, pars = "divergent__") + expect_identical(levels(np_one$Parameter), "divergent__") + expect_true(all(np_one$Parameter == "divergent__")) + expect_equal(range(np_one$Iteration), c(1, 500)) + expect_equal(range(np_one$Chain), c(1, 2)) + expect_true(all(np_one$Value == 0)) })