Skip to content

Commit

Permalink
Merge pull request #1033 from stan-dev/error-message-fixed-param-diag…
Browse files Browse the repository at this point in the history
…nostics

Informative error message for sampler_diagnostics() with fixed_param
  • Loading branch information
jgabry authored Nov 14, 2024
2 parents a9e2536 + 38a33ef commit b762c54
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,9 @@ CmdStanMCMC$set("public", name = "loo", value = loo)
#' }
#'
sampler_diagnostics <- function(inc_warmup = FALSE, format = getOption("cmdstanr_draws_format", "draws_array")) {
if (isTRUE(private$metadata_$algorithm == "fixed_param")) {
stop("There are no sampler diagnostics when fixed_param = TRUE.", call. = FALSE)
}
if (is.null(private$sampler_diagnostics_) &&
!length(self$output_files(include_failed = FALSE))) {
stop("No chains finished successfully. Unable to retrieve the sampler diagnostics.", call. = FALSE)
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-fit-mcmc.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ fit_mcmc_3 <- testing_fit("logistic", method = "sample",
iter_sampling = 0,
save_warmup = 1,
refresh = 0, metric = "dense_e")
fit_mcmc_fixed_param <- testing_fit("logistic", method = "sample",
seed = 1234, chains = 1,
iter_warmup = 100,
iter_sampling = 0,
save_warmup = 1,
refresh = 0, fixed_param = TRUE)
PARAM_NAMES <- c("alpha", "beta[1]", "beta[2]", "beta[3]")

test_that("draws() stops for unkown variables", {
Expand Down Expand Up @@ -399,3 +405,10 @@ test_that("metadata()$time has chains rowss", {
expect_equal(nrow(fit_mcmc_2$metadata()$time), fit_mcmc_2$num_chains())
expect_equal(nrow(fit_mcmc_3$metadata()$time), fit_mcmc_3$num_chains())
})

test_that("sampler_diagnostics() throws informative error when fixed_param=TRUE", {
expect_error(
fit_mcmc_fixed_param$sampler_diagnostics(),
"There are no sampler diagnostics when fixed_param = TRUE"
)
})

0 comments on commit b762c54

Please sign in to comment.