diff --git a/DESCRIPTION b/DESCRIPTION index dbb0dfd4..c389666b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -41,7 +41,7 @@ Imports: ggridges (>= 0.5.5), glue, lifecycle, - posterior (>= 1.7.0), + posterior (>= 1.8.0), reshape2, rlang (>= 1.0.0), stats, diff --git a/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-correlated-prit-2.svg b/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-correlated-prit-2.svg index ec3adb47..30d075b6 100644 --- a/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-correlated-prit-2.svg +++ b/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-correlated-prit-2.svg @@ -27,7 +27,7 @@ - + diff --git a/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-correlated-prit.svg b/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-correlated-prit.svg index 151a21ae..1fb3abd5 100644 --- a/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-correlated-prit.svg +++ b/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-correlated-prit.svg @@ -27,7 +27,7 @@ - + p u n diff --git a/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-diff-correlated-prit.svg b/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-diff-correlated-prit.svg index c4501aa2..d7ee0a6f 100644 --- a/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-diff-correlated-prit.svg +++ b/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-diff-correlated-prit.svg @@ -27,7 +27,7 @@ - + diff --git a/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-grouped-correlated-diff.svg b/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-grouped-correlated-diff.svg index ccdcfdf6..d0f955f0 100644 --- a/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-grouped-correlated-diff.svg +++ b/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-grouped-correlated-diff.svg @@ -37,7 +37,7 @@ O T = -0.004 +0.005 ( α @@ -97,7 +97,7 @@ O T = -0.34 +0.47 ( α @@ -126,7 +126,7 @@ O T = -0.34 +0.47 ( α diff --git a/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-grouped-correlated.svg b/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-grouped-correlated.svg index 6f225944..9e5cb135 100644 --- a/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-grouped-correlated.svg +++ b/tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-grouped-correlated.svg @@ -37,7 +37,7 @@ O T = -0.004 +0.005 ( α @@ -97,7 +97,7 @@ O T = -0.34 +0.47 ( α @@ -126,7 +126,7 @@ O T = -0.34 +0.47 ( α diff --git a/tests/testthat/_snaps/ppc-loo/ppc-loo-pit-ecdf-alpha-0-05.svg b/tests/testthat/_snaps/ppc-loo/ppc-loo-pit-ecdf-alpha-0-05.svg index 3cc6c94f..099c2d85 100644 --- a/tests/testthat/_snaps/ppc-loo/ppc-loo-pit-ecdf-alpha-0-05.svg +++ b/tests/testthat/_snaps/ppc-loo/ppc-loo-pit-ecdf-alpha-0-05.svg @@ -36,7 +36,7 @@ O T = -0.37 +0.48 ( α diff --git a/tests/testthat/_snaps/ppc-loo/ppc-loo-pit-ecdf-changed-theme.svg b/tests/testthat/_snaps/ppc-loo/ppc-loo-pit-ecdf-changed-theme.svg index f33e821a..4b3f5cfd 100644 --- a/tests/testthat/_snaps/ppc-loo/ppc-loo-pit-ecdf-changed-theme.svg +++ b/tests/testthat/_snaps/ppc-loo/ppc-loo-pit-ecdf-changed-theme.svg @@ -36,7 +36,7 @@ O T = -0.37 +0.48 ( α diff --git a/tests/testthat/_snaps/ppc-loo/ppc-loo-pit-ecdf-correlated-prit.svg b/tests/testthat/_snaps/ppc-loo/ppc-loo-pit-ecdf-correlated-prit.svg index f83ff30d..f77673ad 100644 --- a/tests/testthat/_snaps/ppc-loo/ppc-loo-pit-ecdf-correlated-prit.svg +++ b/tests/testthat/_snaps/ppc-loo/ppc-loo-pit-ecdf-correlated-prit.svg @@ -27,7 +27,7 @@ - + diff --git a/tests/testthat/_snaps/ppc-loo/ppc-loo-pit-ecdf-diff-correlated-prit.svg b/tests/testthat/_snaps/ppc-loo/ppc-loo-pit-ecdf-diff-correlated-prit.svg index 350a356e..94508de6 100644 --- a/tests/testthat/_snaps/ppc-loo/ppc-loo-pit-ecdf-diff-correlated-prit.svg +++ b/tests/testthat/_snaps/ppc-loo/ppc-loo-pit-ecdf-diff-correlated-prit.svg @@ -27,7 +27,7 @@ - + diff --git a/tests/testthat/test-extractors.R b/tests/testthat/test-extractors.R index 1116be67..f61c688b 100644 --- a/tests/testthat/test-extractors.R +++ b/tests/testthat/test-extractors.R @@ -135,7 +135,7 @@ test_that("cmdstanr methods work", { fit <- cmdstanr::cmdstanr_example("logistic", iter_sampling = 500, chains = 2) np <- nuts_params(fit) np_names <- paste0(c("treedepth", "divergent", "energy", "accept_stat", "stepsize", "n_leapfrog"), "__") - expect_identical(levels(np$Parameter), np_names) + expect_setequal(levels(np$Parameter), np_names) expect_equal(range(np$Iteration), c(1, 500)) expect_equal(range(np$Chain), c(1, 2)) expect_true(all(np$Value[np$Parameter == "divergent__"] == 0)) diff --git a/tests/testthat/test-ppc-loo.R b/tests/testthat/test-ppc-loo.R index 965e2914..6e899519 100644 --- a/tests/testthat/test-ppc-loo.R +++ b/tests/testthat/test-ppc-loo.R @@ -256,9 +256,7 @@ test_that("ppc_loo_pit_ecdf correlated method handles edge cases", { # Test with single value (edge case) single_pit <- 0.5 - expect_error(ppc_loo_pit_ecdf(pit = single_pit, method = "correlated")) - expect_gg(p5 <- ppc_loo_pit_ecdf(pit = single_pit, method = "correlated", - test = "PIET")) + expect_gg(p5 <- ppc_loo_pit_ecdf(pit = single_pit, method = "correlated", test = "PIET")) }) test_that("ppc_loo_pit functions work when pit specified instead of y, yrep, and lw", { @@ -910,4 +908,4 @@ test_that("check pareto_pit argument is chosen as expected", { ), regexp = "`pareto_pit = TRUE` cannot be used together with a non-`NULL`" ) -}) \ No newline at end of file +}) diff --git a/vignettes/visual-mcmc-diagnostics.Rmd b/vignettes/visual-mcmc-diagnostics.Rmd index 1b6ee6a6..a98a4305 100644 --- a/vignettes/visual-mcmc-diagnostics.Rmd +++ b/vignettes/visual-mcmc-diagnostics.Rmd @@ -445,20 +445,25 @@ mcmc_trace(posterior_cp, pars = "tau", np = np_cp) + The first thing to note is that all chains seem to be exploring the same region of parameter values, which is a good sign. But the plot is too crowded to help us diagnose divergences. We may however zoom in to investigate, using the -`window` argument: +`window` argument, which takes the first and last iteration to show. Here +`zoom_window` is the stretch of 200 iterations with the most divergences: ```{r echo=FALSE} -#A check that the chosen window still relevant -n_divergent_in_window <- np_cp %>% filter(Parameter == "divergent__" & Value == 1 & Iteration >= 400 & Iteration <= 600) %>% nrow() - -if(n_divergent_in_window < 6) { - divergences <- np_cp %>% filter(Parameter == "divergent__" & Value == 1) %>% select(Iteration) %>% get("Iteration", .) %>% sort() %>% paste(collapse = ",") - stop(paste("Too few divergences in the selected window for traceplot zoom. Change the window or the random seed.\nDivergences happened at: ", divergences)) +# Zoom in on the 200-iteration window with the most divergences. Where the +# divergences fall depends on the platform and compiler, so a hard-coded window +# can stop being illustrative even with a fixed seed. +div_iters <- np_cp %>% filter(Parameter == "divergent__" & Value == 1) %>% pull(Iteration) +window_starts <- seq_len(max(np_cp$Iteration) - 200) +n_div_in_window <- sapply(window_starts, function(s) sum(div_iters >= s & div_iters <= s + 200)) +zoom_window <- window_starts[which.max(n_div_in_window)] + c(0, 200) + +if (max(n_div_in_window) < 6) { + stop(paste("Too few divergences in any window for traceplot zoom. Change the random seed.\nDivergences happened at: ", paste(sort(div_iters), collapse = ","))) } ``` ```{r mcmc_trace_zoom} -mcmc_trace(posterior_cp, pars = "tau", np = np_cp, window = c(200,400)) + +mcmc_trace(posterior_cp, pars = "tau", np = np_cp, window = zoom_window) + xlab("Post-warmup iteration") ```