Skip to contents

brms's posterior_average() on draws from frm_sample(): a sample of posterior draws taken from several models in proportion to their weights. The number of draws taken from each model is the weight times ndraws, rounded by the largest remainder, and the draws of a model are taken without replacement. The result equals brms's on the same draws, the same weights and the same seed.

Usage

posterior_average(x, ...)

# S3 method for class 'frmtmb_draws'
posterior_average(
  x,
  ...,
  variable = NULL,
  pars = NULL,
  weights = "stacking",
  ndraws = NULL,
  nsamples = NULL,
  missing = NULL,
  model_names = NULL,
  control = list(),
  seed = NULL
)

Arguments

x, ...

Draws objects from frm_sample(), one per model. Every argument in ... must be a model; name one to label it.

variable

Names of the variables to average. NULL takes the variables that every model has.

pars

brms's deprecated spelling of variable.

weights

Numeric weights, one per model, or a method name. See the weights section.

ndraws

Total number of draws. NULL takes the number of draws of the first model.

nsamples

brms's deprecated spelling of ndraws.

missing

What a model that lacks a variable contributes for it: one number for every variable, or a named list with one number per missing variable. NULL (the default) refuses a variable that some model lacks.

model_names

Labels for the models. The default deparses the arguments, as brms does.

control

A list of arguments passed on to the weight method.

seed

Seed for the draw selection.

Value

A data frame with one row per draw and one column per variable, the draws of the first model first. The attribute weights holds the normalized weights and the attribute ndraws the number of draws taken from each model, both named by model.

Weights

weights is a numeric vector with one weight per model, or the name of a method that computes them:

  • "stacking" (the default) and "pseudobma": the weights of loo_model_weights().

  • "loo" and "waic": Akaike-type weights, exp(-delta / 2) normalized, over each model's LOOIC or WAIC.

  • "kfold" and "bma" are refused: frmtmb.sample has no kfold() and no marginal likelihood (see kfold() and post_prob()).

Examples

# \donttest{
if (requireNamespace("tmbstan", quietly = TRUE) &&
    requireNamespace("rstan", quietly = TRUE) &&
    !frmtmb.sample:::tmbstan_build_broken()) {
  set.seed(1)
  dd <- data.frame(x = rnorm(60))
  dd$y <- rnorm(60, 1 + 0.5 * dd$x)
  d1 <- frm_sample(bf(y ~ x), family = gaussian(), data = dd,
                   chains = 1, iter = 400, seed = 1, refresh = 0)
  d2 <- frm_sample(bf(y ~ 1), family = gaussian(), data = dd,
                   chains = 1, iter = 400, seed = 1, refresh = 0)
  pa <- posterior_average(d1, d2, variable = "sigma",
                          weights = c(0.7, 0.3), seed = 1)
  attr(pa, "ndraws")
}
#> frm_sample(): default priors (brms 2.23 defaults; prior = "flat" puts brms's (flat) in every slot)
#>   Intercept          student_t(3, 1.3, 2.5)
#>   sigma              student_t(3, 0, 2.5)  [natural scale]
#>   b                  (flat), as brms leaves slopes
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess
#> frm_sample(): default priors (brms 2.23 defaults; prior = "flat" puts brms's (flat) in every slot)
#>   Intercept          student_t(3, 1.3, 2.5)
#>   sigma              student_t(3, 0, 2.5)  [natural scale]
#>   b                  (flat), as brms leaves slopes
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#bulk-ess
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess
#>  d1  d2 
#> 140  60 
# }