A Bayesian fit gives posterior draws of the coefficients, β_1, …,
β_S, in place of an estimate with a covariance and df. predictmeans
keeps its own grid, weights, empty-cell rules and contrasts, the K
matrix of every mean, and works through the draws: each draw gives K
β_s, a draw of every mean, and so of every difference, contrast and
back-transformed mean. This is the same idea as a permlist,
which gives many copies of a test statistic. But permutations are copies
under the null hypothesis, used only for p-values; posterior draws are
copies of the quantity itself, so they give its estimate and interval
directly:
| frequentist fit | Bayesian fit | |
|---|---|---|
| estimate | K b | posterior median of K β_s |
| spread | SE, sqrt(diag(K V K’)) | posterior SD (posterior.sd, never called a standard
error) |
| interval | t limits with df | equal-tailed credible interval (quantiles of the draws); no df |
| test | p-value, adj
|
probability of direction pd; with adj, a
simultaneous band |
equivalence |
two one-sided tests |
p.rope, the posterior probability within the
margins |
| back-transformation | inverse link of the mean, delta-method SE | inverse link of each draw (exact) |
bias_adjust |
the variance estimate | the integral over the random effects, for each draw |
brmsfit (brms) and stanreg (rstanarm) fits
work this way in predictmeans(),
contrastmeans(), varcomp(),
R2_model(), pm_check(),
residplot() and (brms survival families)
survmeans(); the other functions stop and say so. Other
Bayesian classes need a pm_draws() method
(?pm_draws).
Means and pairs
Contagious bovine pleuropneumonia in 15 herds over four periods
(lme4::cbpp), a binomial GLMM with a random herd effect.
rstanarm’s models are compiled already, so the fit takes seconds (brms
compiles each model first, then the calls are the same):
fit <- stan_glmer(cbind(incidence, size - incidence) ~ period + (1 | herd), data = lme4::cbpp,
family = binomial, chains = 4, iter = 2000, refresh = 0, seed = 1)
pm <- predictmeans(fit, "period", pairwise = TRUE)
pm## Posterior summaries of the means of 'period' (stanreg model, 4000 draws)
## Estimate: posterior median; limits: 95% equal-tailed credible interval; no df
## Response scale: posterior median of the inverse link of each draw (random effects at zero: a typical cluster); bias_adjust = TRUE gives the mean
##
## period estimate posterior.sd conf.low conf.high response response.posterior.sd
## 1 -1.391 0.2518 -1.909 -0.9162 0.19920 0.03998
## 2 -2.392 0.3340 -3.062 -1.7650 0.08378 0.02633
## 3 -2.534 0.3634 -3.285 -1.8650 0.07352 0.02561
## 4 -2.991 0.4533 -3.960 -2.1950 0.04782 0.02162
## response.conf.low response.conf.high n
## 0.12910 0.2857 15
## 0.04469 0.1462 14
## 0.03610 0.1342 14
## 0.01870 0.1002 13
##
## Pairwise differences (pd: probability of direction):
## level1 level2 estimate posterior.sd conf.low conf.high pd odds.ratio
## 1 2 0.9959 0.3053 0.4235 1.6210 0.9995 2.707
## 1 3 1.1360 0.3384 0.4935 1.8370 1.0000 3.113
## 2 3 0.1394 0.3921 -0.6094 0.9248 0.6405 1.150
## 1 4 1.6030 0.4332 0.8458 2.5130 0.9998 4.966
## 2 4 0.5994 0.4719 -0.2640 1.5510 0.9070 1.821
## 3 4 0.4598 0.5055 -0.4803 1.4840 0.8225 1.584
## odds.ratio.conf.low odds.ratio.conf.high
## 1.5270 5.057
## 1.6380 6.280
## 0.5437 2.521
## 2.3300 12.340
## 0.7680 4.718
## 0.6186 4.409
The means are on the logit scale; response is the
inverse link of each draw, the incidence of a typical herd (random
effect zero). The pairs are differences of log odds, with their odds
ratios. pd is the posterior probability that a difference
has the sign of its median: 0.5 means no evidence of direction, 1
certainty.
The frequentist fit of the same model, for comparison:
fq <- lme4::glmer(cbind(incidence, size - incidence) ~ period + (1 | herd), data = lme4::cbpp, family = binomial)
cbind(bayes = pm$means$estimate, glmer = predictmeans(fq, "period")$means$estimate)## bayes glmer
## [1,] -1.391012 -1.398532
## [2,] -2.392106 -2.390848
## [3,] -2.533859 -2.527196
## [4,] -2.991212 -2.978848
The mean over the herds
bias_adjust = TRUE gives the mean incidence over the
herds: for each draw, the inverse link averaged over the herd effect
with that draw’s variance (an exact integral), then summarised. It is
higher than that of a typical herd, as the logistic curve is skewed at
these low incidences:
pa <- predictmeans(fit, "period", bias_adjust = TRUE)
cbind(typical_herd = pm$means$response, all_herds = pa$means$response)## typical_herd all_herds
## [1,] 0.19924632 0.22113075
## [2,] 0.08377663 0.10065663
## [3,] 0.07351838 0.08917737
## [4,] 0.04782449 0.05968517
pa$settings$back_transform$estimate## [1] "posterior median of the population mean: the inverse link averaged over the random effects of each draw"
Simultaneous bands, letters and equivalence
With adj (any value), the pairs get a simultaneous band
(Besag et al. 1995): the smallest set of equal-tailed intervals that
hold for all pairs together in 95% of the draws. Letters come from these
bands only (pointwise intervals would not make letters that hold
jointly). equivalence = 0.5 adds the posterior probability
that a difference lies within ±0.5 on the logit scale:
pb <- predictmeans(fit, "period", pairwise = TRUE, adj = "tukey", equivalence = 0.5)
pb$pairs[, c("level1", "level2", "estimate", "conf.low", "conf.high", "adj.conf.low", "adj.conf.high", "pd", "p.rope")]## level1 level2 estimate conf.low conf.high adj.conf.low adj.conf.high
## 1 1 2 0.9958817 0.4235329 1.6207465 0.2551180 1.796199
## 2 1 3 1.1355802 0.4935350 1.8373525 0.3129090 2.064069
## 3 2 3 0.1394463 -0.6094236 0.9248171 -0.8850296 1.212313
## 4 1 4 1.6026693 0.8457915 2.5126890 0.6647121 2.822351
## 5 2 4 0.5993916 -0.2640175 1.5513401 -0.5877152 1.818329
## 6 3 4 0.4598268 -0.4802876 1.4837096 -0.7810553 1.835815
## pd p.rope
## 1 0.99950 0.04650
## 2 1.00000 0.02550
## 3 0.64050 0.77800
## 4 0.99975 0.00225
## 5 0.90700 0.40850
## 6 0.82250 0.50750
pb$means[, c("period", "response", "group")]## period response group
## 1 1 0.19924632 A
## 2 2 0.08377663 B
## 3 3 0.07351838 B
## 4 4 0.04782449 B
Plots
The means with their credible intervals, the pairs (odds ratios on a
log axis, with the simultaneous bands), and the posterior distribution
of each mean or difference (type = "draws"):
autoplot(pb)
plot of chunk unnamed-chunk-6
autoplot(pb, type = "pairs")
plot of chunk unnamed-chunk-6
autoplot(pb, type = "draws")
plot of chunk unnamed-chunk-7
autoplot(pb, type = "draws", what = "pairs")
plot of chunk unnamed-chunk-7
Contrasts
Period 1 against the mean of the later periods, from the same draws:
contrastmeans(fit, "period", ctrmatrix = rbind(`1 vs later` = c(1, -1/3, -1/3, -1/3)))## Posterior summaries of contrasts of 'period' (stanreg model, 4000 draws)
## Estimate: posterior median; limits: 95% equal-tailed credible interval; pd: probability of direction
##
## contrast estimate posterior.sd conf.low conf.high pd
## 1 vs later 1.25 0.2482 0.7874 1.751 1
Curves and slopes
covariatemeans() and pm_slopes() work the
same way: each point of a curve, or each slope, is a row of the K
matrix, so the draws give it. Incidence against herd size, by period,
from a model with that covariate:
fit_s <- stan_glmer(cbind(incidence, size - incidence) ~ period + size + (1 | herd), data = lme4::cbpp,
family = binomial, chains = 4, iter = 2000, refresh = 0, seed = 1)
autoplot(covariatemeans(fit_s, "period", covariate = "size"))
plot of chunk unnamed-chunk-9
pm_slopes(fit_s, "size", "period")## Posterior summaries of the slopes of 'size' by 'period' (stanreg model, 4000 draws; at size = 15.04)
## Estimate: posterior median; limits: 95% equal-tailed credible interval
##
## period estimate posterior.sd conf.low conf.high pd
## 1 0.005877 0.02824 -0.04681 0.06304 0.5825
## 2 0.005877 0.02824 -0.04681 0.06304 0.5825
## 3 0.005877 0.02824 -0.04681 0.06304 0.5825
## 4 0.005877 0.02824 -0.04681 0.06304 0.5825
Variance components and R²
The herd variance (posterior median and interval), and Nakagawa’s marginal and conditional R² of each draw:
varcomp(fit)## vcov posterior.sd 2.5 % 97.5 %
## herd.(Intercept) 0.4917104 0.344718 0.1432311 1.488398
R2_model(fit)## Nakagawa R2 of each posterior draw (4000 draws): median, posterior SD and 95% equal-tailed interval
## R2 estimate posterior.sd conf.low conf.high
## Marginal 0.0933 0.0376 0.0384 0.182
## Conditional 0.2200 0.0630 0.1250 0.370
Are the draws good enough?
The summaries are only as good as the draws. pm_check()
reports the largest Rhat, the smallest bulk and tail effective sample
sizes and divergent transitions; residplot() draws the
trace and the density of each coefficient by chain. Checks of the model
itself (does it reproduce the data?) are posterior predictive checks,
pp_check() of brms and rstanarm.
pm_check(fit)## Checks of a stanreg model (56 observations)
##
## Fit
## OK Convergence (Rhat) largest 1.001 (period3), 4 chains
## OK Effective sample size smallest bulk 1781, tail 2350 of 4000 draws
## OK Divergent transitions 0 of 4000
## i Model checks posterior predictive checks: pp_check() ...
##
## Notes
## i Model checks: posterior predictive checks: pp_check() of brms or
## rstanarm.
##
## Nothing flagged.
## (! worth a look, i information, OK no concern.) Checks inform, they do not decide; see residplot().
residplot(fit)
plot of chunk unnamed-chunk-11
An advice line says when the draws have not converged (Rhat above 1.01, effective sample size below 400) or are too few for the tail quantiles of the limits (fewer than 1,000).
Survival models (brms)
brms survival families (weibull(),
lognormal(), exponential() with
cens(), and cox()) give means on the log-time
scale, time ratios and, for Weibull and exponential models, hazard
ratios exp(-k d) computed per draw with that draw’s shape k.
survmeans() gives survival curves of a typical cluster or,
with marginal = TRUE, averaged over the random effects. The
worked example, a Weibull model of the lung cancer data with a random
institution effect beside its frequentist versions, is section 6 of the
survival
article.
What is not supported
The help page ?pm_draws lists what is and is not
available for Bayesian fits. In short:
- The permutation and bootstrap functions,
semipred(),CookD(),se_ranef(),heritability()andresidvariogram()stop for Bayesian fits.pm_joint()gives the posterior region through 0, not an F test. - brms: the families whose mean parameter is the mean of the response,
the survival families and
cox()(with survival curves from its spline baseline), the ordinalcumulative(),sratio(),cratio()andacat()(class probabilities withmean_type = "prob"), and the zero-inflated and hurdle families (overall means withmean_type = "overall", also when zi or hu has predictors); no categorical or mixture models, no multivariate or non-linear formulas, no smooths or special terms. rstanarm:stan_glm(),stan_glmer(),stan_lmer(),stan_lm(), the.nbversions andstan_aov(). MCMCglmm: gaussian, poisson and binary models with random intercepts (give the data:fit$data <- d). - The summaries are exact for the draws, with the Monte Carlo error of
the draws; the advice says when there are too few or they have not
converged. The brms survival quantities agree with SAS’s own Bayesian
fits of the same data (LIFEREG and PHREG with a
BAYESstatement, PROC MCMC;dev/sas/survival-bayes.sas) within the Monte Carlo error and the effect of the different priors.
How the numbers were checked
tests/testthat/test-bayes.R checks that the medians, SDs
and quantiles of the means and pairs equal those of emmeans’ draws of
the same fits; that pd, the intervals and
p.rope equal those of bayestestR; that the population means
and the marginal survival curves equal integrate() for
every draw; that the variance components equal brms’
VarCorr(); that the overall means of zero-inflated and
hurdle models and the class probabilities of cumulative ones equal brms’
posterior_epred(); that each draw’s R² equals
performance::r2_nakagawa() of a glmmTMB fit held at that
draw’s parameters; and that flat-prior fits agree with
lm(), coxph() and survreg().
