Bayesian GLMMs by NUTS
bayes_glmm_fit.Rdbayes_glmm_fit() samples the posterior of the model of glmm_fit()
(random terms written as in lme4) with nuts-rs, jointly with the random
effects, which are sampled non-centred (b = Lambda v with
v ~ N(0, I), so the chains stay out of the funnel between a standard
deviation and its effects). The priors: N(0, prior_sd^2) on each
coefficient (intercept_sd for the intercept), half-normal with scale
sd_scale on each random term's standard deviation, LKJ(lkj) on each
factor's correlations (uniform at 1), and half-normal with scale
dispersion_scale on the gamma's or Tweedie's dispersion. The chains
start at the Laplace fit, which is kept as laplace. On a single
factor the posterior matches exact integration, and on 800 rows with
random slopes it sits on the Laplace fit (docs/design/models.md,
"Mixed models").
Usage
bayes_glmm_fit(
formula,
data,
family = "poisson",
power = NULL,
offset = NULL,
weights = NULL,
prior_sd = 2.5,
intercept_sd = 10,
sd_scale = 1,
lkj = 1,
dispersion_scale = 10,
chains = 4,
tune = 1000,
draws = 1000,
seed = 0,
target_accept = 0.8,
max_depth = 10
)
bayes_glmm_model(ptr, terms, xlevels, groups)Arguments
- formula
A formula with the fixed effects and one or more random terms,
(1 | g)or(1 + x | g).- data
A data frame.
- family
"poisson","gamma"or"tweedie".- power
The Tweedie power, in (1, 2).
- offset, weights
Optional offset (added to any
offset()term) and prior weights.- prior_sd, intercept_sd
Prior standard deviations of the coefficients and the intercept, on the log scale.
- sd_scale
Scale of the half-normal prior on each random term's standard deviation.
- lkj
The LKJ prior's shape on each factor's correlations.
- dispersion_scale
Scale of the half-normal prior on the dispersion.
- chains, tune, draws
Chains, warm-up draws and kept draws per chain.
- seed
Seed, a whole number.
- target_accept
Target acceptance rate for step-size adaptation.
- max_depth
Largest tree depth.
- ptr, terms, xlevels, groups
Internal.
Value
A bayes_glmm_model with coefficients (posterior means),
summary (a data frame of mean, sd, quantiles, R-hat and ESS for each
coefficient, sd[g|term], cor[g|a,b] and the dispersion when it
is free), draws (a matrix of those parameters, one row per draw),
effect_draws (a matrix of the random effects b[g|level|term]),
random_effects (a list by factor of data frames with each level
and term, its posterior mean and sd), divergences and
laplace (the glmm_model the chains started from). predict()
gives the posterior mean of each row's mean, its effects looked up as
in glmm_fit().
Examples
d <- data.frame(y = (0:39 * 7) %% 5 + 0:39 %% 4, site = paste0("s", 0:39 %% 4))
m <- bayes_glmm_fit(y ~ 1 + (1 | site), d, chains = 2, tune = 300, draws = 300)
m@summary
#> parameter mean sd q05 q50 q95
#> 1 (Intercept) 1.2215913 0.2808489 0.7993755 1.1956458 1.8026324
#> 2 sd[site|(Intercept)] 0.4855008 0.2465265 0.1748971 0.4396171 0.9749786
#> rhat ess_bulk ess_tail
#> 1 1.061356 92.31719 83.03684
#> 2 1.005837 107.24457 223.30412