
Posterior summaries and sampler diagnostics of mvgam models
Source:R/diagnostics.mvgam.R
mvgam_diagnostics.RdThe brms and bayesplot accessors for a fitted model.
fixef() and vcov() summarise the population-level coefficients
of the observation formula, or of the trend formula with
trend_effects = TRUE. coef() gives each level of each grouping
factor its coefficients: the population effect plus that level's
deviation, as brms::coef.brmsfit() does. posterior_summary()
summarises any parameters. rhat() and neff_ratio() report convergence.
nuts_params() and log_posterior() return the sampler's own
quantities.
bayes_R2() is the Bayesian R^2 of Gelman et al. (2019).
ndraws(), nchains(), niterations() and nvariables() count
the posterior.
Usage
# S3 method for class 'mvgam'
coef(object, summary = TRUE, robust = FALSE, probs = c(0.025, 0.975), ...)
# S3 method for class 'mvgam'
nuts_params(object, pars = NULL, ...)
# S3 method for class 'mvgam'
log_posterior(object, ...)
# S3 method for class 'mvgam'
rhat(x, pars = NULL, ...)
# S3 method for class 'mvgam'
neff_ratio(object, pars = NULL, ...)
# S3 method for class 'mvgam'
fixef(
object,
summary = TRUE,
robust = FALSE,
probs = c(0.025, 0.975),
pars = NULL,
trend_effects = FALSE,
...
)
# S3 method for class 'mvgam'
bayes_R2(
object,
resp = NULL,
summary = TRUE,
robust = FALSE,
probs = c(0.025, 0.975),
by_series = FALSE,
...
)
# S3 method for class 'mvgam'
prior_summary(object, ...)
# S3 method for class 'mvgam'
ndraws(x, ...)
# S3 method for class 'mvgam'
nchains(x, ...)
# S3 method for class 'mvgam'
niterations(x, ...)
# S3 method for class 'mvgam'
nvariables(x, ...)
# S3 method for class 'mvgam'
posterior_summary(
x,
pars = NULL,
variable = NULL,
probs = c(0.025, 0.975),
robust = FALSE,
...
)
# S3 method for class 'mvgam'
getCall(x, ...)
# S3 method for class 'mvgam'
vcov(object, correlation = FALSE, pars = NULL, trend_effects = FALSE, ...)Arguments
- object, x
A fitted
mvgamobject.- summary
Logical.
TRUE(the default) returns a summary.FALSEreturns the draws.- robust
Logical.
TRUEsummarises by the median and the median absolute deviation. Defaults toFALSE, the mean and the standard deviation.- probs
Numeric vector of the two interval probabilities,
c(0.025, 0.975)by default.- ...
Further arguments.
nuts_params()andlog_posterior()pass them to bayesplot.bayes_R2()passes them toposterior_epred.mvgam(). The other methods refuse them.- pars
Optional character vector of parameter names, or of sampler parameters such as
"divergent__"fornuts_params().NULLtakes all of them.- trend_effects
Logical.
fixef()andvcov()report the coefficients of the observation formula by default.TRUEreports those of thetrend_formulainstead. Their names end in_trend.- resp
Optional name of one response of a multivariate model.
- by_series
Logical, for
bayes_R2().TRUEcomputes the fit of each series from its own rows. A binary response, such asbernoulli()or the detections ofocc(), adds Tjur's R^2 and the AUC. Tjur's R^2 is the mean predicted probability of the rows observed as 1 less that of the rows observed as 0. The AUC is the probability that a row observed as 1 is predicted above a row observed as 0. A series observed at one value alone takesNAfor both. Defaults toFALSE.- variable
Parameter names for
posterior_summary(), the brms name of the argument. Thereparsis its deprecated alias.- correlation
Logical.
TRUEreturns the correlation matrix of the coefficients. Defaults toFALSE, the covariance matrix.
Value
fixef(), posterior_summary() and bayes_R2() return a
matrix with columns Estimate, Est.Error and one per quantile,
or the draws with summary = FALSE. Under by_series = TRUE
bayes_R2() returns a data frame with one row per series and
measure, or draws in columns named <measure>[<series>], such as
AUC[sp1]. coef() returns a named list with one array per
grouping factor: levels, statistics and coefficients, or draws,
levels and coefficients with summary = FALSE. A fit without
group-level effects in its observation formula is refused. A
level's coefficient omits the terms of other covariates that
change it, such as env:size under jsdgam()'s trait_slopes.
species_effects() gives each species' full response.
vcov() returns the covariance matrix of the coefficients.
rhat() and neff_ratio() return a named vector with one value
per parameter. nuts_params() and log_posterior() return the
long data frames of bayesplot. The counting methods return
an integer.
Checking a fit
Three checks come before any interpretation of a fit. Vehtari et al. (2021) give the thresholds for the first two.
Every Rhat should be below 1.01.
max(rhat(mod))gives the largest. A higher value means the chains disagree about the posterior. Longer chains, such asupdate(mod, iter = 4000), often resolve it.The bulk and tail effective sample sizes should exceed 400.
summary.mvgam()prints them asBulk_ESSandTail_ESS. Longer chains raise them.There should be no divergent transitions. The fit warns when there are any.
sum(nuts_params(mod, pars = "divergent__")$Value)counts them. A divergence marks a region of the posterior the sampler could not explore. Its draws may then be biased. Refit with smaller sampler steps,update(mod, control = list(adapt_delta = 0.95)).
Divergences that persist at a higher adapt_delta usually mean the
data cannot separate some of the parameters. A common cause is a
smooth with more flexibility than the series supports, competing
with the trend for the same variation. A smaller basis, such as
s(x, k = 5), or a more informative prior then helps.
References
Vehtari, A., Gelman, A., Simpson, D., Carpenter, B. and Burkner, P.-C. (2021). Rank-normalization, folding, and localization: An improved Rhat for assessing convergence of MCMC. Bayesian Analysis, 16(2):667-718. doi:10.1214/20-BA1221
Gelman, A., Goodrich, B., Gabry, J. and Vehtari, A. (2019). R-squared for Bayesian regression models. The American Statistician, 73(3):307-309. doi:10.1080/00031305.2018.1549100
See also
summary.mvgam() prints these summaries for every
parameter. mcmc_plot.mvgam() plots the draws. mvgam_draws
extracts them. loo_R2.mvgam() is the leave-one-out R^2.
Other model checking:
plot.mvgam(),
pp_check.mvgam(),
residuals.mvgam()
Examples
# \dontrun{
set.seed(13)
simdat <- sim_mvgam(family = poisson(), n_series = 1L,
n_timepoints = 120L, trend_model = AR())
mod <- mvgam(y ~ s(x, k = 5), trend_formula = ~ AR(p = 1),
data = simdat$data_train,
family = poisson(),
chains = 2, silent = 2)
#> Warning: 8 of 2000 (0.0%) transitions ended with a divergence.
#> See https://mc-stan.org/misc/warnings for details.
# The population coefficients with 95% intervals
fixef(mod)
#> Estimate Est.Error Q2.5 Q97.5
#> Intercept 1.639007 0.1423052 1.367617 1.929177
#> sx_1 8.505474 2.1566000 4.264522 12.855963
# Rhat below 1.01 and effective sample ratios above 0.1 suggest the
# chains mixed
rhat(mod, pars = c("ar1_trend", "sigma_trend"))
#> ar1_trend[series_1] sigma_trend[series_1]
#> 1.003372 1.001022
neff_ratio(mod, pars = c("ar1_trend", "sigma_trend"))
#> ar1_trend[series_1] sigma_trend[series_1]
#> 0.3106775 0.3693468
# The share of variance in the counts the model explains
bayes_R2(mod)
#> Estimate Est.Error Q2.5 Q97.5
#> R2 0.8296004 0.03977316 0.7407305 0.8927362
# Next, mcmc_plot() draws the posterior of these parameters
# }