
Bridge sampling and Bayes factors for mvgam fits
Source: R/brms_wrappers.mvgam.R
bridge_sampler.mvgam.RdEstimates the log marginal likelihood of a fit by bridge sampling
(Gronau, Singmann and Wagenmakers 2020) and compares two fits by
their Bayes factor. The marginal likelihood weighs how well a model
explains the data under its prior. loo.mvgam() and
lfo_cv.mvgam() score predictions of held-out data instead. A
Bayes factor sets no forecast horizon, which makes it a complement
to leave-future-out scores for state-space models. It also
depends on the priors, which predictive scores barely do.
Usage
# S3 method for class 'mvgam'
bridge_sampler(samples, recompile = FALSE, ...)
# S3 method for class 'mvgam'
bayes_factor(x1, x2, log = FALSE, ...)Arguments
- samples
An
mvgamfit. The name follows the bridgesampling generic.- recompile
Logical. Bridge sampling evaluates the density through a compiled rstan model. A fit without one, sampled by cmdstanr or loaded with
readRDS()in a new session, has one compiled, with its draws kept.TRUEcompiles one for any fit. Defaults toFALSE.- ...
Arguments of
bridgesampling::bridge_sampler(), such asmethod,repetitionsorcores.- x1, x2
mvgamfits, orbridgeobjects returned bybridge_sampler(), to compare.- log
Logical.
TRUEreturns the log Bayes factor.
Value
bridge_sampler() returns a bridge object holding the
log marginal likelihood in logml. bayes_factor() returns a
bayes_factor object.
Details
The Stan program must keep the normalising constants of its
densities. mvgam builds it that way by default. A fit built with
normalize = FALSE is refused. Bridge sampling also needs the draws
of every parameter, which a fit keeps under
save_pars = save_pars(all = TRUE).
Multivariate fits
The Stan program of an mvbf() fit has
one joint log posterior over every response. bridge_sampler()
then estimates the joint log marginal likelihood
\(\log p(y_{1}, \ldots, y_{K} \mid \mathrm{model})\). The
variance of the estimate grows with the number of parameters. For
multi-response, factor or VAR fits, pass repetitions above 1 or
method = "warp3" when the default method reports a high re2
(relative squared error).
References
Gronau QF, Singmann H and Wagenmakers E-J (2020). bridgesampling: An R package for estimating normalizing constants. Journal of Statistical Software. 92(10).
See also
loo.mvgam(), lfo_cv.mvgam(). The
forecast evaluation article
compares models by their forecasts.
Other model comparison:
compare_elpds(),
kfold.mvgam(),
lfo_cv.mvgam(),
log_lik.mvgam(),
loo.mvgam(),
loo_compare.mvgam_lfo(),
mvgam_loo_extras,
posterior_average.mvgam(),
waic.mvgam()
Examples
# \dontrun{
set.seed(1)
simdat <- sim_mvgam(family = poisson(), n_series = 1L,
n_timepoints = 80L, trend_model = AR())
# Bridge sampling needs many draws and every parameter's draws. It
# evaluates the density through rstan. A fit sampled by rstan needs
# no second compile.
mod_ar <- mvgam(y ~ s(x, k = 5), trend_formula = ~ AR(p = 1),
data = simdat$data_train, family = poisson(),
save_pars = save_pars(all = TRUE), backend = "rstan",
chains = 2, iter = 3000, silent = 2)
#> Warning: There were 6 divergent transitions after warmup. See
#> https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
#> to find out why this is a problem and how to eliminate them.
#> Warning: Examine the pairs() plot to diagnose sampling problems
mod_rw <- update(mod_ar, trend_formula = ~ RW())
#> Warning: There were 2 divergent transitions after warmup. See
#> https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
#> to find out why this is a problem and how to eliminate them.
#> Warning: Examine the pairs() plot to diagnose sampling problems
# The log marginal likelihood of each model
bs_ar <- bridge_sampler(mod_ar, silent = TRUE)
bs_rw <- bridge_sampler(mod_rw, silent = TRUE)
bs_ar
#> Bridge sampling estimate of the log marginal likelihood: -165.47
#> Estimate obtained in 8 iteration(s) via method "normal".
# A Bayes factor above 1 favours the AR(1) trend. Passing the
# bridge objects reuses their estimates.
bayes_factor(bs_ar, bs_rw)
#> Estimated Bayes factor in favor of x1 over x2: 7.30112
# Next, compare their forecasts with loo() or lfo_cv()
# }