| tulpa | R Documentation |
Single entry point for fitting a Bayesian hierarchical model. tulpa()
parses the formula, builds the model matrices, selects an inference backend
through the tier/mode system (see inference_mode_info()), assembles the
arguments that backend needs, and dispatches.
The fit conditions on the random-effect standard deviations sigma_re (and,
for non-Gaussian dispersion, phi): both the Laplace (Tier 2) and the
sampler (Tier 1) paths target the posterior given these. Integrating over the
hyperparameters is the role of the nested-Laplace / EM layer.
tulpa(
formula,
data,
family = "gaussian",
mode = "auto",
sigma_re = NULL,
n_trials = NULL,
weights = NULL,
phi = 1,
estimate_phi = FALSE,
phi2 = NULL,
beta_prior = NULL,
re_prior = NULL,
hyperprior = c("proper", "flat"),
ziformula = NULL,
zi_prior = NULL,
warm_start = NULL,
spatial = NULL,
temporal = NULL,
control = list(),
...
)
formula |
A model formula. Fixed effects, |
data |
A data frame. Incomplete rows (an |
family |
Character family name: one of |
mode |
Inference mode or backend. |
sigma_re |
Random-effect SDs to condition on: length 1 (recycled) or one
per RE term. Defaults to 1 per term with a warning. Ignored by every
backend that DETERMINES the RE scale itself – by integrating it
( |
n_trials |
Binomial denominators (length |
weights |
Optional observation weights (non-negative numeric vector,
length |
phi |
Dispersion passed to the family, held fixed. One convention at
every door: for Defaulted, it conditions at 1 and says so with a warning, since for a
family that reads a dispersion that is a modelling choice rather than a
neutral value. |
estimate_phi |
Estimate the dispersion from the data instead of
conditioning on Available under |
phi2 |
Optional second dispersion: the Student-t degrees of freedom
( |
beta_prior |
Optional |
re_prior |
Optional |
hyperprior |
The outer hyperparameter prior, |
ziformula |
Optional one-sided formula for the zero-inflation
probability, e.g. |
zi_prior |
Optional |
warm_start |
Optional starting point for the NUTS sampler, from a
cheaper fit of the same model: The variance-component slots take an adapting mass by default, because a
plug-in fit estimates no curvature for them. Passing a fit from
|
spatial |
Optional spatial-field spec. How it is addressed depends on the field family:
The mode selects how the spatial hyperparameter is handled:
|
temporal |
Optional temporal field spec, integrated by nested Laplace
for the discrete walks and sampled for the continuous ones:
|
control |
Optional list of backend tuning arguments. Each backend
accepts its own set, checked at the door:
The default One statistical knob lives here rather than in the signature: |
... |
Reserved for future statistical arguments. Nothing is read from
it today, so any entry errors: a stray name here is a misspelled argument
or a tuning knob that belongs in |
A tulpa_fit object carrying the backend's output plus
inference_mode, inference_tier, backend, selection_reason,
formula, and family. Two field-name conventions to know when
reaching into the object directly (the generic accessors handle both):
on nested-Laplace fits $weights is the hyperparameter GRID weights;
user observation weights are stored as $obs_weights. $draws is a
draws matrix on engine fits, while model-package fits may carry a list
($y_rep, $log_lik) under the same name.
No random effects and random intercepts ((1 | g)) are supported on
the design path (mode = "laplace") and the sampler path (mode = "mala",
"pathfinder", "imh_laplace", and the ModelData kernels "hmc" / "sghmc"
/ "sgld" / "mclmc" / "smc" / "vi" / "ess").
Random slopes are supported on the Laplace (Tier 2) path: there is no
scalar sigma_re to condition on, so the RE covariance Sigma is integrated
rather than fixed. This covers correlated terms ((1 + x | g), a full
Sigma), uncorrelated terms ((1 + x || g), a diagonal Sigma), and
several terms together ((1 + x | g) + (1 | h)) – each term becomes a
covariance block, and any accompanying (1 | g) term is integrated as a 1x1
block (nothing is silently conditioned at sigma_re = 1). mode = "laplace"
routes to the nested-Laplace Sigma integrator (tulpa_re_cov_nested(),
CCD design, PC + LKJ hyperprior by default – see hyperprior);
control$re_cov = "gibbs" switches to the exact
Metropolis-within-Gibbs debias (tulpa_re_cov_gibbs()), and
control$re_cov = "aghq" keeps the nested integrator but replaces the
inner joint-Laplace marginal with adaptive Gauss-Hermite quadrature
(control$n_quad, default 9 there; see n_quad in
tulpa_re_cov_nested()). Both also run on the
sampler path (mode = "mala" / "pathfinder").
mode = "gibbs" (Polya-Gamma) fits a single random-intercept model for
family = "binomial" or "neg_binomial_2", and samples the RE sd
rather than conditioning on sigma_re; tune it via
re_prior$prior_sigma_scale and a mean-zero beta_prior.
Latent prior blocks (latent(tgmrf(...))) route to the nested-Laplace
path (Tier 2), which integrates over the block hyperparameters. mode = "auto" and "structured" select it automatically when latent blocks are
present; mode = "nested_laplace" forces it. Several random-intercept
(1 | g) terms may accompany the blocks; the one-term restriction belongs
to the Polya-Gamma spatial Gibbs sampler (mode = "gibbs"), which updates
one RE block alongside the field. Joint multi-arm nested models cannot be
expressed by a single-response formula – call
tulpa_nested_laplace_joint() directly.
The ModelData sampler kernels ("hmc", "ess", "sghmc", "sgld",
"mclmc", "smc", "vi") thread the full latent vector – fixed effects,
random effects (all forms), areal spatial (icar / bym2), and temporal
(rw1 / rw2 / ar1) – through one ModelData builder and sample the
variance components jointly with the field. ess carries random effects but
declines a structured spatial / temporal block (its isotropic Gaussian-prior
block cannot represent the graph precision); continuous-coordinate fields
(gp / nngp / hsgp / spde), car_proper, and exotic latent blocks stay
on the dedicated nested-Laplace / SPDE / Polya-Gamma paths.
Rue, Martino & Chopin (2009). Approximate Bayesian inference for latent Gaussian models by using integrated nested Laplace approximations. JRSS-B 71(2):319-392. Hoffman & Gelman (2014). The No-U-Turn Sampler: adaptively setting path lengths in Hamiltonian Monte Carlo. JMLR 15(47):1593-1623.
inference_mode_info(), tulpa_laplace(), mala(), pathfinder()
set.seed(1)
n <- 200L
g <- sample(letters[1:12], n, replace = TRUE)
d <- data.frame(
y = rbinom(n, 1, plogis(-0.3 + 0.6 * rnorm(n))),
x = rnorm(n),
g = g
)
# Random-intercept logistic GLMM, Laplace tier.
fit <- tulpa(y ~ x + (1 | g), data = d, family = "binomial", mode = "laplace")
coef(fit)
summary(fit)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.