Skip to content

Conversation

@VisruthSK
Copy link
Member

Uses more posterior functions in monitor.R for various functions. Exported functions are replaced with minimal wrappers, others are removed.

There are no tests for monitor.R, but running some examples seems to be ok. I can add tests if required (probably just snapshots using those examples).

Fixes #1169.

@jgabry @avehtari

Rewrote some functions to act as wrappers around posterior functions, and swap in posterior functions in check_hmc_diagnostics.R

out <- as.data.frame(do.call(rbind, out))
probs_str <- names(quantile(sims_i, probs = probs, na.rm = TRUE))
probs_str <- names(posterior::quantile2(sims_i, probs = probs))
Copy link
Member

Choose a reason for hiding this comment

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

Are the names that posterior::quantile2 uses the same as the names used by quantile? That is, will these names be any different than in the current version?

Copy link
Member Author

Choose a reason for hiding this comment

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

The names are different; it should be easy to get the names from quantile() and rename the posterior::quantile2() results, would that be preferred?

Copy link
Member

@jgabry jgabry Oct 23, 2025

Choose a reason for hiding this comment

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

I think we'd want to stick to the names that are currently used, so yeah I guess we should rename the posterior::quantile2 results. I just made another comment about some quantiles not being printed. I think this could be related.

@jgabry jgabry requested a review from bgoodri October 22, 2025 15:59
Copy link
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.

This looks good, thanks! I put one question in a review comment. As the maintainer, @bgoodri should probably also approve of making this change, but it seems like a good idea to me.

@jgabry
Copy link
Member

jgabry commented Oct 22, 2025

Oh and posterior also needs to be added to the DESCRIPTION file

Comment on lines 290 to 291
quan <- unname(posterior::quantile2(sims_i, probs = probs))
quan2 <- posterior::quantile2(sims_i, probs = c(0.05, 0.5, 0.95))
Copy link
Member

Choose a reason for hiding this comment

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

When I run monitor() for some reason the quan2 are printed but not the quan. For example:

> monitor(fit1, probs = c(0.37, 0.99))
Inference for the input samples (4 chains: each with iter = 10; warmup = 0):

       Q5  Q50  Q95 Mean  SD  Rhat Bulk_ESS Tail_ESS
y[1] -1.2  0.0  1.3  0.0 0.9  1.19       20       20
y[2] -1.1 -0.1  1.8  0.1 1.0  1.08       20       20
lp__ -1.6 -0.6 -0.1 -0.7 0.4  1.14       20       20

Copy link
Member

@jgabry jgabry Oct 23, 2025

Choose a reason for hiding this comment

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

I think it could be because the print method print.simsummary is looking for quantile columns with a capital Q but in the monitor function there's this:

  probs_str <- names(posterior::quantile2(sims_i, probs = probs))
  str_quan <- paste0("Q", probs * 100)
  str_quan2 <- paste0("Q", c(0.05, 0.5, 0.95) * 100)
  str_mcse_quan <- paste0("MCSE_", str_quan)
  colnames(out) <- c("mean", "se_mean", "sd", probs_str, "n_eff", "Rhat",
                     "valid", str_quan2, str_mcse_quan, "MCSE_SD", "Bulk_ESS", "Tail_ESS")

Here probs_str will have a lowercase q but the others will have capital Q.

@VisruthSK
Copy link
Member Author

I added a snapshot based on an example model Jonah wrote for monitor(), but I still haven't figured out how to load rstan locally so the true value needs to set once.

@jgabry
Copy link
Member

jgabry commented Oct 24, 2025

I think for testing monitor we should pass it a fixed set of posterior draws instead of creating a stanfit object (it can handle both). That's because with different hardware and different compilers results can be slightly different. So we'd have to set a tolerance but that could be fragile. I just pushed a commit that updates the test to do this.


out <- as.data.frame(do.call(rbind, out))
probs_str <- names(posterior::quantile2(sims_i, probs = probs))
probs_str <- names(quantile(sims_i, probs = probs, na.rm = TRUE))
Copy link
Member

Choose a reason for hiding this comment

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

This isn't working correctly either, still not being printed. I will look into this.

@jgabry
Copy link
Member

jgabry commented Oct 24, 2025

@bgoodri When reviewing this PR I noticed that the monitor function is a bit of a mess (not @VisruthSK's fault, this was the case before this PR). I don't understand why there's a probs argument but then 0.05, 0.5, and 0.95, are also hardcoded internally to always be used. This duplicates 0.5, which is already included in the default probs, and would duplicate the users input if they put 0.05 or 0.95 in probs. Also the printing is messed up so not all the quantiles are even printed, which is perhaps why this wasn't noticed. I think a better way is to just have a probs argument (keeping current defaults) and use whatever is in probs without forcing anything internally. I'm going to push a commit to this branch that does this, but we can revert if you know of a reason for all this that I'm overlooking or if you have a better idea.

@VisruthSK
Copy link
Member Author

Thanks for generating the snapshot.

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.

Use posterior package for convergence diagnostics

3 participants