
Model-averaged posterior draws and predictions across mvgam fits
Source:R/pp_average.mvgam.R
posterior_average.mvgam.RdBayesian model averaging of two or more mvgam fits, as in
brms::posterior_average.brmsfit() and brms::pp_average.brmsfit().
Each fit contributes draws in proportion to its model weight.
posterior_average() averages parameter draws. pp_average()
averages predictions.
Usage
# S3 method for class 'mvgam'
posterior_average(
x,
...,
variable = NULL,
pars = NULL,
weights = "stacking",
ndraws = NULL,
nsamples = NULL,
missing = NULL,
model_names = NULL,
control = list(),
seed = NULL
)
# S3 method for class 'mvgam'
pp_average(
x,
...,
weights = "stacking",
method = "posterior_predict",
ndraws = NULL,
nsamples = NULL,
summary = TRUE,
probs = c(0.025, 0.975),
robust = FALSE,
model_names = NULL,
control = list(),
seed = NULL
)Arguments
- x
A fitted
mvgamobject. Further fits are passed via....- ...
Further fitted
mvgamobjects to average over. Forpp_average(), named arguments are passed to the prediction functionmethodnames.- variable
Optional character vector of parameter names to extract, as
variables.mvgam()lists them. A bare vector name selects every element.NULL(the default) selects all parameters shared across fits (or all parameters across the union whenmissingis supplied), excluding"lp__".- pars
Deprecated brms alias for
variable.- weights
A numeric vector with one weight per fit, scaled to sum to one, or one of
"stacking","pseudobma","pseudobma+","loo"and"waic". mvgam_loo_extras describes each.- ndraws
Total number of model-averaged draws to return. Defaults to the number of draws in the first fit.
- nsamples
Deprecated brms alias for
ndraws.- missing
Values for parameters some fits lack.
NULL(the default) restrictsvariableto the parameters every fit has and refuses a name outside them. A list keyed by parameter name gives each parameter its own value. A single number fills every parameter a fit lacks.- model_names
Optional character vector of model names, one per forecast. It replaces the names taken from the call.
- control
A list of extra arguments passed to
loo::loo_model_weights()when computing stacking / pseudo-BMA weights.- seed
Optional integer seed. The session's random state is restored when the call returns.
- method
Which prediction function
pp_average()calls on each fit before averaging:"posterior_predict"(the default),"posterior_epred"or"posterior_linpred".- summary
Logical. If
TRUE(the default),pp_average()summarises the averaged predictions withposterior_summary.mvgam()as a mean, an error and two quantiles.FALSEreturns the averaged draws.- probs
Numeric vector of probabilities for quantile computation. Default is
c(0.025, 0.975)for 95% credible intervals.- robust
Logical. If
FALSE(the default), uses mean and standard deviation for summaries. IfTRUE, uses median and median absolute deviation (MAD).
Value
posterior_average() returns a data.frame of
model-averaged posterior draws. pp_average() returns a numeric
matrix of averaged predictions. With summary = TRUE its rows are
observations and its columns summary statistics. Otherwise its
rows are draws and its columns observations. Attribute weights
holds the weight of each fit. Attribute ndraws holds the number
of draws taken from each fit.
See also
loo_model_weights.mvgam() computes the weights.
ensemble() averages forecasts. The
forecast evaluation article
weights and combines competing models.
Other model comparison:
bridge_sampler.mvgam(),
compare_elpds(),
kfold.mvgam(),
lfo_cv.mvgam(),
log_lik.mvgam(),
loo.mvgam(),
loo_compare.mvgam_lfo(),
mvgam_loo_extras,
waic.mvgam()
Examples
# \dontrun{
# A Poisson GAM without a latent trend. PSIS approximates each
# held-out count of such a model well (see Details of ?loo.mvgam)
set.seed(13)
simdat <- sim_mvgam(family = poisson(), n_series = 1L,
n_timepoints = 120L, trend_model = "None")
mod <- mvgam(y ~ s(x), data = simdat$data_train,
family = poisson(), chains = 2, silent = 2)
#> Warning: 11 of 2000 (1.0%) transitions ended with a divergence.
#> See https://mc-stan.org/misc/warnings for details.
# }
# \dontrun{
# The same smooth of x replaced by a linear effect
mod_lin <- update(mod, formula. = y ~ x)
# Stacking weights from approximate leave-one-out cross-validation
loo_model_weights(mod, mod_lin)
#> Method: stacking
#> ------
#> weight
#> mod 0.900
#> mod_lin 0.100
# The intercept, averaged over both fits under those weights
pa <- posterior_average(mod, mod_lin, variable = "b_Intercept")
attr(pa, "weights")
#> mod mod_lin
#> 0.90006149 0.09993851
summary(pa)
#> b_Intercept
#> Min. :1.452
#> 1st Qu.:1.603
#> Median :1.637
#> Mean :1.636
#> 3rd Qu.:1.669
#> Max. :1.790
# Averaged expected counts, one row per observation
head(pp_average(mod, mod_lin, method = "posterior_epred"))
#> Estimate Est.Error Q2.5 Q97.5
#> [1,] 2.917276 0.5032926 2.042109 3.992054
#> [2,] 13.036550 2.0955562 7.866624 16.432100
#> [3,] 7.120997 0.6529359 5.951876 8.424106
#> [4,] 8.683885 1.0082068 6.991904 11.017109
#> [5,] 2.781831 0.5425894 1.851517 3.979510
#> [6,] 8.372967 1.8643111 5.648742 12.807312
# }