Skip to content

describe_posterior() triggers model recompiling & sampling #997

@mattansb

Description

@mattansb

Discussed in easystats/bayestestR#693

Originally posted by JWiley January 27, 2025
I am experiencing some behaviour that I think is unexpected, but perhaps I am missing something.
I fit some models in brms and save them. Later I read them back in and call describe_posterior(), mostly because I want to report 99% CIs which is not the default calculated in brms.

This is the code:

out$Summary <- bayestestR::describe_posterior(
  out$Results,
  centrality = c("mean", "median"), dispersion = TRUE,
  ci = CI, ci_method = CIType,
  test = c("p_direction"),
  diagnostic = c("Rhat", "ESS"),
  effects = "all", component = "all",
  parameters = "(^b_)|(^sd_)|(^cor_)|(^sds_)|(^bs_)")

The part that is unexpected to me is that this seems to trigger recompiling and resampling the model.
Which really slows it all down.

The desired updates require recompiling the model
Compiling Stan program...
Start sampling
Running MCMC with 4 parallel chains...

Chain 1 Iteration:    1 / 3000 [  0%]  (Warmup) 
Chain 2 Iteration:    1 / 3000 [  0%]  (Warmup) 
Chain 3 Iteration:    1 / 3000 [  0%]  (Warmup) 
Chain 4 Iteration:    1 / 3000 [  0%]  (Warmup) 
[snip]
Chain 1 Iteration: 2800 / 3000 [ 93%]  (Sampling) 
Chain 4 finished in 686.1 seconds.
Chain 3 Iteration: 3000 / 3000 [100%]  (Sampling) 
Chain 3 finished in 704.9 seconds.
Chain 2 Iteration: 3000 / 3000 [100%]  (Sampling) 
Chain 2 finished in 706.4 seconds.
Chain 1 Iteration: 2900 / 3000 [ 96%]  (Sampling) 
Chain 1 Iteration: 3000 / 3000 [100%]  (Sampling) 
Chain 1 finished in 727.6 seconds.

All 4 chains finished successfully.
Mean chain execution time: 706.3 seconds.
Total execution time: 727.8 seconds.

It's fairly easy to side step this, given all I want that's not already in the model results I saved is the 99% CIs and maybe some of the probability of direction, but I just expected this call to describe_posterior() to be virtually "free" computationally.
As far as I know, I'm not trying to calculate bayes factors or anything where you'd need prior draws, which is the only thing I can think of that's not already drawn and saved in the model.

Any thoughts or insight would be welcome.


I'm not entirely sure what all is needed in the model to trigger this

  • if the outcome distribution is gaussian, the recompile and resampling is not needed
  • if there is no random slope even with negative binomial distribution the recompile and resampling is not needed

so my best guess is that it is something to do with a random slope + the negative binomial distribution --- it may also be to do with correlations amongst random effects

with most models, this behaviour is not exhibited, which I think is why I thought it may be unexpected, but it also may be expected and I don't understand what is happening or needs to happen for the posterior summaries that are calculated


Reproducible example:

## analysis related
library(brms)
library(cmdstanr)
library(bayestestR)

CIType <- "HDI"
CI <- 0.99

m <- brm(
  formula = bf(
    hp ~ mpg + (1 | am / cyl) + (1 + mpg | cyl),
    shape ~ 1 + (1 | cyl)
  ), data = mtcars,
  family = negbinomial(),
  save_pars = save_pars(group = TRUE, latent = FALSE, all = TRUE),
  init = "random",
  chains = 4, iter = 3000, warmup = 1000, thin = 1,
  cores = 4,
  normalize = TRUE, algorithm = "sampling",
  seed = 1234, backend = "cmdstanr"
)

bayestestR::describe_posterior(
  m,
  centrality = c("mean", "median"), dispersion = TRUE,
  ci = 0.99, ci_method = "HDI",
  test = c("p_direction"),
  diagnostic = c("Rhat", "ESS"),
  effects = "all", component = "all",
  parameters = "(^b_)|(^sd_)|(^cor_)|(^sds_)|(^bs_)"
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions