The adapter generic for Bayesian model classes (see pm_coef): an S x p matrix of posterior draws of
the fixed-effect coefficients, columns named as pm_coef() names them, or NULL for a fit that has
none. A class whose pm_draws() gives draws is analysed by the draws in predictmeans and
contrastmeans: posterior medians, posterior SDs, equal-tailed credible intervals and the probability
of direction, with no df and no p-values.
brmsfit (brms), stanreg (rstanarm) and MCMCglmm fits have their own methods: the draws of the
coefficients of the mean, named as the columns of its design. An MCMCglmm fit keeps no data: give them
(predictmeans(fit, ..., data = d)) or store them once (fit$data <- d); families gaussian, poisson and
categorical (two classes) and ordinal (the units variance fixed in the prior; latent means on the standard probit scale; class probabilities of an observation, on the latent sd sqrt(1 + units), and with bias_adjust = TRUE also over the random effects), random intercepts and us() / idh() structures; for poisson and categorical the means are at units (the additive overdispersion) = 0, a typical observation, and bias_adjust = TRUE averages over units and the random effects (the mean). For another Bayesian class write a pm_draws() method, with
pm_coef() (the posterior means), pm_vcov() and pm_basis().
Usage
pm_draws(model, ...)
# Default S3 method
pm_draws(model, ...)
# S3 method for class 'pm_foreign'
pm_draws(model, ...)
# S3 method for class 'pm_bayesfit'
pm_draws(model, ...)
# S3 method for class 'brmsfit'
pm_draws(model, ...)
# S3 method for class 'stanreg'
pm_draws(model, ...)
# S3 method for class 'MCMCglmm'
pm_draws(model, ...)What is done for Bayesian fits
predictmeans(),contrastmeans(),covariatemeans()(curves, and withcontrthe difference of two curves, with a simultaneous band over the covariate forband = "simultaneous") andpm_slopes(): means, pairs, contrasts, curves and slopes from the draws; the inverse link (ortrans) of each draw;bias_adjust = TRUE: the mean over the random effects of each draw; withadj, simultaneous bands (Besag et al. 1995) and letters;equivalence: the posterior probability within the margins (p.rope).varcomp(): the variance components;R2_model(): Nakagawa's R2 of each draw;pm_check(): Rhat, effective sample sizes, divergent transitions;residplot(): trace and density of each coefficient by chain;autoplot(): means, pairs and the posterior of each (type = "draws").brms
weibull(),exponential()andlognormal()withcens(): time ratios, hazard ratios exp(-k d) per draw (Weibull, exponential), andsurvmeans()curves and medians, of a typical cluster or averaged over the random effects (marginal = TRUE).brms
cox(): hazard ratios per draw (means relative to the data's average linear predictor, as forcoxph()), and survival curves from its spline baseline hazard (survmeans(); NA beyond the spline's range). Zero-inflated and hurdle families: the mean of the count (or non-zero) part, and withmean_type = "overall"the overall mean per draw ((1 - zi) mu; (1 - hu) E(y | y > 0); per trial forzero_inflated_binomial) at each cell, averaged over the cells on the response scale, and the pairs as ratios of overall means (pd, bands and letters on their log); a zi or hu modelled by predictors enters each cell (its variables not in the mean: over their levels, equally), andbias_adjust = TRUEintegrates over the random effects of mu and of zi / hu (not when the two are correlated).cumulative(),sratio(),cratio()andacat()(logit): latent means x'b - mean(thresholds) (cumulative logit: odds ratios of a higher class), and withmean_type = "prob"or"mean_class"the class probabilities or the mean class per draw, each cell's probabilities averaged over the cells of the mean (asposterior_epred()averaged over the other factors, and as forclmfits).pm_joint()andcontrastmeans(joint = TRUE): the posterior region through 0 of several contrasts (d2, the Mahalanobis distance of 0 from the posterior, andp.contour= (b + 1) / (S + 1), b of the S draws at least as far from the posterior mean; Box and Tiao's contour probability), in place of the F test.Random intercepts and slopes in
bias_adjust,R2_model()andsurvmeans(marginal = TRUE): the variance at a row is z' Sigma z for each draw (z the row's values of the random-effect terms).
What is not done (it stops with a message)
df_term(),permmodels(),permlmer(),bootmodels(),CookD(),se_ranef(),heritability(),residvariogram(),semipred(), the test of equal curves ofcovariatemeans(contr = ),varcomp(test = TRUE),permlist.brms: families other than gaussian, student, skew_normal, binomial, bernoulli, beta_binomial, poisson, negbinomial, geometric, gamma, inverse.gaussian, beta, lognormal, weibull, exponential, cox, cumulative, the zero-inflated poisson, negbinomial, binomial and beta, the hurdle poisson, negbinomial, lognormal and gamma, and sratio, cratio, acat, categorical (class probabilities as for
nnet::multinomfits: the class a factor of the means,mean_type = "prob","prob_diff"or"link"; random effects at zero, or withbias_adjust = TRUEaveraged over them for up to 4 classes), andmixture()(the overall mean sum_k theta_k E_k by default; withmean_type = "conditional"the mean of each component, only for ordered components; fixed mixing proportions), so no zero-one-inflated models; ordinal models with a modelled disc or thresholds that are not flexible; multivariate and non-linear formulas; smooths, Gaussian processes,mo(),me(),mi(), category-specific and autocorrelation terms, offsets. A modelledsigmaorshapeleaves the means ofmuas they are, but survival curves and hazard ratios need one shape or sigma for all rows.rstanarm: models other than
stan_glm(),stan_glmer(),stan_lmer(),stan_lm(),stan_glm.nb(),stan_glmer.nb()andstan_aov(). MCMCglmm: other families, several responses, other random structures than intercepts, us() and idh();R2_model()of non-gaussian MCMCglmm fits.A random slope whose variable is not a column of the fixed-effect design (its value at the means is not known);
R2_model()for families other than gaussian and binomial.Model checks of the data (posterior predictive checks): use
pp_check()of brms or rstanarm.
Accuracy and checks
The summaries are exact for the draws: medians and quantiles of the draws of each quantity (type 7, as
bayestestR::eti()); with an even number of draws the median averages two draws, so the median of a
back-transformed quantity and the back-transformation of the median differ in the last digits. Their Monte Carlo
error is that of the draws: the advice says when the draws have not converged (Rhat above 1.01, bulk or tail
effective sample size below 400) or are too few for the limits (fewer than 1,000). Checked against emmeans'
draws, bayestestR, brms' VarCorr() and posterior_epred() (overall means of zero-inflated and hurdle
models and class probabilities of ordinal ones, averaged over the other factors), integrate()
(tests/testthat/test-bayes.R).
Nakagawa's R2 of each draw equals performance::r2_nakagawa() of a glmmTMB fit held at the draw's parameter
values (gaussian with random slopes, Bernoulli); for an aggregated binomial response (cbind()) performance
uses another observation-level variance, where predictmeans uses the latent pi^2/3. The survival quantities of brms
fits are checked against their closed forms per draw, integrate() for the marginal curves, and
survreg() within 5% with flat priors, and against SAS's own Bayesian fits of the same data (LIFEREG and
PHREG with a BAYES statement, PROC MCMC with a random institution effect; dev/sas/survival-bayes.sas): time
ratios within 0.5%, hazard ratios within 1.1%, survival probabilities and medians of the frailty model within
4.5% (different samplers and priors). On SAS's own draws (PROC MCMC, PHREG), predictmeans' survival formulas
reproduce SAS's time ratios, hazard ratios, S(t), marginal S(t) and medians to SAS's printed digits.
References
Besag, J., Green, P., Higdon, D. and Mengersen, K. (1995). Bayesian computation and stochastic systems. Statistical Science, 10, 3-41.
Examples
# \donttest{
if (requireNamespace("rstanarm", quietly = TRUE) && requireNamespace("emmeans", quietly = TRUE)) {
fit <- rstanarm::stan_glm(weight ~ group, data = PlantGrowth, chains = 1, iter = 1000,
refresh = 0, seed = 1)
dim(pm_draws(fit))
predictmeans(fit, "group", pairwise = TRUE)
}
# }
