Package 'lmebayesCore'

Title: Core C++ Sampling Engine for 'lmebayes'
Description: Core C++ engine for lmebayes: envelope-based iid samplers, two-block Gibbs mixed-model engines, and optional OpenCL acceleration. Full-featured developer backend for lmebayes and extensions. End users should use lmebayes for lmer/glmer-style mixed-model workflows.
Authors: Kjell Nygren [aut, cre], The R Core Team [ctb, cph] (R Mathlib sources, R stats modeling code, and derived/adapted routines), The R Foundation [cph] (Portions of R Mathlib and R source code), Ross Ihaka [ctb, cph] (R Mathlib and original R modeling infrastructure), Robert Gentleman [ctb, cph] (Portions of R Mathlib), Simon Davies [ctb] (Original R glm implementation), Morten Welinder [ctb, cph] (Portions of R Mathlib), Martin Maechler [ctb] (Portions of R Mathlib)
Maintainer: Kjell Nygren <[email protected]>
License: GPL-2
Version: 0.1.0
Built: 2026-10-03 13:43:11 UTC
Source: https://github.com/knygren/lmebayesCore

Help Index


lmebayesCore: Core C++ Sampling Engine for lmebayes

Description

Core C++ engine for envelope-based iid samplers, two-block Gibbs mixed-model engines, and optional OpenCL acceleration. Full-featured developer backend for lmebayes and related extensions. End users should install lmebayes for lmer/glmer-style mixed-model workflows.

Details

Low-level entry points include envelope construction (EnvelopeBuild, EnvelopeOrchestrator), registered simulation pipelines (rNormalGLM_std, rIndepNormalGammaReg_std), matrix-input samplers (rglmb, rlmb), the two-block Gibbs mixed-model engines (rlmerb, rglmerb), and OpenCL kernel loaders. Formula interfaces lmerb and glmerb live in lmebayes; lmebayesCore supplies the sampling engine.

Vignette index (the B series)

The Chapter-B* vignettes document the engine itself: the theory it implements, the routes it selects between, and the diagnostics it emits. They assume the reader has met the models already, for which see the Chapter-00 to Chapter-13 curriculum in lmebayes.

vignette("Chapter-B00")

Roadmap, notation, and the correspondence to lme4.

vignette("Chapter-B01")

The two-block architecture: replicate chains, the sweep loop, and the call stack.

vignette("Chapter-B02")

Ergodicity and total-variation convergence; the rate λ∗\lambda^* and the rank conditions.

vignette("Chapter-B03")

Calibrating m_convergence from tv_tol, and the pilot-versus-main cost trade-off.

vignette("Chapter-B04")

The known-vcov Gaussian route and the exact iid sampler.

vignette("Chapter-B05")

Estimated variance components (dIndependent_Normal_Gamma Block 2).

vignette("Chapter-B06")

The extended local rate diagnostic.

vignette("Chapter-B07")

Per-group measurement dispersion.

vignette("Chapter-B08")

Design, identifiability and rank conditions.

vignette("Chapter-B09")

Row-block engines.

vignette("Chapter-B10")

Sweep history and convergence plots.

Vignette index (the C series)

The Chapter-C* vignettes take one model class each and work through its posterior algebra, the eigenvalue calculation behind the convergence rate λ∗\lambda^*, and simulation results with convergence plots. Where the B series describes what the engine does, the C series derives why it is correct and then shows the numbers.

vignette("Chapter-C01")

The Gaussian running example: data, model, design, rank restriction and calibrated priors, shared by every Gaussian chapter in the series.

vignette("Chapter-C02")

The exact Gaussian iid sampler: known Ψ\Psi and σ2\sigma^2, closed-form posterior, no Markov chain.

vignette("Chapter-C03")

Total variation distances between multivariate normal densities: Nygren (2020) reproduced in full — the generalized error functions, Lemmas 1–2, Theorems 1–3, Claims 1–4, Corollary 1 and their proofs. Model-free.

vignette("Chapter-C04")

The same Gaussian model via the two-block Gibbs sampler: the Chapter-C03 rate and sweep budget instantiated on the running design, sweep history, and variance-convergence diagnostics.

The Chapter-A* vignettes shipped in the sources are inherited iid GLM reference material from glmbayes and are not built into the package tarball.

OpenCL startup checks

In interactive sessions, attaching the package with library(lmebayesCore) may emit a short packageStartupMessage when glmbayesCore_has_opencl() is FALSE but a GPU or OpenCL stack appears available on the host. Set options(glmbayes.quiet_opencl_startup = TRUE) to suppress attach notes (recommended for CI and R CMD check).

Author(s)

Maintainer: Kjell Nygren [email protected]

Other contributors:

  • The R Core Team (R Mathlib sources, R stats modeling code, and derived/adapted routines) [contributor, copyright holder]

  • The R Foundation (Portions of R Mathlib and R source code) [copyright holder]

  • Ross Ihaka (R Mathlib and original R modeling infrastructure) [contributor, copyright holder]

  • Robert Gentleman (Portions of R Mathlib) [contributor, copyright holder]

  • Simon Davies (Original R glm implementation) [contributor]

  • Morten Welinder (Portions of R Mathlib) [contributor, copyright holder]

  • Martin Maechler (Portions of R Mathlib) [contributor]

References

There are no references for Rd macro ⁠\insertAllCites⁠ on this help page.

See Also

lmebayes for the end-user modelling package.


Test whether group random effects lie in widetilde B(delta_2).

Description

Test whether group random effects lie in widetilde B(delta_2).

Usage

beta_in_marginal_safe_set(b, beta_set)

Arguments

b

Group effects as a J x p_re matrix (rows = groups) or a stacked numeric vector in group_precision_floor order.

beta_set

A beta_marginal_safe_set object.

Value

Logical scalar: TRUE if Ξ(β)≤rGauss\Xi(\beta) \le r_{\mathrm{Gauss}}.

See Also

rNormal_reg_group_safe, rNormalGLM_reg_group_safe


Marginal-mode beta posterior mode (gamma integrated out)

Description

Finds the Newton mode β†\beta^\dagger of the gamma-integrated marginal posterior on stacked group coefficients. Reusable input for beta_marginal_safe_set and group_precision_floor.

Usage

beta_marginal_mode(
  design,
  pfamily_list,
  family,
  dispprior_list = NULL,
  offset = NULL,
  weights = NULL,
  tol = 1e-10,
  maxit = 200L
)

Arguments

design

A model_setup list.

pfamily_list

Block~2 prior list from pfamily_list().

family

A family object.

dispprior_list

Optional Block~1 dispersion prior for gaussian().

offset, weights

Observation offset and weights; default from design.

tol, maxit

Newton controls (unused beyond initial solve).

Value

An object of class "beta_marginal_mode" with beta, hessian, f_mode, engine, prior_int, group_data, P_b, and family.

See Also

beta_marginal_safe_set, group_precision_floor


Marginal-mode beta safe set for Rosenthal / TV certificates

Description

Build the beta-safe set B~(δ2)={Ξ(β)≤rGauss(n,δ2)}\widetilde B(\delta_2) = \{\Xi(\beta) \le r_{\mathrm{Gauss}}(n,\delta_2)\} at fixed β†\beta^\dagger from beta_marginal_mode. Does not compute data-precision floors; use group_precision_floor for ΓjLB\Gamma_j^{\mathrm{LB}}.

Usage

beta_marginal_safe_set(
  design = NULL,
  pfamily_list = NULL,
  family = NULL,
  delta_2 = 0.01,
  beta_mode = NULL,
  dispprior_list = NULL,
  offset = NULL,
  weights = NULL,
  tol = 1e-10,
  maxit = 200L,
  verbose = FALSE
)

Arguments

design

A model_setup list (used if beta_mode is NULL).

pfamily_list

Block~2 prior list (used if beta_mode is NULL).

family

A family object (used if beta_mode is NULL).

delta_2

Tail mass budget in (0,1)(0,1) for the Laplace reference at rGaussr_{\mathrm{Gauss}}.

beta_mode

Optional result of beta_marginal_mode; if NULL, marginal mode is computed from design.

dispprior_list, offset, weights

Passed to beta_marginal_mode when beta_mode is NULL.

tol, maxit

Passed to beta_marginal_mode when beta_mode is NULL.

verbose

If TRUE, emit message notes about the calibrated level.

Value

An object of class "beta_marginal_safe_set" with level, beta_mode, delta_2, P_b, and certification metadata.

See Also

beta_marginal_mode, group_precision_floor, gamma_beta_tv_certificate


Restricted two-block Gibbs minorization certificate (Chapter C05).

Description

Computes the EM fixed point γ⋆\gamma^\star, the mode profile ε(γ⋆)\varepsilon(\gamma^\star), and the spectrum-calibrated restricted-chain multiplier ε=εdQ(C~d)\varepsilon = \varepsilon_d Q(\widetilde C_d).

Usage

certificate(
  design,
  pfamily_list,
  family = gaussian(),
  delta = 0.01,
  dispprior_list = NULL,
  estep = c("exact", "aghq", "mc"),
  acceleration = c("none", "squarem"),
  icm_init = TRUE,
  icm_tol = 1e-08,
  icm_maxit = 200L,
  n = 10000L,
  mc_seed = NULL,
  tol = NULL,
  em_tol = 1e-10,
  maxit = 200L
)

Arguments

design

A model_setup list.

pfamily_list

Block~2 prior list from pfamily_list().

family

A family object.

delta

Tail / escape probability budget in (0, 1).

dispprior_list

Optional Block~1 dispersion prior for gaussian().

estep

E-step tier: "exact" (default Gaussian closed form when unrestricted), "mc" (simulated conditional means; used automatically when beta_set is supplied), or "aghq" (not yet implemented).

acceleration

"none" or "squarem" (not yet implemented).

icm_init

If TRUE, run iterated conditional modes (Block~1 modes via rglmb, Block~2 via the C05 mean map) before EM. Skipped on the Gaussian exact closed-form path. Gaussian Block~1 uses exact conditional modes.

icm_tol

ICM convergence tolerance on ∥Δγ∥P11\|\Delta\gamma\|_{P_{11}}.

icm_maxit

Maximum ICM iterations.

n

Number of βj\beta_j draws per group when estep = "mc". The sample mean bˉj\bar b_j has elementwise MC standard error sd(βj∣γ,y)/n\mathrm{sd}(\beta_j \mid \gamma, y)/\sqrt{n}; relative error on means is therefore O(1/n)O(1/\sqrt{n}) (e.g. n = 10000 targets ≈1%\approx 1\% of the posterior scale).

mc_seed

Optional seed for the MC E-step (first iteration only).

tol

If supplied, compute a sweep count n such that (1 - eps)^n + delta <= tol (Theorem 2).

em_tol

EM convergence tolerance passed to population_mode as tol (distinct from sweep-count tol below).

maxit

Maximum EM iterations.

Details

The certificate applies to the restricted Gibbs chain on C~d\widetilde C_d, not necessarily the production sampler; see inst/CHAPTER_C05_IMPLEMENTATION.md section 8. Set sizing inverts the weighted-χ2\chi^2 escape tail on the coupling spectrum at γ⋆\gamma^\star (section 4A.3). Outside Gaussian closure the exact escape mass may differ from delta.

Value

An object of class "minorization_certificate".

See Also

population_mode, epsilon, deficiency_spectrum, gamma_beta_tv_certificate


Check identifiability and estimability of a single-factor mixed-model design

Description

Runs the "two-step identifiability assessment" used internally by model_setup: a Level~1 (within-group) rank/estimability check on the level-1 design D, followed by a Level~2 (across-group) rank check on the level-2 design matrices W, restricted to the Level-1-estimable groups. Takes the same D/group/W matrix inputs (and the same conventions – see Details) as rLMM_reg/rGLMM_reg, so it can be run directly on hand-built matrices from those matrix-level workflows, or on model_setup()'s design$D/design$group/ design$W directly, without requiring a full lmer/glmer reference fit.

Usage

check_identifiability(
  y,
  D,
  group,
  W,
  family = gaussian(),
  group_name = attr(group, "group_name"),
  verbose = FALSE
)

Arguments

y

Response vector of length nrow(D).

D

Level-1 design matrix (nrow(D) x p_re); must have unique, non-empty colnames(D) (random-effect coefficient names).

group

Grouping factor of length nrow(D) (must be a factor); levels(group) fixes the row order assumed by W and names groupef.rank/groupef.estimable in the return value – there is no separate group_levels argument. Unlike rLMM_reg, group_name is a separate argument here (see below) rather than derived from group via substitute().

W

Named list of group-level design matrices (J x q_k), one per column of D; names must match colnames(D) exactly.

family

A family object (or a string/function accepted by family); normalized internally via the same logic model_setup uses. Defaults to gaussian().

group_name

Display name of the grouping factor (e.g. "school_id"), used only in error and verbose messages (not in any computation). Defaults to attr(group, "group_name") (the convention already used by rLMM_reg); must be resolvable to a non-empty string, either this way or by passing it explicitly. Unlike rLMM_reg, there is no fallback to substitute(group) – this function may be called on matrices that are not bare variables.

verbose

If TRUE, emit message()s summarizing the Level 1 and Level 2 outcomes. Default FALSE (silent), so calling this from model_setup introduces no new console output.

Details

Level 1 (within-group). For each group jj, the within-group submatrix DjD_j (D[group == jj, ]) is checked for full column rank (groupef.rank). A rank-deficient group has too few distinct observations to estimate all random slopes independently. Algebraically full-rank groups are then additionally checked for estimability (groupef.estimable, with per-group detail in groupef.glm_check), with the specific check depending on family:

  • binomial(), poisson(), Gamma(): a classical glm(y ~ D_j - 1, family) fit is attempted; a group can be full column rank yet have no finite MLE under complete or quasi-complete separation (binomial), an all-zero count response (poisson), or a non-positive response (Gamma) – Gamma groups are additionally required to have n_j > p_j so the dispersion/shape parameter is estimable.

  • gaussian(): no fit is attempted (the OLS coefficient MLE always exists given full column rank), but the group must still have n_j > p_j so the residual variance/dispersion is estimable (otherwise the group achieves a perfect/saturated fit with zero residual degrees of freedom).

  • other families: groupef.estimable is set equal to groupef.rank (no additional check yet).

Level 2 (across-group). Restricting to the estimable groups from Level 1 (groupef.estimable), each level-2 design matrix W[[k]] is checked for full column rank (popef.rank), using only the rows at the estimable positions (row jj of W[[k]] corresponds to levels(group)[j], exactly as in rLMM_reg). Rank deficiency at this level means the level-2 hyperparameters are not identified by the data, even as the number of estimable groups grows. The scalar popef.rank_ok is TRUE only when every popef.rank entry is TRUE.

Inputs must already be consistently keyed. This function does not infer or coerce missing names – it validates them up front and errors immediately on a mismatch, rather than risking a silently wrong match deeper in the rank/estimability computation:

  • D must have unique, non-empty colnames(D): these are the random-effect coefficient names used to key W (there is no separate groupef.names argument to override them, matching rLMM_reg).

  • group must be a factor (not coerced from a character vector); levels(group) fixes the row order assumed by W (there is no separate group_levels argument).

  • W must be a named list, with names matching colnames(D) exactly (as sets; no missing, empty, or duplicate names), and each W[[k]] must have exactly length(levels(group)) rows.

  • group_name, used only for error/verbose messages, must be resolvable from either the group_name argument or attr(group, "group_name").

Value

A list with:

groupef.rank

Named logical, one entry per group (names levels(group)): TRUE if DjD_j is full column rank.

groupef.estimable

Named logical, same names as groupef.rank: TRUE if the group is additionally estimable (see Details).

groupef.glm_check

Per-group diagnostic data frame for binomial()/poisson()/Gamma()/gaussian(), or NULL for other families.

popef.rank

Named logical, one entry per RE coefficient (names colnames(D)): TRUE if W[[k]] is full column rank when restricted to groupef.estimable groups.

popef.deficient

Negation of popef.rank.

popef.rank_ok

Scalar TRUE only when every popef.rank entry is TRUE.

group_levels

levels(group), i.e. the exact names used on groupef.rank/groupef.estimable.

group_name

The resolved grouping-factor display name.

See Also

model_setup, rLMM_reg, rGLMM_reg, extract_re_hyper_matrices


Calibrate reference radius r(delta) and deficiency d = r^2/2.

Description

Inverts the Gaussian-reference escape tail on BB-weights,

Pr⁡(∑iwiZi2>r2)=δ,wi=κi1−κi,\Pr\Bigl(\sum_i w_i Z_i^2 > r^2\Bigr) = \delta, \qquad w_i = \frac{\kappa_i}{1-\kappa_i},

then sets d = r^2/2 (equivalently 2d = r^2) and set_cut = exp(-d).

Usage

deficiency_calibrate(delta, spectrum)

Arguments

delta

Tail / escape probability budget in (0, 1).

spectrum

A list from deficiency_spectrum with weights.

Details

Weighted-χ2\chi^2 tails for general spectra use CompQuadForm::imhof() when the suggested package CompQuadForm is installed, otherwise mgcv::psum.chisq() if mgcv is available.

Value

A list with r2, r, d, delta, escape_mass, set_cut, and spectrum.

See Also

epsilon, deficiency_spectrum


Coupling eigenvalue spectrum at the population mode.

Description

Returns symmetrized coupling eigenvalues κi=eig(A)\kappa_i = \mathrm{eig}(A) and deficiency weights wi=κi/(1−κi)=eig(B)w_i = \kappa_i/(1-\kappa_i) = \mathrm{eig}(B) with A=P11−1/2SP11−1/2A = P_{11}^{-1/2} S P_{11}^{-1/2}, S=P11J~S = P_{11}\tilde J, B=A(I−A)−1B = A(I-A)^{-1} (spectrally; matrix product only when AA and P11P_{11} share eigenvectors). Computed at the terminal conditional-mean E-step of population_mode.

Usage

deficiency_spectrum(mode)

Arguments

mode

A population_mode result with p11 and tilde_J.

Details

Limits: κi→0⇒wi→0⇒d→0\kappa_i \to 0 \Rightarrow w_i \to 0 \Rightarrow d \to 0; κi→1⇒wi→∞⇒d→∞\kappa_i \to 1 \Rightarrow w_i \to \infty \Rightarrow d \to \infty.

Value

A list with kappa, weights, kappa_max, rho, q, and method.

See Also

deficiency_calibrate, epsilon


Build a named list of dGamma measurement-dispersion priors

Description

Generic for constructing a named list of dGamma prior objects from a prior-specification object. For mixed models, one dGamma per group level supplies observation-level σj2\sigma^2_j priors for lmerb(..., group.dispersion = ...).

Usage

dGamma_list(object, ...)

Arguments

object

A prior-specification object.

...

Additional arguments passed to methods (e.g. max_disp_perc_measurement). For print.dGamma_list(), passed to print.pfamily.

Details

Where pfamily_list configures the between-group variance components τk2\tau^2_k, dGamma_list() configures the within-group observation variance. It returns one dGamma prior per group level, so each group gets its own σj2\sigma^2_j rather than sharing a pooled value.

Each element places a Gamma prior on the precision 1/σj21/\sigma^2_j (hence Inv_Dispersion = TRUE) and carries a truncation window disp_lower/disp_upper calibrated from that group's own data. Because groups differ in size and in residual scatter, the windows differ across groups – often by an order of magnitude – which is the point of the per-group treatment.

For Prior_Setup_GLMM with a per-group dispformula, each group's dGamma() uses shape_ING/rate from object$group.ing_prior (Part VI calibration; see inst/DGAMMA_LIST_MARGINAL_AND_BOUNDS.md). Truncation bounds are posterior-shape quantiles at that group's max_disp_perc.

Value

For dGamma_list(), an object of class "dGamma_list" (also inherits from "list"): a named list of "pfamily" objects (one per group). Attributes such as window_diagnostics and measurement_prior_group are retained for downstream use but are not shown by print.dGamma_list(). For print.dGamma_list(), x invisibly.

See Also

dGamma, Prior_Setup_GLMM, pfamily_list, rLMM_reg


Restricted-chain Doeblin multiplier epsilon from spectrum-calibrated sizing.

Description

Combines the mode profile eps_star with the reference radius r(delta) and deficiency d = r^2/2 from the BB-spectrum weights wi=κi/(1−κi)w_i = \kappa_i/(1-\kappa_i) at mode. Produces eps_d = exp(-d) eps_star and eps = eps_d Q(C_tilde_d) with Q(C_tilde_d) at Pr(sum kappa_i Z_i^2 <= r^2).

Usage

epsilon(eps_star, delta, mode)

Arguments

eps_star

Mode profile ε(γ⋆)\varepsilon(\gamma^\star) from epsilon_star.

delta

Tail budget used to calibrate d.

mode

A population_mode result with p11 and tilde_J. The spectrum uses the terminal conditional-mean E-step.

Details

Weighted-χ2\chi^2 tails for general coupling spectra require suggested package CompQuadForm (preferred) or mgcv.

Value

A list with eps_star, d, r, r2, eps_d, Q_mass_lb, eps, spectrum, kappa, cal, and route.


Numerical epsilon(gamma*) from the minorization profile.

Description

Solves the convex program of Chapter C05 Definition 5 at the EM fixed point: minimize g(γ′∣γ⋆)=log⁡q(γ′∣γ⋆)−log⁡qQ(γ′)g(\gamma' \mid \gamma^\star) = \log q(\gamma' \mid \gamma^\star) - \log q_Q(\gamma') over destination γ′\gamma', with the kernel integral over β\beta uses an exact Gaussian convolution when family = gaussian(), otherwise Monte Carlo draws from π(β∣γ⋆,y)\pi(\beta \mid \gamma^\star, y).

Usage

epsilon_optimize(
  mode,
  n = NULL,
  mc_seed = NULL,
  gamma_prime_start = NULL,
  control = list(maxit = 200L),
  ...
)

Arguments

mode

Object returned by population_mode.

n

Number of β\beta draws for the MC inner integral when the likelihood is not Gaussian. Defaults to mode$n from population_mode, or 10000L.

mc_seed

Optional seed for the cached β\beta draws (fixed for the whole optimization).

gamma_prime_start

Initial destination γ′\gamma'; defaults to mode$gamma_star.

control

A list passed to optim (method = "BFGS").

...

Ignored.

Value

A list with eps_star, method, certified, g_opt, gamma_prime, optim, and n.

See Also

population_mode, epsilon_star


Mode profile epsilon(gamma*) for the restricted Gibbs certificate.

Description

Mode profile epsilon(gamma*) for the restricted Gibbs certificate.

Usage

epsilon_star(mode, method = c("closure", "optimize"), ...)

Arguments

mode

Object returned by population_mode.

method

"closure" uses det⁡(I+J~)−1/2\det(I+\tilde J)^{-1/2} when available (Gaussian reference); "optimize" calls epsilon_optimize.

...

Passed to epsilon_optimize when method = "optimize".

Value

A list with eps_star, method, and certified.


Floor coupling eigenvalues from data-precision lower bounds.

Description

Forms

SLB=∑jHj⊤Pb(P22,jLB)−1PbHjS^{\mathrm{LB}} = \sum_j H_j^\top P_b (P_{22,j}^{\mathrm{LB}})^{-1} P_b H_j

and returns κiLB=eig(P11−1/2SLBP11−1/2)\kappa_i^{\mathrm{LB}} = \mathrm{eig}(P_{11}^{-1/2} S^{\mathrm{LB}} P_{11}^{-1/2}). Rosenthal drift constants are computed separately by rosenthal_drift_constants.

Usage

floor_coupling_eigenvalues(mode, floor)

Arguments

mode

A population_mode result with p11.

floor

A group_precision_floor result with Gamma_lb.

Value

A list with kappa_lb, kappa_max_lb, S_lb, and q.

See Also

rosenthal_drift_constants, rosenthal_tv_bound


Backward-compatible wrapper combining eigenvalues and drift constants.

Description

Backward-compatible wrapper combining eigenvalues and drift constants.

Usage

floor_coupling_spectrum(mode, beta_set)

Arguments

mode

A population_mode result.

beta_set

A group_precision_floor or legacy object with Gamma_lb.

Value

List from floor_coupling_eigenvalues plus Rosenthal drift fields from rosenthal_drift_constants.


Sharpest displayed gamma-beta total-variation certificate.

Description

Orchestrates categories 1–8: population_mode, epsilon_star, beta_marginal_mode, beta_marginal_safe_set, group_precision_floor, floor_coupling_eigenvalues, and optimal_rosenthal_tv_bound.

Usage

gamma_beta_tv_certificate(
  design,
  pfamily_list,
  family = gaussian(),
  delta_2 = 0.01,
  dispprior_list = NULL,
  k = NULL,
  tol = NULL,
  inner_tol = NULL,
  total_tol = NULL,
  alpha = NULL,
  display_mode = c("sharp", "general"),
  include_full_pi_gamma = TRUE,
  estep = c("exact", "aghq", "mc"),
  kappa_method = c("laplace", "crude", "none"),
  acceleration = c("none", "squarem"),
  n = 10000L,
  mc_seed = NULL,
  icm_init = TRUE,
  icm_tol = 1e-08,
  icm_maxit = 200L,
  em_tol = 1e-10,
  maxit = 200L,
  delta = NULL,
  optimize_alpha = TRUE,
  verbose = FALSE
)

Arguments

design

A model_setup list.

pfamily_list

Block~2 prior list from pfamily_list().

family

A family object.

delta_2

Asymptotic tail allowance for the full marginal.

dispprior_list

Optional Block~1 dispersion prior for gaussian().

k

If set, evaluate at this sweep count only.

tol

Alias for inner_tol when inverting for sweep count.

inner_tol

Target for the inner Rosenthal bound.

total_tol

Target for full πγ\pi_\gamma bound when include_full_pi_gamma = TRUE (uses total_tol - delta_2).

alpha

If set, skip alpha optimization.

display_mode

"sharp" (default) or "general".

include_full_pi_gamma

If TRUE, report full πγ\pi_\gamma bound as inner bound plus delta_2.

estep

E-step tier: "exact" (default Gaussian closed form when unrestricted), "mc" (simulated conditional means; used automatically when beta_set is supplied), or "aghq" (not yet implemented).

kappa_method

Passed to group_precision_floor.

acceleration

"none" or "squarem" (not yet implemented).

n

Number of βj\beta_j draws per group when estep = "mc". The sample mean bˉj\bar b_j has elementwise MC standard error sd(βj∣γ,y)/n\mathrm{sd}(\beta_j \mid \gamma, y)/\sqrt{n}; relative error on means is therefore O(1/n)O(1/\sqrt{n}) (e.g. n = 10000 targets ≈1%\approx 1\% of the posterior scale).

mc_seed

Optional seed for the MC E-step (first iteration only).

icm_init

If TRUE, run iterated conditional modes (Block~1 modes via rglmb, Block~2 via the C05 mean map) before EM. Skipped on the Gaussian exact closed-form path. Gaussian Block~1 uses exact conditional modes.

icm_tol

ICM convergence tolerance on ∥Δγ∥P11\|\Delta\gamma\|_{P_{11}}.

icm_maxit

Maximum ICM iterations.

em_tol

EM convergence tolerance passed to population_mode as tol.

maxit

Maximum EM iterations.

delta

Tail budget for display_mode = "general" minorization via epsilon (not used in the default sharp display).

optimize_alpha

If TRUE, minimize over alpha.

verbose

If TRUE, pass verbose to beta_marginal_safe_set.

Value

An object of class "gamma_beta_tv_certificate".

See Also

certificate, optimal_rosenthal_tv_bound


Conditional means of group random effects given population effects

Description

Computes the C05 EM E-step: per-group conditional means E[βj∣γ,y]E[\beta_j \mid \gamma, y] (and posterior covariances VjV_j) at a supplied population location fixef. For gaussian() with estep = "exact" and no beta_set, the posterior mean is closed form. Otherwise means are Monte Carlo averages.

Usage

group_effects_conditional_mean(
  design = NULL,
  pfamily_list = NULL,
  family = NULL,
  fixef = NULL,
  mode = NULL,
  dispprior_list = NULL,
  beta_set = NULL,
  estep = c("exact", "aghq", "mc"),
  n = 10000L,
  mc_seed = NULL
)

Arguments

design

A model_setup list (required if mode is NULL).

pfamily_list

Block~2 prior list (required if mode is NULL).

family

A family object (required if mode is NULL).

fixef

Named list of population fixed effects (γ\gamma in C05 notation). Ignored when mode is supplied.

mode

Optional result of population_mode; supplies fixef, design, p11, and model context.

dispprior_list

Optional Block~1 dispersion prior for gaussian().

beta_set

Optional beta_marginal_safe_set object (B~(δ2)\widetilde B(\delta_2)); enables joint-draw rejection (see Details).

estep

"exact" (Gaussian closed form when unrestricted), "mc" (simulated means), or "aghq" (not yet implemented). Ignored when beta_set is supplied (always uses rejection MC).

n

Target number of accepted joint draws when beta_set is set; otherwise number of βj\beta_j draws per group when estep = "mc" (required for non-Gaussian families).

mc_seed

Optional RNG seed for the MC E-step.

Details

When beta_set (B~(δ2)\widetilde B(\delta_2)) is supplied, means are Monte Carlo averages of accepted joint draws from rNormal_reg_group_safe or rNormalGLM_reg_group_safe (by family).

Value

An object of class "group_effects_conditional_mean" with b_mean, V_list, b_mc_se (when MC is used), fixef, estep, n, n_target, n_tried, accept_rate (when beta_set is set), beta_set, restricted, and model context (design, family, p11).

See Also

population_mode, beta_marginal_safe_set, rNormal_reg_group_safe, rNormalGLM_reg_group_safe


Certified group-level data precision lower bounds

Description

Compute per-group lower-bound data precision matrices Γj\Gamma_j on a certified beta level set. Typically used after beta_marginal_mode and beta_marginal_safe_set.

Usage

group_precision_floor(
  design = NULL,
  pfamily_list = NULL,
  family = NULL,
  beta_mode = NULL,
  beta_set = NULL,
  epsilon = 0.01,
  dispprior_list = NULL,
  offset = NULL,
  weights = NULL,
  kappa_method = c("crude", "laplace", "none"),
  union_over = c("groups", "functionals"),
  mode_method = c("marginal_newton", "icm"),
  level_method = c("prop2_groups", "prop2_functionals", "r_gauss_joint"),
  tol = 1e-10,
  maxit = 200L,
  verbose = FALSE
)

Arguments

design

Used when beta_mode and beta_set are both NULL (legacy standalone path).

pfamily_list

Block~2 prior list.

family

A family object.

beta_mode

Result of beta_marginal_mode (preferred).

beta_set

Result of beta_marginal_safe_set supplying the joint level rGaussr_{\mathrm{Gauss}}; if NULL, epsilon and level_method define the set.

epsilon

Escape budget when building levels without beta_set.

dispprior_list

Optional Block~1 dispersion prior for gaussian().

offset, weights

Observation offset and weights; default from design.

kappa_method

"crude" (certified), "laplace", or "none".

union_over

"groups" or "functionals" (Prop 2 schemes).

mode_method

Default "marginal_newton"; ignored when beta_mode is supplied.

level_method

Default "r_gauss_joint" when beta_set is supplied; otherwise "prop2_groups" for legacy.

tol, maxit

Newton controls (unused beyond initial solve).

verbose

If TRUE, emit message notes (legacy Prop 2 path).

Value

An object of class "group_precision_floor".

See Also

beta_marginal_safe_set, floor_coupling_eigenvalues


Model setup for generalized linear mixed models

Description

Wrapper around lmer or glmer for models with exactly one grouping factor. Design matrices come from formula (including cross-level RE moderation terms). Random-effect and residual variance components (Psi, dispersion) come from the same reference lmer/glmer fit on formula, so printed summaries match summary(lmer(...)) on that formula.

Usage

model_setup(
  formula,
  data = NULL,
  family = gaussian(),
  REML = TRUE,
  control = NULL,
  start = NULL,
  verbose = 0L,
  subset,
  weights,
  na.action,
  offset,
  contrasts = NULL,
  devFunOnly = FALSE,
  fit_mer = TRUE,
  dispformula = ~1,
  ...
)

Arguments

formula

A two-sided linear formula, as in lmer: response on the left of ~, fixed and random-effects terms ((terms | group)) on the right. Used for both design extraction and the reference lmer/glmer fit (fixed effects, hyper calibration, and variance components); see Details for the single-grouping-factor / || constraints.

data

An optional data frame containing the variables named in formula (as in lmer); if omitted, variables are taken from the environment of formula.

family

As in glm: a character string naming a family function, a family function, or the result of a call to one. Defaults to gaussian(), in which case lmer is used. Non-Gaussian families use glmer.

REML

Logical scalar, as in lmer's own REML argument: should the reference fit's estimates optimize the REML criterion rather than the log-likelihood? Passed to lmer when family = gaussian(); ignored otherwise (glmer has no REML criterion).

control

lmerControl when family = gaussian(), otherwise glmerControl – including the nonlinear optimizer and its settings (as in lmer/glmer's own control argument); passed through to the reference fit when fit_mer = TRUE. NULL (default) resolves to lmerControl()'s defaults for Gaussian families, or glmer's own default control otherwise.

start

Optional starting values, passed to the reference lmer/glmer fit's own start argument (numeric vector or named list with par/theta, and for glmer also fixef/beta; see their documentation). NULL (default) uses lme4's own defaults.

verbose

Integer scalar passed to the reference lmer (or glmer for non-Gaussian family); see their verbose argument (> 0 prints optimizer iterations; > 1 also prints inner PIRLS steps).

subset

An optional expression indicating the subset of rows of data to use, as in lmer's subset argument. Passed to both extract_re_hyper_matrices (via lFormula) and the reference lmer/glmer (and glmmTMB when used) so the sampler design matrices and the classical reference fit share the same rows.

weights

An optional vector of prior weights, as in lmer's weights argument. Passed to design extraction (lFormula), the reference lmer/glmer fit, and glmmTMB when used, so dropped rows match subset/ na.action. Stored on the returned design as length-n weights (default all ones). Mixed-model sampler engines that still hard-code unit weights do not consume this vector yet; see Prior_Setup_GLMM.

na.action

A function indicating what should happen when data contain NAs (default na.omit, as in lmer). Passed to both design extraction (lFormula) and the reference fit so dropped rows match.

offset

An optional a priori known component to include in the linear predictor, as in lmer's offset argument (plus any offset() term in formula). Passed to design extraction and the reference fit; stored on the returned design as length-n offset (default all zeros). Mixed-model sampler engines that still hard-code a zero offset do not consume this vector yet.

contrasts

An optional list of contrasts (see the contrasts.arg of model.matrix.default, as in lmer). Passed to both design extraction (lFormula) and the reference fit so factor coding in W/D matches fixef/vcov.

devFunOnly

Logical, as in lmer's devFunOnly argument: if TRUE, return only the deviance evaluation function instead of a fitted model. Gaussian lmer fits only; ignored for glmer.

fit_mer

If TRUE (default), fit the reference lmer/glmer model and extract variance components – analogous to glmmTMB's doFit argument. If FALSE, skip the reference fit and return design matrices and rank diagnostics only (used by glmerb() in lmebayes, which fits its own glmer separately).

dispformula

As in glmmTMB's dispformula argument: a one-sided formula selecting the measurement-dispersion structure: ~1 (default, pooled) or ~<group_name> (matching the random-effects grouping factor exactly, requesting per-group dispersion). Only affects design$glmmTMB_fit (see @return); design$lmer/design$glmer are always the plain pooled-dispersion lmer/glmer fit, regardless of dispformula. ~<group_name> additionally requires family = gaussian() (no observation-level dispersion parameter to model per group otherwise). Mirrors the dispformula argument on Prior_Setup_GLMM and on lmerb()/glmerb() in lmebayes; all three are independent arguments that must be kept consistent by the caller.

...

Ignored.

Details

Uncorrelated random effects (||). The sampler treats Sigma_ranef as diagonal (no off-diagonal covariance). Multi-coefficient random terms must use ||, e.g. (1 + x || group) rather than (1 + x | group). A single random intercept may use (1 | group); (1 || group) is not supported by lme4.

Fixed-effect constraints. model_setup accepts the same formula language as lmer, subject to one structural rule: every fixed effect that does not correspond to a random-slope term must be a group-constant (level-2) covariate—a predictor whose value is the same for every observation within a given group. School-level attributes such as private_school or title1 satisfy this constraint. Student-level covariates that vary within groups may appear as fixed main effects only when they also appear as random slopes (they then represent the population mean slope γ10\gamma_{10}, e.g., distracted_ppvt). Cross-level interactions of the form level2_var:random_slope (e.g., free_reduced_lunch:distracted_a1) are additionally permitted; they moderate the prior mean of the corresponding random slope across groups (see extract_re_hyper_matrices). Fixed terms that are none of these three types—level-2 covariate, population mean slope, or cross-level moderation interaction—are rejected with an informative error.

Two-step identifiability assessment. After fitting lmer/glmer, model_setup calls check_identifiability to assess whether the model is empirically identified at both the within-group (groupef.rank, groupef.estimable, groupef.glm_check) and across-group (popef.rank, popef.deficient, popef.rank_ok) levels, and copies its fields onto the returned design object – see check_identifiability for the full algorithm description (including the family-specific estimability checks and how popef.rank_ok is derived). Non-estimable groups are flagged but retained in the lmer/glmer fit; Prior_Setup_GLMM excludes them when calibrating priors, and requires popef.rank_ok = TRUE to derive default hyperpriors automatically.

The example uses big_word_club from bayesrules (see ?bayesrules::big_word_club) and the same formula as the full lmerb() demo in lmebayes (demo("Ex_12_lmerb_BigWordClub", package = "lmebayes")).

Value

Object of class "model_setup" with:

y

Response vector, length nrow(D).

weights

Numeric prior-weight vector of length length(y) (ones when unspecified), aligned with y/ D/group after subset/na.action.

offset

Numeric offset vector of length length(y) (zeros when unspecified and no formula offset()), aligned with y/D/group.

D

Group-effects model matrix (n_obs x p_re): per-observation loadings on the within-group group-effect coefficient vector (columns groupef.names).

group

Factor of length nrow(D) for block subsetting; levels(group) fixes group order used elsewhere. Also carries attr(group, "group_name") (same value as group_name) so group alone can be passed to sampler engine functions without a separate group_name argument.

group_name

Grouping factor name.

groupef.names

Group-effect coefficient names from lme4; always identical to names(W) (and to colnames(D)).

W

Named list of group-level hyper-design matrices (one per groupef.names entry, one row per group level).

popef.moderation

Cross-level moderation metadata; see extract_re_hyper_matrices.

formula

The formula argument, as supplied.

family

The normalized family object.

call

The matched call.

terms

The terms object for formula, as in lm/glm (and glmbayes::glmb/lmb).

data

The data argument, as supplied (NULL when omitted), as in lm/glm (and glmbayes::glmb/lmb).

dispformula

The dispformula argument, as supplied.

lmer / glmer

The reference lmer (Gaussian) or glmer (otherwise) fit on formula; NULL when fit_mer = FALSE.

varcorr

The raw VarCorr object from the reference fit.

Psi

Named numeric vector (names groupef.names) of group-effect variances.

dispersion

Residual (measurement) variance from the reference fit.

glmmTMB_fit

NULL when dispformula = ~1 or fit_mer = FALSE; otherwise a glmmTMB fit on formula with the same dispformula (Gaussian models only), fit via the same data/REML/ control-derived arguments as lmer. This is additive: lmer/glmer are never replaced by a glmmTMB fit. Prior_Setup_GLMM and lmerb() (in lmebayes) reuse glmmTMB_fit as their per-group-dispersion calibration reference instead of fitting glmmTMB a second time.

groupef.rank

Named logical (names levels(group)): TRUE if that group's DjD_j is full column rank; see check_identifiability.

groupef.estimable

Named logical, same names as groupef.rank: TRUE if the group is additionally estimable (family-specific MLE/dispersion check); see check_identifiability.

groupef.glm_check

Per-group diagnostic data frame from check_identifiability (family-dependent; may be NULL).

popef.rank

Named logical (names groupef.names): TRUE if W[[k]] is full column rank when restricted to groupef.estimable groups.

popef.deficient

Negation of popef.rank.

popef.rank_ok

Scalar TRUE only when every popef.rank entry is TRUE; required by Prior_Setup_GLMM to derive default hyperpriors automatically.

See Also

check_identifiability, extract_re_hyper_matrices


Legacy MVN-calibrated reference radius (equal-weight chi-square shortcut).

Description

Computes d = 0.5 * qchisq(1 - delta, q). This equal-weight route is retained for reference only; epsilon and certificate use deficiency_calibrate on the coupling spectrum from deficiency_spectrum instead.

Usage

mvn_calibrate(q, delta)

Arguments

q

Hyperparameter dimension.

delta

Tail / escape probability budget in (0, 1).

Value

A list with r2, r, d, and set_cut = exp(-d).

See Also

deficiency_calibrate, epsilon


Optimize Rosenthal TV bound over alpha and/or sweep count k.

Description

Optimize Rosenthal TV bound over alpha and/or sweep count k.

Usage

optimal_rosenthal_tv_bound(
  mode,
  eigenvalues,
  eps,
  k = NULL,
  alpha = NULL,
  inner_tol = NULL,
  total_tol = NULL,
  delta_2 = 0,
  include_full_pi_gamma = TRUE,
  optimize_alpha = TRUE,
  display_mode = c("sharp", "general"),
  d = NULL,
  V_sup = NULL
)

Arguments

mode

A population_mode result.

eigenvalues

Output of floor_coupling_eigenvalues.

eps

Minorization constant for the bound.

k

If set, evaluate at this sweep count only.

alpha

If set, skip alpha optimization.

inner_tol

Target for the inner Rosenthal bound.

total_tol

Target for full πγ\pi_\gamma bound when include_full_pi_gamma = TRUE (uses total_tol - delta_2).

delta_2

Asymptotic tail allowance for the full marginal.

include_full_pi_gamma

If TRUE, set full_bound to inner bound plus delta_2.

optimize_alpha

If TRUE, minimize over alpha.

display_mode

"sharp" (default) or "general".

d

Deficiency level for Vsup⁡(d)V_{\sup}(d) when V_sup is omitted.

V_sup

Override for Vsup⁡V_{\sup} in general mode.

Value

A list of class "optimal_rosenthal_tv_bound".

See Also

rosenthal_tv_bound, gamma_beta_tv_certificate


Build a named list of pfamily objects from a prior-setup object

Description

Generic for converting a prior-setup object into a named list of pfamily objects (class "pfamily_list") for block or mixed-model samplers.

Usage

pfamily_list(object, ptypes = NULL, ...)

Arguments

object

A prior-setup object. Dispatch is method-specific; see Methods and linked help pages for required classes and arguments.

ptypes

Prior-family name(s) per component; meaning depends on the active method (for row-block setup see Prior_SetupGroup).

...

Passed to methods (unused for Prior_SetupGroup).

Details

For a Prior_Setup_GLMM object, each group-effect coefficient kk uses parameters from object$pop.prior_list[[k]]:

  • "dNormal": dNormal(mu, Sigma, dispersion) with group-effect variance τk2\tau^2_k from pop.prior_list[[k]]$dispersion (A12 §3.3.4 marginal hyper-regression center for Gaussian models; classical VarCorr in pop.dispersion.ref).

  • "dIndependent_Normal_Gamma": the same mu and Sigma, plus a Gamma prior on precision 1/τk21/\tau^2_k from object$pop.ing_prior[[k]] (calibrated by Prior_Setup_GLMM via pop.dispersion.pwt / pop.dispersion.nprior, mean-matched at that same marginal τk2\tau^2_k). With effective prior sample size n0n_0 and pkp_k population predictors for component kk,

    shape=(n0+1+pk)/2,rate=τk2 (n0+pk−1)/2.shape = (n_0 + 1 + p_k)/2, \qquad rate = \tau^2_k \, (n_0 + p_k - 1)/2.

    Requires n0≤Jn_0 \le J (equivalently pop.dispersion.pwt ≤0.5\le 0.5). Truncation bounds disp_lower / disp_upper come from the limiting posterior window stored on pop.ing_prior; see inst/ING_TRUNCATION_WINDOW.md.

This is separate from observation-level σ2\sigma^2 priors: those use dGamma_list (per-group dispformula) or a single dGamma built from object$group.ing_prior when dispersion is pooled.

Value

For pfamily_list(), an object of class "pfamily_list" (also inherits from "list"): a named list of pfamily objects. Attribute "ptypes" records the resolved prior-family name per component when the method sets it. For print.pfamily_list(), x invisibly.

See Also

Prior_SetupGroup, Prior_Setup_GLMM, pfamily, dNormal, dNormal_Gamma, dIndependent_Normal_Gamma


Combined mean-convergence plot for Block~2 fixed effects (Claim 1)

Description

Tracks convergence of the Block~2 fixed-effects (hyperparameter) posterior mean across inner Gibbs sweeps as a single combined chart (all traces overlaid on one axis), the mean-side counterpart of plot_var_convergence's variance-side Claim~3 chart. Per the two-block Gibbs ergodicity result's Claim~1, the cross-chain mean bias E[γ(l)]−γ∗E[\gamma^{(l)}] - \gamma^* decays like AlA^l (linear in l, vs. Claim~3's A2lA^{2l} for variance). Each coefficient's per-sweep deviation from its reference mean is standardized by that same sweep's cross-chain SD, Z(l)=(mean(l)−meanref)/sd(l)Z(l) = (\mathrm{mean}(l) - \mathrm{mean}_{ref}) / \mathrm{sd}(l), so coefficients on very different scales can be overlaid on one chart and Z(l) is expected to shrink toward 0 as l grows.

Usage

plot_mean_convergence(hist, ...)

## Default S3 method:
plot_mean_convergence(
  hist,
  coef_focus = NULL,
  component = c("fixef", "precision"),
  design = NULL,
  measurement_prior_list = NULL,
  whitened = FALSE,
  engine = c("base", "ggplot"),
  n_chains = NULL,
  conf_level = 0.95,
  stage_label = hist$stage,
  split = c("auto", "none"),
  max_whitened = 4L,
  ...
)

## S3 method for class 'rLMMNormal_reg'
plot_mean_convergence(
  hist,
  coef_focus = NULL,
  component = c("fixef", "precision"),
  whitened = FALSE,
  engine = c("base", "ggplot"),
  n_chains,
  conf_level = 0.95,
  stage = c("main", "pilot"),
  stage_label = NULL,
  ...
)

## S3 method for class 'rLMMindepNormalGamma_reg'
plot_mean_convergence(
  hist,
  coef_focus = NULL,
  component = c("fixef", "precision"),
  whitened = FALSE,
  engine = c("base", "ggplot"),
  n_chains,
  conf_level = 0.95,
  stage = c("main", "pilot"),
  stage_label = NULL,
  ...
)

## S3 method for class 'rlmerb'
plot_mean_convergence(
  hist,
  coef_focus = NULL,
  component = c("fixef", "precision"),
  whitened = FALSE,
  engine = c("base", "ggplot"),
  n_chains,
  conf_level = 0.95,
  stage = c("main", "pilot"),
  stage_label = NULL,
  ...
)

## S3 method for class 'rGLMM_reg'
plot_mean_convergence(
  hist,
  coef_focus = NULL,
  component = c("fixef", "precision"),
  whitened = FALSE,
  engine = c("base", "ggplot"),
  n_chains,
  conf_level = 0.95,
  stage = c("main", "pilot"),
  stage_label = NULL,
  ...
)

## S3 method for class 'rglmerb'
plot_mean_convergence(
  hist,
  coef_focus = NULL,
  component = c("fixef", "precision"),
  whitened = FALSE,
  engine = c("base", "ggplot"),
  n_chains,
  conf_level = 0.95,
  stage = c("main", "pilot"),
  stage_label = NULL,
  ...
)

Arguments

hist, coef_focus, component, engine, n_chains, conf_level, split, max_whitened

Same meaning as in plot_var_convergence.

...

Passed to methods; see plot_mean_convergence.default.

design, measurement_prior_list

When both are supplied, the exact reference mean is resolved automatically via lmerb_posterior_mean and, for whitened = TRUE, the exact reference covariance (via lmerb_posterior_covariance) needed to whiten the mean deviation vector; see plot_var_convergence for when these are available.

whitened

When FALSE (default), plots Z(l) per named coefficient in coef_focus. When TRUE, whitens each sweep's cross-chain mean deviation vector by the reference covariance (Σref−1/2(mean(l)−meanref)\Sigma_{ref}^{-1/2} (\mathrm{mean}(l) - \mathrm{mean}_{ref})) and plots its components – basis-invariant – labeled z1, z2, ... instead of named coefficients.

stage_label

Defaults to the resolved hist$stage; pass explicitly to override.

stage

"main" (default) or "pilot"; see plot_var_convergence.rLMMNormal_reg.

Details

What is plotted

The x-axis is the inner Gibbs sweep index ll, and each point is a cross-chain statistic computed over the n_chains independent replicate chains, all of which sit at sweep ll simultaneously. Nothing here is a running statistic along a single chain.

The plotted quantity is the standardized deviation

Z(l)=mean(l)−meanrefsd(l),Z(l) = \frac{\mathrm{mean}(l) - \mathrm{mean}_{ref}}{\mathrm{sd}(l)},

which divides out the scale of each coefficient so that an intercept measured in hundreds and a slope measured in tenths can share one axis. A converged run shows every trace scattered around 0 with no trend; a systematic drift toward or away from 0 means the sweep budget is too small. Supplying n_chains adds a naive band that separates Monte Carlo noise from genuine bias.

mean_ref is the exact posterior mean when design and measurement_prior_list are supplied (only possible when σ2\sigma^2 and the RE variance components are all fixed), and otherwise the empirical last-sweep mean. As with plot_var_convergence, the empirical fallback cannot detect a run that never converged, because the reference is drawn from the same unconverged distribution.

Reading it alongside the variance chart

This chart is the location-side counterpart of plot_var_convergence, which tracks spread. The variance converges at twice the rate of the mean, so this is the slower — and therefore the more informative — of the two when deciding whether m_convergence is large enough. A fit whose variance ratios have settled at 1 while these traces still drift has converged in spread but not in location.

Because every replicate chain starts at the posterior mean, the bias begins at zero on Gaussian fits with fixed variance components: expect a flat chart from sweep 1 there. Read a non-flat one as evidence that something upstream — an estimated variance component, or a start at the mode rather than the mean — has moved the starting point off the target mean. vignette("Chapter-B02", package = "lmebayesCore") derives the two rates.

When there is nothing to plot

As for plot_var_convergence: the exact route runs no sweeps to record, some Gaussian paths do not export per-sweep cross-chain statistics, and whitened = TRUE needs slots that only the sweep-recording engines populate. See that topic, which also explains how to obtain n_chains from a fitted object.

Value

hist invisibly.

See Also

plot_var_convergence, lmerb_posterior_mean


Combined variance-convergence plot for Block~2 fixed effects (Claim 3)

Description

Tracks convergence of the Block~2 fixed-effects (hyperparameter) posterior variance across inner Gibbs sweeps as a single combined chart (all traces overlaid on one axis, following lmebayes::Ex_16's style), per the two-block Gibbs ergodicity result (Claim~3 of the package's ergodicity reference, inst/BLOCK_GIBBS_ERGODICITY.md): after l sweeps, the cross-chain covariance Σ11(l)\Sigma^{(l)}_{11} satisfies

Σ11−1/2Σ11(l)Σ11−1/2=I−A2l\Sigma_{11}^{-1/2} \Sigma^{(l)}_{11} \Sigma_{11}^{-1/2} = I - A^{2l}

for a convergent (symmetric, eigenvalues in [0,1)[0, 1)) matrix AA. Var(l) / Var_ref ratios are therefore bounded above by 1 and increase toward it as l grows.

Usage

plot_var_convergence(hist, ...)

## Default S3 method:
plot_var_convergence(
  hist,
  coef_focus = NULL,
  component = c("fixef", "precision"),
  design = NULL,
  measurement_prior_list = NULL,
  whitened = FALSE,
  engine = c("base", "ggplot"),
  n_chains = NULL,
  conf_level = 0.95,
  stage_label = hist$stage,
  split = c("auto", "none"),
  max_whitened = 4L,
  ...
)

## S3 method for class 'rLMMNormal_reg'
plot_var_convergence(
  hist,
  coef_focus = NULL,
  component = c("fixef", "precision"),
  whitened = FALSE,
  engine = c("base", "ggplot"),
  n_chains,
  conf_level = 0.95,
  stage = c("main", "pilot"),
  stage_label = NULL,
  ...
)

## S3 method for class 'rLMMindepNormalGamma_reg'
plot_var_convergence(
  hist,
  coef_focus = NULL,
  component = c("fixef", "precision"),
  whitened = FALSE,
  engine = c("base", "ggplot"),
  n_chains,
  conf_level = 0.95,
  stage = c("main", "pilot"),
  stage_label = NULL,
  ...
)

## S3 method for class 'rlmerb'
plot_var_convergence(
  hist,
  coef_focus = NULL,
  component = c("fixef", "precision"),
  whitened = FALSE,
  engine = c("base", "ggplot"),
  n_chains,
  conf_level = 0.95,
  stage = c("main", "pilot"),
  stage_label = NULL,
  ...
)

## S3 method for class 'rGLMM_reg'
plot_var_convergence(
  hist,
  coef_focus = NULL,
  component = c("fixef", "precision"),
  whitened = FALSE,
  engine = c("base", "ggplot"),
  n_chains,
  conf_level = 0.95,
  stage = c("main", "pilot"),
  stage_label = NULL,
  ...
)

## S3 method for class 'rglmerb'
plot_var_convergence(
  hist,
  coef_focus = NULL,
  component = c("fixef", "precision"),
  whitened = FALSE,
  engine = c("base", "ggplot"),
  n_chains,
  conf_level = 0.95,
  stage = c("main", "pilot"),
  stage_label = NULL,
  ...
)

Arguments

hist

Object of class "two_block_sweep_history" (see print.two_block_sweep_history). For whitened = TRUE, hist must also carry cov_by_sweep/coef_index, which are only populated by sweeps-outer/chains-inner engines – i.e. every engine driven by rGLMM_sweep: the ⁠_run_with_pilot()⁠ family (rLMMNormal_reg_estimated_vcov(), rLMMindepNormalGamma_reg_known_vcov(), rLMMindepNormalGamma_reg_estimated_vcov()) and, as of the rGLMM_sweep()-based rewrite of its engine, rLMMNormal_reg_known_vcov(sim_method = "TWO_BLOCK_GIBBS") as well.

...

Passed to methods (e.g. coef_focus, design, whitened, n_chains, ...; see below). Ignored by plot_var_convergence.default beyond the documented formals.

coef_focus

Optional list of length-2 character vectors c(re_component, covariate) to restrict the plot to; default (NULL) uses every coefficient tracked in hist.

component

"fixef" (default) plots the Block~2 fixed-effects/hyperparameter series described above. "precision" instead plots the estimated random-effects precision (1/τk21/\tau^2_k, i.e. the diagonal of Σranef−1\Sigma_{ranef}^{-1}) for every ING (dIndependent_Normal_Gamma) Block~2 component – requires hist$disp_table (populated whenever at least one Block~2 component has an estimated, not fixed, variance – see fit$any_non_normal); errors if that is empty (e.g. a known-vcov fit) or if whitened = TRUE (only the cross-chain variance of each component's precision is captured so far, not its covariance with other components/fixed effects, so whitening is not yet possible – see inst/BLOCK_GIBBS_ERGODICITY.md for the deferred extension). Precision is plotted (not the variance τk2\tau^2_k itself) because E[1/τk2]E[1/\tau^2_k] stays well-defined under a weak/vague prior where E[τk2]E[\tau^2_k] need not be. This is a distinct quantity from the observation-level dispersion estimated by rLMMindepNormalGamma_reg_*() (despite both being called "dispersion" internally) – component = "precision" is always about the RE variance-covariance, never the observation noise variance. design/measurement_prior_list are ignored for component = "precision": no exact reference exists yet for an estimated Σranef\Sigma_{ranef}, so the denominator is always the empirical last-sweep value.

design, measurement_prior_list

When both are supplied, the exact reference covariance is resolved automatically via lmerb_posterior_covariance and used as the denominator – this is not an opt-in toggle, it is used whenever the ingredients for it exist. Only valid when dispersion and the RE variance-covariance are both fixed (not sampled). When omitted (the only option for estimated dispersion or estimated-vcov models), the denominator falls back to the empirical last-sweep cross-chain variance/covariance ("Var_final").

whitened

When FALSE (default), plots Var(l) / Var_ref per named coefficient in coef_focus. When TRUE, whitens each sweep's full cross-chain covariance by the reference covariance (Σref−1/2Cov(l)Σref−1/2\Sigma_{ref}^{-1/2} \mathrm{Cov}(l) \Sigma_{ref}^{-1/2}) and plots its eigenvalues – basis-invariant, so no eigenvectors of the theoretical AA are ever needed – labeled var1, var2, ... instead of named coefficients (per Claim~3, these eigen-directions mix the original coefficients together, so named labels no longer apply).

engine

"base" (default) or "ggplot" (requires ggplot2); both draw one combined chart with all traces overlaid.

n_chains

Optional integer, the number of independent chains each plotted Var(l) (and Var_ref, when it is the empirical Var_final rather than an exact reference) was computed across – i.e. the same n_chains/n_pilot/n passed to the sweep-generating call (see Details). When supplied, draws a single pair of horizontal dotted reference lines: a naive conf_level band around 1 for Ratio(l), under the null that the true ratio at that sweep already equals 1 (unrealistic at small l, but useful as a ruler for how much of the departure from 1 is just Monte Carlo noise). The band's distribution depends on whether Var_ref is exact or empirical (see design/measurement_prior_list above):

  • ref_source = "exact": only Var(l) (the numerator) has sampling error, so Ratio(l)∼χnchains−12/(nchains−1)\mathrm{Ratio}(l) \sim \chi^2_{n_{chains}-1} / (n_{chains}-1).

  • ref_source = "empirical (Var_final)": Var_ref itself is a sample variance/covariance across the same n_chains chains (just at the final sweep), so both numerator and denominator carry sampling error and the classic two-sample variance-ratio result applies instead: Ratio(l)∼F(nchains−1, nchains−1)\mathrm{Ratio}(l) \sim F(n_{chains}-1,\, n_{chains}-1). This is wider than the chi-squared band, and reduces to it exactly as Var_final's own degrees of freedom go to infinity (stats::qf(p, df1, Inf) == stats::qchisq(p, df1) / df1), so both cases are computed via a single stats::qf() call with df2 = Inf for the exact case.

Not series-specific (a single band, not one per coefficient/eigenvalue) since it depends only on n_chains/ref_source. For whitened = TRUE this is an additional approximation regardless of ref_source: whitened eigenvalues are eigenvalues of a Wishart-distributed matrix, not marginally chi-squared/F, and tend to spread out more than this band implies (eigenvalue repulsion), so treat it as an illustrative ruler, not an exact whitened confidence band. Also note that Var(l) and Var_final come from the same n_chains chains at two different sweep indices, not independent samples the way a textbook two-sample F-test assumes – another reason this is a naive/illustrative band rather than an exact one. Omit (the default, NULL) to skip the band entirely.

conf_level

Confidence level for the n_chains band (default 0.95); ignored when n_chains is NULL.

stage_label

Defaults to the resolved hist$stage; pass explicitly to override.

split

"auto" (default) or "none". "auto" draws one separate chart – its own plot page/window, never stacked via layout/par(mfrow = ...) on top of another – per group: for whitened = FALSE, one chart for every coefficient whose re_component is "(Intercept)" ("Intercept predictors") and one for every other re_component combined ("Slope predictors"), whichever of the two is non-empty; for whitened = TRUE, consecutive batches of at most max_whitened eigenvalues each. "none" restores the single-combined-chart behaviour (every tracked coefficient/eigenvalue overlaid on one panel), same as before split existed.

max_whitened

Maximum number of eigenvalue series per chart when whitened = TRUE and split = "auto" (default 4); ignored otherwise.

stage

"main" (default) or "pilot" – which sweep history to plot (fit$sweep_history or fit$pilot$draws$sweep_history). Ignored by plot_var_convergence.default, which always takes hist literally.

Details

What is plotted

The x-axis is the inner Gibbs sweep index ll, not the stored-draw index: these charts diagnose convergence within the burn-in that precedes each stored draw. Each point is a cross-chain statistic at sweep ll — the variance across the n_chains independent replicate chains, all of which are at sweep ll simultaneously — not a running statistic along a single chain. This is why the sweep-outer loop order matters: it is what makes "the sampler's distribution after ll sweeps" an observable quantity.

The y-axis is the ratio Var(l) / Var_ref. Under Claim 3 that ratio is bounded above by 1 and rises toward it, so a converged run shows every trace flattening just below 1. Traces that are still climbing at the last sweep indicate the sweep budget is too small; traces that overshoot 1 substantially are Monte Carlo noise, and n_chains draws the band that says how much overshoot is expected.

Var_ref is the exact posterior variance when design and measurement_prior_list are both supplied (only possible when σ2\sigma^2 and the RE variance components are all fixed), and otherwise the empirical last-sweep variance. The empirical fallback cannot detect a run that has not converged at all, since the denominator is then drawn from the same unconverged distribution; prefer the exact reference whenever the model allows it.

Reading it alongside the mean chart

Location and spread converge at different speeds, and the two plotting functions separate them: plot_mean_convergence tracks the mean, plot_var_convergence the variance. The variance converges at twice the rate, so the mean chart settles later and is usually the one that decides whether the sweep budget is adequate. A run whose variance ratios sit at 1 while its standardized mean deviations still drift has converged in spread but not in location.

Because every chain starts at the posterior mean, the mean chart is flat from sweep 1 on well-behaved Gaussian fits, leaving the variance chart to carry the information. vignette("Chapter-B02", package = "lmebayesCore") derives the two rates.

When there is nothing to plot

sweep_history is recorded only when all chains advance together, one sweep at a time, so that the cross-chain spread at sweep ll is meaningful. Two situations produce no history:

  • The exact route (fixed σ2\sigma^2, all components dNormal, sim_method = "DEFAULT") runs no sweeps at all, because the draws are already exact. There is nothing to diagnose. Passing sim_method = "TWO_BLOCK_GIBBS" forces the iterative path on the same target if you want a history anyway.

  • Some Gaussian paths run sweeps but do not export per-sweep cross-chain statistics; fit$sweep_history is then NULL.

whitened = TRUE additionally requires cov_by_sweep and coef_index, which only the sweep-outer engines above populate, and component = "precision" additionally requires disp_table, which requires at least one ING Block 2 component.

Supplying n_chains

n_chains is not stored on hist itself, so it must be supplied from the fitted object that produced it: for the main-stage history (fit$sweep_history) use nrow(fit$popef[[k]]) for any RE component k (all have the same number of rows, the n passed to the ⁠_run_with_pilot()⁠ call); for the pilot-stage history (fit$pilot$draws$sweep_history) use fit$pilot$n.

Value

hist invisibly.

See Also

plot_mean_convergence, lmerb_posterior_covariance


EM fixed point for the C05 population mode gamma*.

Description

Finds the fixed point of the conditional-mean map (Chapter C05 Stage 1). Uses conditional means for Block~1, not modes.

Usage

population_mode(
  design,
  pfamily_list,
  family = gaussian(),
  dispprior_list = NULL,
  beta_set = NULL,
  estep = c("exact", "aghq", "mc"),
  acceleration = c("none", "squarem"),
  n = 10000L,
  mc_seed = NULL,
  icm_init = TRUE,
  icm_tol = 1e-08,
  icm_maxit = 200L,
  icm_screen = NULL,
  mc_alpha = 0.05,
  mc_stable = 2L,
  tol = 1e-10,
  maxit = 200L,
  verbose = FALSE
)

Arguments

design

A model_setup list.

pfamily_list

Block~2 prior list from pfamily_list().

family

A family object.

dispprior_list

Optional Block~1 dispersion prior for gaussian().

beta_set

Optional beta_marginal_safe_set; enables widetilde-B rejection MC (see Details).

estep

E-step tier: "exact" (default Gaussian closed form when unrestricted), "mc" (simulated conditional means; used automatically when beta_set is supplied), or "aghq" (not yet implemented).

acceleration

"none" or "squarem" (not yet implemented).

n

Number of βj\beta_j draws per group when estep = "mc". The sample mean bˉj\bar b_j has elementwise MC standard error sd(βj∣γ,y)/n\mathrm{sd}(\beta_j \mid \gamma, y)/\sqrt{n}; relative error on means is therefore O(1/n)O(1/\sqrt{n}) (e.g. n = 10000 targets ≈1%\approx 1\% of the posterior scale).

mc_seed

Optional seed for the MC E-step (first iteration only).

icm_init

If TRUE, run iterated conditional modes (Block~1 modes via rglmb, Block~2 via the C05 mean map) before EM. Skipped on the Gaussian exact closed-form path. Gaussian Block~1 uses exact conditional modes.

icm_tol

ICM convergence tolerance on ∥Δγ∥P11\|\Delta\gamma\|_{P_{11}}.

icm_maxit

Maximum ICM iterations.

icm_screen

If TRUE (default when the E-step is Monte Carlo), after ICM run mc_stable MC mean-map fixed-point checks at the ICM fixef; skip the EM loop when all pass. If NULL, enabled whenever icm_init is TRUE and the E-step uses MC.

mc_alpha

Two-sided level for the MC fixed-point screen and EM stop (tol_eff = max(tol, z_{1-alpha/2} * mc_delta_floor)).

mc_stable

Number of consecutive MC fixed-point passes required at the same fixef (ICM screen and EM stopping rule).

tol

Convergence tolerance on the Mahalanobis change in fixef under the C05 metric ∥Δγ∥P11\|\Delta\gamma\|_{P_{11}}. When the E-step is Monte Carlo, the effective tolerance is max(tol, z_{1-alpha/2} * mc_delta_floor).

maxit

Maximum EM iterations.

verbose

If TRUE, emit message() diagnostics for ICM initialization and each EM record (gradient gg, ∥g∥\|g\|, MC log⁡π(γ∣y)\log \pi(\gamma \mid y) when joint safe draws are available).

Details

In the restricted certificate, the EM E-step uses conditional means under the β\beta-truncated target on B~(δ2)\widetilde B(\delta_2). Pass Pass beta_set from beta_marginal_safe_set; routes to rNormal_reg_group_safe (Gaussian) or rNormalGLM_reg_group_safe (non-Gaussian).

Value

A list with fixef (γ⋆\gamma^\star), gamma_star, b_mean, b_mc_se (terminal MC standard errors when estep = "mc"), V_list, n, model context (design, family, measurement_prior_list, p11), refresh objects (P11, Sigma_star), tilde_J, kappa_spectrum, weights, kappa, rho, Sigma_pi, eps_star_closure, icm and icm_screen diagnostics, EM diagnostics (tol_eff, mc_delta_floor, em_route, em_trace), beta_set, restricted, and related fields.

See Also

group_effects_conditional_mean, rNormal_reg_group_safe, rNormalGLM_reg_group_safe


Print a population_mode_em_trace object.

Description

Print a population_mode_em_trace object.

Usage

## S3 method for class 'population_mode_em_trace'
print(x, ...)

Arguments

x

A population_mode_em_trace object (from population_mode$em_trace).

...

Ignored.

Value

x, invisibly.


Prior setup for generalized linear mixed models

Description

Calibrates priors for the level-2 fixed effects (fixef) of a hierarchical mixed model using the reference lmer/glmer fits on all groups (from model_setup). Per-group design rank (groupef.rank) and estimability (groupef.estimable – classical-glm MLE existence for family = binomial()/poisson()/Gamma(), residual degrees of freedom for family = gaussian()) are diagnostic checks computed by model_setup and do not subset the data or the reference glmer fit. Random-effect variances are treated as fixed at their mixed-model estimates. The returned object organizes calibrated priors as two independent blocks:

Usage

Prior_Setup_GLMM(
  formula,
  data,
  family = gaussian(),
  REML = TRUE,
  control = NULL,
  start = NULL,
  verbose = 0L,
  subset,
  weights,
  na.action,
  offset,
  contrasts = NULL,
  dispformula = ~1,
  pop.pwt = NULL,
  pop.pwt_default_low = 0.01,
  pop.pwt_default_high = 0.05,
  pop.nprior = NULL,
  pop.sd = NULL,
  pop.dispersion.pwt = NULL,
  pop.dispersion.nprior = NULL,
  pop.max_disp_perc = 0.99,
  pop.intercept_source = c("null_model", "full_model"),
  pop.effects_source = c("null_effects", "full_model"),
  pop.mu = NULL,
  group.dispersion.pwt = NULL,
  group.dispersion.nprior = NULL,
  group.alpha_target = 0.01,
  group.max_disp_perc = 0.99
)

Arguments

formula

Mixed-model formula passed to model_setup, whose reference lmer/glmer fits (all groups) supply the calibration quantities.

data

Data frame containing all variables in formula.

family

Model family. Default gaussian(). Non-Gaussian families use glmer for calibration; Block~1 dispersion Gamma calibration is omitted (analogous to Prior_Setup for flat GLMs).

REML

Logical scalar passed to model_setup (and thus to the Gaussian reference lmer / glmmTMB fits). Default TRUE. Ignored for non-Gaussian families (glmer has no REML criterion). Affects the classical estimates used for prior calibration only.

control

Optional lmerControl / glmerControl object passed to model_setup. When NULL (default), Gaussian models use lmerControl(optimizer = "bobyqa", optCtrl = list(maxfun = 2e5)) and non-Gaussian models leave glmer's own control default.

start

Optional starting values passed to model_setup for the reference lmer/glmer fit. Default NULL.

verbose

Integer scalar passed to model_setup for the reference lmer/glmer optimizer. Default 0L.

subset, na.action

Optional arguments passed through to model_setup (same meanings as in lmer / glmer). model_setup() applies them to both design extraction (lFormula) and the reference lmer/glmer/glmmTMB fits, so sampler matrices (design$y/D/W) and classical calibration use the same rows.

weights, offset

Optional arguments passed through to model_setup (same meanings as in lmer/glmer). Stored on design$weights / design$offset as length-n vectors aligned with design$y (after subset/ na.action), for Phase~1 availability to samplers.

Not fully active for mixed-model sampling yet. They affect the classical reference fit used for prior calibration. Low-level two_block_rNormal_reg already accepts and echoes prior.weights/offset2 when passed explicitly; high-level mixed routes that still hard-code unit weights / zero offset (and ICM / prior-weight algebra that assume unit weights) do not consume design$weights/design$offset yet. Prefer incorporating an offset in the formula or waiting until those paths are wired before relying on these for the Bayesian sampler.

contrasts

Optional contrast list passed to model_setup (as in lmer). Applied to both design extraction and the reference fit so factor coding in W matches fixef/vcov.

dispformula

One-sided formula selecting the Block~1 measurement- dispersion structure: ~1 (default, pooled) or ~<group_name> (matching the random-effects grouping factor exactly, requesting per-group dispersion). Passed through to model_setup, which fits and stores the glmmTMB reference (design$glmmTMB_fit) reused below as fit_ref. Gaussian models only compute the per-group group.ing_prior calibration (a within-group regression fit for every group level, used only by dGamma_list()) when dispformula is the group formula; dispformula = ~1 skips it entirely, so group.dispersion.pwt / group.dispersion.nprior must then be scalar (pooled). Mirrors the dispformula argument on lmerb()/glmerb() in lmebayes, which gates the analogous choice of sampler route; the two are independent arguments that must be kept consistent by the caller.

pop.pwt

Prior weight(s) in (0,1)(0, 1) for the population-level (Block~2, fixef) coefficients. Either a scalar (applied to every random-effect component and every Block~2 predictor), or a list with one element per random-effect component (named with the RE coefficient names in any order, or unnamed positional). Each list element is a scalar (recycled over that component's Block~2 predictors) or a vector of length pkp_k (optionally named with the predictor column names of X_hyper[[k]], reordered to match). The prior covariance for each fixef_k block is scaled relative to vcov(fit_ref) following the Prior_Setup convention: (1−pop.pwt)/pop.pwt(1-\mathrm{pop.pwt})/\mathrm{pop.pwt} for a scalar, and elementwise (1−pop.pwti)/pop.pwti (1−pop.pwtj)/pop.pwtj\sqrt{(1-\mathrm{pop.pwt}_i)/\mathrm{pop.pwt}_i}\, \sqrt{(1-\mathrm{pop.pwt}_j)/\mathrm{pop.pwt}_j} for vectors. At most one of pop.pwt (explicitly supplied), pop.nprior, and pop.sd may be supplied. When pop.pwt is NULL (the default), dimension-adaptive defaults are chosen separately for each random-effect component (see pop.pwt_default_low/ pop.pwt_default_high).

pop.pwt_default_low

Scalar in (0,1)(0, 1), default 0.01. Used when pop.pwt is NULL and a random-effect component's Block~2 predictor count pk=ncol(X_hyper[[k]])p_k = ncol(X\_hyper[[k]]) is strictly less than 14 (mirrors Prior_Setup pwt_default_low).

pop.pwt_default_high

Scalar in (0,1)(0, 1), default 0.05. Used when pop.pwt is NULL and pk≥14p_k \ge 14 for that component (mirrors Prior_Setup pwt_default_high).

pop.nprior

Optional absolute effective prior sample size(s), in group units (J=J =nlevels(group)), for the population-level (Block~2, fixef) coefficients – an alternative to pop.pwt, on the same ni=J wi/(1−wi)n_i = J\,w_i/(1-w_i) duality as pop.dispersion.pwt/pop.dispersion.nprior. Same shape as pop.pwt: a scalar, or a list with one element per random-effect component (named or unnamed positional), each element NULL (keep the pop.pwt-derived default for that component), a scalar, or a vector of length pkp_k (optionally named with the predictor column names of X_hyper[[k]], reordered to match). Converted internally to an effective weight wi=pop.npriori/(pop.npriori+J)w_i = \mathrm{pop.nprior}_i / (\mathrm{pop.nprior}_i + J), used in place of pop.pwt for that component's Sigma scaling. At most one of pop.pwt (explicitly supplied), pop.nprior, and pop.sd may be supplied. The returned pop.nprior field (see Value below) is always populated with the effective prior sample size actually implied (derived from whichever of pop.pwt/ pop.sd was used when pop.nprior is not supplied), in the same shape this argument expects – call once without pop.nprior to inspect the defaults, then feed a modified copy of ps$pop.nprior back in to override.

pop.sd

Optional prior standard deviation(s), on the coefficient scale of vcov(fit_ref), for the population-level (Block~2, fixef) coefficients – an alternative to pop.pwt. Same shape as pop.pwt: a scalar, or a list with one element per random-effect component (named or unnamed positional), each element NULL (keep the pop.pwt-derived default for that component – a partial override across components), a scalar, or a vector of length pkp_k (optionally named with the predictor column names of X_hyper[[k]], reordered to match). Converted internally to an effective weight wi=Vii/(Vii+pop.sdi2)w_i = V_{ii} / (V_{ii} + \mathrm{pop.sd}_i^2), where ViiV_{ii} is the corresponding diagonal entry of vcov(fit_ref), and used in place of pop.pwt for that component's Sigma scaling. At most one of pop.pwt (explicitly supplied), pop.nprior, and pop.sd may be supplied. The returned pop.sd field (see Value below) is always populated with the effective standard deviations actually used (diag(Σfixef,k)\sqrt{\mathrm{diag}(\Sigma_{\mathrm{fixef},k})}, derived from whichever of pop.pwt/pop.nprior was used when pop.sd is not supplied), in the same shape this argument expects – call once without pop.sd to inspect the defaults, then feed a modified copy of ps$pop.sd back in to override.

pop.dispersion.pwt

Optional relative prior weight(s) in (0,1)(0, 1) for the Block~2 dispersion (precision) prior, decoupled from pop.pwt. A scalar, or a list / numeric vector with one value per random-effect component (named or positional). Converted internally to an effective prior sample size nk=J wk/(1−wk)n_k = J\,w_k/(1-w_k) where JJ is the number of groups. At most one of pop.dispersion.pwt and pop.dispersion.nprior may be supplied; when neither is, the value is derived from pop.pwt (the mean across a component's predictors), keeping the Block~2 τk2\tau^2_k dispersion prior consistent with the coefficient prior strength. Weak values carry no computational penalty for dIndependent_Normal_Gamma sampling: the τ2\tau^2 truncation window comes from limiting-posterior quantiles independent of the prior strength (see pop.ing_prior below).

pop.dispersion.nprior

Optional absolute effective prior sample size(s) (in group units) for the Block~2 τk2\tau^2_k dispersion prior. A positive scalar, or a list / numeric vector with one value per random-effect component (named or positional). See pop.dispersion.pwt.

pop.max_disp_perc

Scalar in (0.5,1)(0.5, 1), default 0.99, or a named/positional length-prep_{\mathrm{re}} vector (one value per random-effect component). Same tail-probability role as group.max_disp_perc, but for the Block~2 τk2\tau^2_k truncation window stored in pop.ing_prior. Only operationally consumed when the sampler's dispersion pfamily is dIndependent_Normal_Gamma (ignored for dNormal).

pop.intercept_source

Character string controlling the prior mean for the global intercept hyperparameter (Intercept)::(Intercept) only. One of "null_model" (default) or "full_model". When "null_model", the prior mean is taken from a random-intercept-only reference fit y ~ 1 + (1 | group) that omits all fixed-effect predictors (analogous to Prior_Setup with intercept_source = "null_model"). When "full_model", the full-model MLE intercept is used.

pop.effects_source

Character string controlling the prior mean for all other Block~2 hyperparameters (including population-mean slopes stored as (Intercept) columns in non-intercept RE components, and any non-intercept columns in X_hyper). One of "null_effects" (default) or "full_model". When "null_effects", prior means are set to zero. When "full_model", full-model MLE values are used.

pop.mu

Optional prior mean override(s) for the population-level (Block~2, fixef) coefficients – a finer-grained alternative to pop.intercept_source/pop.effects_source; prefer those first, and reach for pop.mu only when a specific mean needs to differ from either derived default. Same shape as pop.pwt: a scalar, or a list with one element per random-effect component (named or unnamed positional), each element NULL (keep the pop.intercept_source/pop.effects_source-derived default mean for that component – a partial override across components), a scalar, or a vector of length pkp_k (optionally named with the predictor column names of X_hyper[[k]], reordered to match). Replaces mu wholesale for the components supplied; components left NULL keep their usual pop.intercept_source/pop.effects_source default. The returned pop.mu field (see Value below) is always populated with the values actually used (derived defaults when pop.mu is not supplied), in the same shape this argument expects – call once without pop.mu to inspect the defaults, then feed a modified copy of ps$pop.mu back in to override.

group.dispersion.pwt

Optional relative prior weight(s) in (0,1)(0, 1) for the Block~1 observation σ2\sigma^2 Gamma prior (Gaussian models only), decoupled from pop.pwt (Block~2 fixef) and from pop.dispersion.pwt (Block~2 τk2\tau^2_k). Either a scalar converted to nprior=w/(1−w)×nn_{\mathrm{prior}} = w/(1-w)\times n on the total observation count nn (pooled group.ing_prior), or a named or positional vector of length JJ (J=J = nlevels(groups)) with one weight per group level for per-group dGamma_list() calibration (nprior,j=wj/(1−wj)×njn_{\mathrm{prior},j} = w_j/(1-w_j)\times n_j). When group.dispersion.pwt is a vector, the pooled group.ing_prior continues to use the default w = 0.01 on total nn (unless group.dispersion.nprior is supplied explicitly). At most one of group.dispersion.pwt and group.dispersion.nprior may be supplied; when neither is, scalar group.dispersion.pwt = 0.01 is used for both pooled and per-group paths.

group.dispersion.nprior

Optional positive scalar: absolute effective prior sample size for the Block~1 σ2\sigma^2 prior (observation units, not groups). See group.dispersion.pwt.

group.alpha_target

Scalar in (0,1)(0, 1), default 0.01, or NULL to disable. Gaussian models with per-group dispformula only: target percentage of posterior beta_j draws allowed to fall outside the per-group Mahalanobis/log-concavity ellipsoid of inst/BLOCK_GIBBS_ERGODICITY_ING.md Section 16.6. This is a different quantity from group.dispersion.pwt: group.dispersion.pwt is the Gamma prior weight (an input to the prior), while group.alpha_target is a target violation rate (an output being aimed at). When non-NULL and dispformula requests per-group dispersion, Prior_Setup_GLMM() searches, for each group, for the smallest group.dispersion.pwt driving the predicted violation rate down to group.alpha_target, floored at the group.dispersion.pwt resolved from the group.dispersion.pwt/group.dispersion.nprior arguments above (so calibration only ever sharpens, never loosens, the per-group prior; supplying group.dispersion.pwt explicitly still guarantees a minimum). Active by default as a safeguard against poorly-behaved per-group priors; set to NULL to opt out and use group.dispersion.pwt/ group.dispersion.nprior directly, unmodified. Silently ignored (not an error) when left at its default on a model that does not support per-group calibration (e.g.\ dispformula = ~1); it is only an error to explicitly supply a non-NULL value in that case. See group.pwt_calibration below.

group.max_disp_perc

Scalar in (0.5,1)(0.5, 1), default 0.99, or a named/positional length-JJ vector (one value per group level, Gaussian models with per-group dispformula only). Tail probability used to compute the Block~1 σ2\sigma^2 truncation window stored in group.ing_prior – the pooled scalar default when a vector is supplied here (dispformula = ~1), or the resolved per-group value (per group, per-group dispformula). The window is the central 2×group.max_disp_perc−12 \times \mathrm{group.max\_disp\_perc} - 1 mass interval: disp_lower = (1−group.max_disp_perc)(1-\mathrm{group.max\_disp\_perc}) quantile and disp_upper = group.max_disp_perc\mathrm{group.max\_disp\_perc} quantile of the relevant Gamma (precision) distribution, inverted to the dispersion scale. Tighter values (e.g.\ 0.95) shrink the truncation window and typically improve Block~1 acceptance rates at the cost of slightly less envelope coverage; looser values (e.g.\ 0.999) widen the window. Passed to dGamma() as max_disp_perc. A per-group vector lets outlier groups be given a tighter (or looser) window than the rest; see dGamma_list().

Details

Block 1 (per-group, independent):

p(bj∣y,fixef,σ2,Σb)=N(μb,j∗,Σb,j∗)p(\mathbf{b}_j \mid \mathbf{y}, \mathrm{fixef}, \sigma^2, \Sigma_b) = \mathcal{N}(\boldsymbol{\mu}_{b,j}^*, \boldsymbol{\Sigma}_{b,j}^*)

Σb,j∗−1=Zj′Zj/σ2+diag(1/τk2)\boldsymbol{\Sigma}_{b,j}^{*-1} = \mathbf{Z}_j'\mathbf{Z}_j / \sigma^2 + \mathrm{diag}(1/\tau^2_k)

when family = gaussian(). For non-Gaussian families there is no observation-level dispersion; Block~1 uses dNormal with ddef = TRUE (see dNormal).

Block 2 (per-RE coefficient kk, independent):

p(fixefk∣bk,τk2)=N(μfixef,k∗,Σfixef,k∗)p(\mathrm{fixef}_k \mid \mathbf{b}_k, \tau^2_k) = \mathcal{N}(\boldsymbol{\mu}_{\mathrm{fixef},k}^*, \boldsymbol{\Sigma}_{\mathrm{fixef},k}^*)

Σfixef,k∗−1=Xk′Xk/τk2+Σfixef,k−1\boldsymbol{\Sigma}_{\mathrm{fixef},k}^{*-1} = \mathbf{X}_k'\mathbf{X}_k / \tau^2_k + \boldsymbol{\Sigma}_{\mathrm{fixef},k}^{-1}

Naming convention. Top-level fields use pop.* for Block~2 (population / γ\gamma, τk2\tau^2_k) and group.* for Block~1 (within-group / observation σ2\sigma^2, RE covariance Ψ\Psi). Nested entries in pop.prior_list[[k]] use glmbayes-style mu, Sigma, and dispersion (where dispersion is the plug-in RE variance τk2\tau^2_k).

Why default calibration depends on classical estimates. Prior_Setup_GLMM scales Block~2 covariances from vcov(fit_ref) by (1−pwt)/pwt(1-\mathrm{pwt})/\mathrm{pwt} and plugs in RE variances from the full reference fit, where fit_ref is the pooled lmer/glmer fit when dispformula = ~1, or an equivalent glmmTMB fit with the same dispformula when per-group dispersion is requested (see calibration_source and mer_fit above). By default the global intercept prior mean comes from a random-intercept-only null fit; all other prior means are zero (pop.effects_source = "null_effects"). This requires:

  1. Converged reference lmer/glmer fit from model_setup on the full formula (and a random-intercept-only null fit when pop.intercept_source = "null_model"). Fits with lme4 checkConv failures (e.g.\ large max|grad|) are rejected.

  2. Every X_hyper[[k]] column maps to a fixef(fit_ref) term.

  3. Each RE variance τk2\tau^2_k from the reference fit is strictly positive.

Value

Object of class "Prior_Setup_GLMM" with fields:

formula

Model formula.

family

Family object.

pop.pwt

Prior weight(s) used: the scalar as supplied, the canonical named list of per-predictor weight vectors when a list was supplied, or the dimension-adaptive default list (one weight per Block~2 predictor, per RE component) when pop.pwt was NULL.

pop.nprior

Named list with one named numeric vector per random-effect component – the effective prior sample size (group units) actually implied, ni=J wi/(1−wi)n_i = J\,w_i/(1-w_i), whether derived from pop.pwt/pop.sd (the usual case) or overridden via pop.nprior. Always present, and in the same shape the pop.nprior argument expects, so it can be inspected from a first call and fed back in (after editing) as pop.nprior to a second call.

pop.sd

Named list with one named numeric vector per random-effect component – the effective coefficient-scale prior standard deviations actually used (diag(Σfixef,k)\sqrt{\mathrm{diag}(\Sigma_{\mathrm{fixef},k})}), whether derived from pop.pwt/pop.nprior (the usual case) or overridden via pop.sd. Always present, and in the same shape the pop.sd argument expects, so it can be inspected from a first call and fed back in (after editing) as pop.sd to a second call.

pop.mu

Named list with one named numeric vector per random-effect component – the prior means actually used (mu), whether derived from pop.intercept_source/pop.effects_source (the usual case) or overridden via pop.mu. Always present, and in the same shape the pop.mu argument expects, so it can be inspected from a first call and fed back in (after editing) as pop.mu to a second call.

pop.dispersion.pwt

Named per-component vector of relative dispersion prior weights (always present; consistent with pop.dispersion.nprior via wk=nk/(nk+J)w_k = n_k/(n_k + J)).

pop.dispersion.nprior

Named per-component vector of effective prior sample sizes for the Block~2 dispersion prior (always present; used by pfamily_list() to calibrate dIndependent_Normal_Gamma components).

group.dispersion.pwt

Gaussian models only: scalar or length-JJ vector of relative prior weights for Block~1 σ2\sigma^2 – a scalar unless a per-group vector was supplied or group.alpha_target calibration was active, in which case it is the resolved length-JJ vector (see dGamma_list()).

group.dispersion.nprior

Gaussian models only: effective prior sample size for Block~1 σ2\sigma^2, on the same pooled-vs.-vector basis as group.dispersion.pwt (a scalar on the observation scale, or a named length-JJ vector of per-group nprior,jn_{\mathrm{prior},j} reflecting the group.alpha_target-calibrated group.dispersion.pwt when calibration was active; see group.pwt_calibration).

group.alpha_target

The value used, or NULL if calibration was not active.

group.pwt_calibration

NULL unless calibration was active; otherwise a per-group diagnostics data.frame (group, n_j, pwt_floor, pct_outside_before, w_star, clipped_at_ceiling, w_final, floor_binds, pct_outside_after) from the search described under group.alpha_target.

group.max_disp_perc

Gaussian models only: scalar or length-JJ vector of the tail probability used for the Block~1 σ2\sigma^2 truncation window (group.ing_prior) – the scalar as supplied, or the resolved length-JJ vector when a vector was supplied.

pop.max_disp_perc

Named length-prep_{\mathrm{re}} vector of resolved per-component values used for the Block~2 τk2\tau^2_k truncation windows in pop.ing_prior (always present).

pop.intercept_source

How the global intercept prior mean was chosen ("null_model" or "full_model").

pop.effects_source

How other Block~2 prior means were chosen ("null_effects" or "full_model").

block_formula

Within-group Block~1 formula: response regressed on the random-coefficient structure only (columns of design$groupef.names); level-2 hyper covariates are excluded. Used by dGamma_list().

group.tau_sd

Named vector sqrt(Psi) from the reference fit; shared population RE standard deviations for per-group calibration.

data

Data frame passed to Prior_Setup_GLMM() (reference for dGamma_list() diagnostics).

design

Full model_setup object (all groups), including length-n weights and offset vectors aligned with design$y.

mer_fit

Reference lmer/glmer fit on all groups (the full-formula fit from model_setup), always present regardless of dispformula (backs group.dispersion and x$lmer/x$glmer in lmerb()/glmerb()).

fit_ref

The calibration reference for Block~2 (fixef/τk2\tau^2_k) and the per-group Block~1 inputs (group.tau_sd, BLUP coefficients): identical to mer_fit when dispformula = ~1; otherwise an equivalent glmmTMB fit with the same dispformula (Gaussian models only). See calibration_source.

group.dispersion.fit

NULL when dispformula = ~1; otherwise the same glmmTMB object as fit_ref.

group.dispersion.ref

NULL when dispformula = ~1; otherwise a named length-JJ vector of per-group observation-level dispersion read from fit_ref's dispersion linear predictor (glmmTMB::predict(fit_ref, type = "disp"), aggregated by group). Diagnostic only – not the value fed to the sampler; compare against group.ing_prior's sigma2_hat.

calibration_source

"lme4" or "glmmTMB"; which reference fit produced fit_ref (and therefore pop.prior_list, pop.ing_prior, group.tau_sd, and group.ing_prior).

group.dispersion

For Gaussian models: resolved Block~1 observation σ2\sigma^2 used to mean-match the Gamma prior. When dispformula = ~1, the pooled A12 §3.3.4 aggregate σ^2=(∑jSmarg,j)/(n−Jpre)\hat\sigma^2 = (\sum_j S_{\mathrm{marg},j})/(n - J p_{\mathrm{re}}) (Part VI Omega_j included); classical lmer residual in attr(., "classical"). When dispformula is per-group, a named length-JJ vector of per-group group.ing_prior[[j]]$sigma2_hat. attr(., "source") records provenance. NULL for non-Gaussian families. Pass to rlmerb()/rglmerb() as group.dispersion for fixed-σ2\sigma^2 routes.

group.Sigma

Diagonal RE covariance matrix (Block~1), classical reference-fit Ψ\Psi diagonals.

pop.dispersion.ref

Named vector of classical reference-fit τ^k2\hat\tau^2_k (VarCorr / Psi) before the A12 §3.3.4 hyper-regression remargin; compare to pop.prior_list[[k]]$dispersion.

pop.prior_list

Named Block~2 prior list per RE coefficient. For Gaussian models dispersion is the A12 §3.3.4 marginal hyper-regression center for τk2\tau^2_k (not the raw VarCorr plug-in; see pop.dispersion.ref).

pop.ing_prior

Named per-component list of the prospective dIndependent_Normal_Gamma calibration: Gamma precision-prior shape =(n0+1+pk)/2= (n_0 + 1 + p_k)/2 and rate =τ^k2(n0+pk−1)/2= \hat\tau^2_k (n_0 + p_k - 1)/2 (the lmebayesCore default calibration with n0=n_0 = pop.dispersion.nprior; since rate =τ^k2(shape−1)= \hat\tau^2_k (\code{shape} - 1), the implied inverse-Gamma prior on τk2\tau^2_k has mean exactly τ^k2\hat\tau^2_k), where τ^k2\hat\tau^2_k is the A12 §3.3.4 marginal hyper-regression center in pop.prior_list[[k]]$dispersion, and the default τk2\tau^2_k truncation window disp_lower / disp_upper: the (1−pop.max_disp_perc)(1-\mathrm{pop.max\_disp\_perc}) / pop.max_disp_perc\mathrm{pop.max\_disp\_perc} quantiles of the limiting posterior Γ((J+1)/2,  τ^k2(J−1)/2)\Gamma((J+1)/2,\; \hat\tau^2_k (J-1)/2) – the weak-prior (n0→0n_0 \to 0) limit of the Block~2 posterior Gamma for the precision (lmebayesCore Chapter A12, Theorem 2; inverted to a τ2\tau^2 interval). This window is identical for all n0n_0, covers ≥\ge 2×pop.max_disp_perc−12 \times \mathrm{pop.max\_disp\_perc} - 1 of the exact posterior for every prior strength, and keeps the envelope sampler's cost stable as priors weaken; see inst/ING_TRUNCATION_WINDOW.md. Used by pfamily_list() when ptypes = "dIndependent_Normal_Gamma"; ignored for dNormal priors.

group.ing_prior

Gaussian models only; NULL for non-Gaussian models. Shape depends on dispformula:

dispformula = ~1 (pooled)

A single list: prospective dGamma() group.dispersion calibration for Block~1 ING (observation σ2\sigma^2 shared across all group levels) – mean-matched shape / rate with nprior=group.dispersion.pwt/(1−group.dispersion.pwt)×nn_{\mathrm{prior}} = \mathrm{group.dispersion.pwt}/(1-\mathrm{group.dispersion.pwt})\times n, p=prep = p_{\mathrm{re}}, and σ^2\hat\sigma^2 = group.dispersion (pooled A12 §3.3.4 aggregate of per-group Smarg,jS_{\mathrm{marg},j} by default; same ING algebra as pop.ing_prior for τk2\tau^2_k; requires group.dispersion.pwt ≤0.5\le 0.5), plus disp_lower / disp_upper as the central 2×group.max_disp_perc−12 \times \mathrm{group.max\_disp\_perc} - 1 prior-mass interval from the same calibrated shape/rate (always the pooled/scalar group.max_disp_perc value, even when a per-group vector was supplied). Pass these fields to dGamma().

dispformula requesting per-group dispersion

A named list (one entry per group level) of per-group dGamma() density calibration (sigma2_hat, shape_ING, rate, rate_gamma, n_prior, n_j, n_combined, disp_lower, disp_upper, max_disp_perc, omega_j, ...). shape_ING/rate already fold in the Part VI model-derived Omega_j (fixed-effect/gamma uncertainty about b_j's prior mean, propagated through group j's hyper-design row and pop.prior_list[[k]]$Sigma), so this marginal integrates out both random effects (b_j) and fixed effects (gamma); see inst/DGAMMA_LIST_MARGINAL_AND_BOUNDS.md Part VI. disp_lower/disp_upper are (1−wj)(1-w_j)/wjw_j quantiles of Gamma(shape_ING + n_j/2, rate_post), where rate_post is sigma2_hat mean-matched at that same inflated shape (nj/2n_j/2 added only for the window, so it reflects the posterior spread the sampler's own envelope machinery actually draws σj2\sigma^2_j from – see EnvelopeDispersionBuild.cpp's shape2 = Shape + n_w/2 fallback – while shape_ING/rate themselves, the values actually fed to the sampler as the Gamma prior, are unchanged); wjw_j is this group's own resolved group.max_disp_perc value (stored per-group as max_disp_perc). Used directly by dGamma_list().

Which shape is returned is fully determined by dispformula; see dGamma_list() for how consumers tell the two apart.

dispformula

The dispformula supplied.

x invisibly.

See Also

model_setup, Prior_Setup, build_mu_all, pfamily_list, dGamma_list


Prior setup for row-block regressions

Description

Runs Prior_Setup on each block subset of the data. Typical callers are rNormal_reg_group / rNormalGLM_reg_group or the lmbBlock / glmbBlock wrappers in lmebayes.

Usage

Prior_SetupGroup(
  formula,
  group,
  family = gaussian(),
  data = NULL,
  weights = NULL,
  subset = NULL,
  na.action = na.fail,
  offset = NULL,
  contrasts = NULL,
  pwt = NULL,
  pwt_default_low = 0.01,
  pwt_default_high = 0.05,
  n_prior = NULL,
  sd = NULL,
  dispersion = NULL,
  intercept_source = c("null_model", "full_model"),
  effects_source = c("null_effects", "full_model"),
  mu = NULL,
  k = 1,
  ...
)

Arguments

formula

A formula with a single response.

group

Group partition: factor or vector of length nrow(data) (after model.frame), a column name in data, l2_blocks counts, or a list of row index vectors (see normalize_group).

family

a description of the error distribution and link function to be used in the model.

data

an optional data frame, list or environment (or object coercible by as.data.frame to a data frame) containing the variables in the model. If not found in data, the variables are taken from environment(formula), typically the environment from which glm is called.

weights

an optional vector of ‘prior weights’ to be used in the fitting process. Should be NULL or a numeric vector.

subset

an optional vector specifying a subset of observations to be used in the fitting process. (See additional details about how this argument interacts with data-dependent bases in the ‘Details’ below.)

na.action

how NAs are treated. The default is first, any na.action attribute of data, second a na.action setting of options, and third na.fail if that is unset. The factory-fresh default is na.omit. Another possible value is NULL.

offset

this can be used to specify an a priori known component to be included in the linear predictor during fitting. This should be NULL or a numeric vector of length equal to the number of cases. One or more offset terms can be included in the formula instead or as well, and if more than one is specified their sum is used. See model.offset.

contrasts

an optional list. See the contrasts.arg of model.matrix.default.

pwt, n_prior, sd, mu, dispersion, k

Each of these six calibration arguments (passed to Prior_Setup for every block) may be supplied in either of two forms:

  • the ordinary shared form documented in Prior_Setup (a scalar, or a per-coefficient vector for pwt/sd/mu) – applied identically to every block (default); or

  • a named list with one element per block, keyed by the block/group level IDs (names(Prior_SetupGroup(...)), i.e. normalize_group()$ids) – each element supplies that block's own value, in the same shape Prior_Setup() would otherwise accept. names(x) must exactly match the block IDs. An element may be NULL, meaning use Prior_Setup()'s default for that argument in that block.

The two forms may be mixed freely across the six arguments in one call.

pwt_default_low

Default prior weight used when pwt is not supplied and the model dimension is below 14. Defaults to 0.01.

pwt_default_high

Default prior weight used when pwt is not supplied and the model dimension is 14 or greater. Defaults to 0.05.

intercept_source

Specifies the method through which the prior mean for the intercept term is set. Options are based on the null intercept only model (null_model) or full_models. The default is the null model which is safer if variables are not centered.

effects_source

Specifies the method through which the prior means for the effects terms are set. Options are null_effects (prior means set to zero) or full_model (effect means set to match maximum likelihood estimates).

...

Not used.

Details

What it produces

Prior_SetupGroup() calls Prior_Setup once per block, on that block's rows only, and returns the results as a named list keyed by block ID.

Contrast with hierarchical mixed-model setup

Prior_Setup_GLMM calibrates a hierarchical prior (one reference lmer/glmer fit, Block~2 hyperpriors, cross-group shrinkage). Prior_SetupGroup() calibrates independent priors per row block with no shared γ\gamma or τ2\tau^2; row-block engines draw blocks iid.

Names for per-block calibration lists must match normalize_group block IDs exactly.

Prior families

After calibration, convert to sampler-ready pfamily objects with pfamily_list(x, ptypes = ...) (see Prior families via pfamily_list() below for ptypes and allowed families).

Value

A named list of class "Prior_SetupGroup". Each element is a Prior_Setup result for one block.

x invisibly.

An object of class "pfamily_list": a named list of pfamily objects with attribute "ptypes".

See Also

Prior_Setup, multi_prior_setup, normalize_group, pfamily_list, rNormal_reg_group, rNormalGLM_reg_group, Prior_Setup_GLMM


The Bayesian Generalized Linear Mixed-Effects Model Distribution

Description

rglmerb draws from the posterior of a Bayesian generalized linear mixed model — Poisson, binomial, Gamma, or Gaussian — with a single grouping factor and uncorrelated random effects, given a design from model_setup and priors for the population level (pfamily_list) and, for Gaussian responses only, the residual variance (dispprior_list).

Usage

rglmerb(
  n,
  design,
  pfamily_list,
  family = poisson(),
  dispprior_list = NULL,
  offset = NULL,
  weights = 1,
  gap_tol = 0.0196,
  tv_tol = 0.01,
  mode_gap_max = 1,
  collect_block1 = TRUE,
  verbose = TRUE,
  progbar = FALSE,
  sim_method = "DEFAULT",
  simulate = TRUE
)

Arguments

n

Integer. Number of independent chains in the main stage. Required when simulate = TRUE; ignored when simulate = FALSE.

design

A model_setup object (from model_setup).

pfamily_list

Named list of Block~2 pfamily objects, one per random-effect coefficient in design$groupef.names (same as rGLMM_reg / rLMMNormal_reg).

family

A family object. Default poisson().

dispprior_list

Block~1 observation-dispersion prior, in the same shapes as the rLMM* / rGLMM* engines. Required for family = gaussian() (list(dispersion = ) or dGamma() / dGamma_list()); must be NULL (default) for poisson() and binomial().

offset, weights

Observation offset and prior weights, as in rGLMM_reg / rLMMNormal_reg (offset = NULL, weights = 1). When omitted, inherit design$offset / design$weights if present. Explicit values always override the design. Echoed on the fit; not yet used in Gibbs sweeps.

gap_tol

Legacy mode–mean gap for deriving the pilot chain count when tv_tol is NULL. Ignored for Gaussian without ING population components. Ignored when simulate = FALSE.

tv_tol

Total variation tolerance for convergence calibration. Inner Gibbs sweeps and pilot chain counts are derived internally. Ignored when simulate = FALSE.

mode_gap_max

Pilot inner-sweep calibration (non-Gaussian and Gaussian+ING only). Ignored when simulate = FALSE.

collect_block1

Collect groupef draws from main chains (non-Gaussian only). Ignored when simulate = FALSE.

verbose

Print stage headers and diagnostics.

progbar

Progress bars when verbose is FALSE. Ignored when simulate = FALSE.

sim_method

Simulation method for family = gaussian(): "DEFAULT" or "TWO_BLOCK_GIBBS"; see rlmerb. Ignored (two-block Gibbs only) for non-Gaussian families. Ignored when simulate = FALSE.

simulate

Logical (default TRUE). When TRUE, draw posterior samples. When FALSE, return point estimates only via rglmerb_point (no MCMC).

Details

The returned sample is n mutually independent draws, one per replicate chain, taken at that chain's final sweep. It is analyzed like an iid sample rather than like an MCMC trace.

This is the matrix-level interface; for a formula interface use glmerb() in lmebayes. rlmerb is the Gaussian-only counterpart.

The model being estimated

rglmerb estimates the same two-stage structure as rlmerb, with a link function between the linear predictor and the response mean. For group jj with link gg:

yj∣βj∼family(g−1(Djβj))(within group)y_j \mid \beta_j \sim \mathrm{family}\big(g^{-1}(D_j \beta_j)\big) \qquad \textrm{(within group)}

βj∣γ,Ψ∼N(Wjγ,  Ψ)(across groups)\beta_j \mid \gamma, \Psi \sim N(\mathcal{W}_j \gamma,\; \Psi) \qquad \textrm{(across groups)}

The across-group stage is unchanged and remains Gaussian: group coefficients scatter around a population expectation Wjγ\mathcal{W}_j\gamma with diagonal covariance Ψ=diag(τk2)\Psi = \mathrm{diag}(\tau^2_k). What changes is the within-group stage and, with it, the interpretation of βj\beta_j, which now lives on the link scale — log rates for poisson(), log-odds for binomial(). Summaries that are meaningful on the response scale need the inverse link applied to the draws.

Supported families are gaussian(), poisson(), binomial(), and Gamma() with their standard links. The restriction is log-concavity of the likelihood, which the sampler requires (Nygren and Nygren 2006).

What you supply

design

A model_setup object built with the matching family. For non-Gaussian responses its estimability report matters more than usual: a group with no variation in the response (all-zero counts, a single binomial outcome level) has no finite maximum likelihood estimate, and is reported separately from the rank check. See vignette("Chapter-B08", package = "lmebayesCore").

pfamily_list

The population-level prior, one component per random-effect coefficient, declaring whether each between-group variance τk2\tau^2_k is known (dNormal) or estimated (dIndependent_Normal_Gamma).

dispprior_list

Only meaningful for gaussian(), where it gives the residual variance. poisson() and binomial() have no free dispersion parameter, so it must be NULL.

What the returned draws are

As in rlmerb, rglmerb runs n independent replicate chains and stores one draw from each, taken at the final sweep, so the n draws are mutually independent and ordinary Monte Carlo standard errors apply. There is nothing to thin and no autocorrelation to diagnose.

The strength of the accompanying guarantee is weaker here than in the Gaussian case, and the difference is worth understanding rather than glossing over. The calibration behind m_convergence is derived for Gaussian targets. A Poisson or binomial posterior is not Gaussian, so the engine applies the same calibration at curvature evaluated near the posterior mode. The sweep count is therefore a well-motivated budget rather than a certificate, and tv_tol should be read in that spirit. Only family = gaussian() with fixed σ2\sigma^2 and all-dNormal components yields exact draws.

A second difference follows from the same fact. Because the posterior mode is not the posterior mean when the target is skewed, chains started at the mode begin at an unknown distance from the target. rglmerb therefore runs a short pilot stage first and uses its cross-chain mean as the starting point for the main chains, which shortens the main stage. Pilot draws are diagnostic only and are discarded — they are never part of the returned sample. vignette("Chapter-B03", package = "lmebayesCore") covers the trade-off between pilot size and main-stage length.

Point estimates without sampling

simulate = FALSE returns the joint posterior mode by iterated conditional modes (or the exact mean for gaussian()), at fixed variance-component plug-ins. No sweeps run and no RNG is consumed, so the result is deterministic — useful while iterating on model specification. All draw, pilot, and convergence slots are NULL.

Value

An object of class c("rglmerb", "list"), with the same layout as rlmerb. Each draw slot holds one row per replicate chain, taken at that chain's final sweep.

popef

Named list of population draws γ\gamma, one n ×qk\times q_k matrix per random-effect coefficient, on the link scale. popef.means and popef.mode are the draw mean and the posterior mode.

groupef

Long-format data frame of group coefficients βj\beta_j (n ×J\times J rows: draw, the grouping factor, then one column per coefficient), also on the link scale. groupef.mode is the J×preJ \times p_{re} mode matrix. Collected only when collect_block1 = TRUE.

popef.dispersion

Draws of the between-group variances τk2\tau^2_k; constant for dNormal components, varying for estimated ones. popef.dispersion.mean is the column mean.

group.dispersion

Residual variance, Gaussian responses only; absent for poisson() and binomial().

m_convergence, convergence_info

Sweeps run per chain and the calibration behind that count (method, lambda_star, draw_engine, sim_method_used, icm_info).

pilot

Present when a pilot stage ran, holding its size n, the chisq calibration constant, and its draws. Diagnostic only — these are not posterior draws.

Inputs are echoed back in design, family, n, call, pfamily_list, dispprior_list, prior.weights and offset, alongside the packed prior and thin Prior summaries.

When simulate = FALSE, every draw, pilot and convergence slot is NULL and only the mode fields are populated.

References

Nygren K~N, Nygren L~M (2006). “Likelihood Subgradient Densities.” Journal of the American Statistical Association, 101(475), 1144–1156. doi:10.1198/016214506000000357.

See Also

rlmerb, rglmerb_point, rLMMNormal_reg_known_vcov, rLMMNormal_reg_estimated_vcov, rLMMindepNormalGamma_reg_known_vcov, rLMMindepNormalGamma_reg_estimated_vcov, rGLMM_reg, Prior_Setup_GLMM

Examples

## rglmerb() — matrix-level GLMM sampler (binomial) on book_banning
##
## Probability that a challenged book is removed, with a random intercept and
## a `violent` slope by state.  Restricted to the eight largest states so the
## example stays quick under R CMD check.  Requires suggested package
## bayesrules.  For the formula interface see lmebayes::glmerb().

if (requireNamespace("bayesrules", quietly = TRUE)) {
  data(book_banning, package = "bayesrules")
  dat <- book_banning[, c("state", "removed", "violent")]
  dat <- dat[stats::complete.cases(dat), ]
  dat$removed_i <- as.integer(dat$removed == 1L | dat$removed == "1")
  dat$violent_i <- as.integer(
    dat$violent == TRUE | dat$violent == 1L | dat$violent == "TRUE"
  )
  dat$state <- factor(dat$state)

  keep <- names(sort(table(dat$state), decreasing = TRUE))[seq_len(8L)]
  dat <- droplevels(subset(dat, state %in% keep))

  form <- removed_i ~ violent_i + (1 + violent_i || state)

  design <- model_setup(form, data = dat, family = binomial())
  ps <- Prior_Setup_GLMM(form, data = dat, family = binomial(), pop.pwt = 0.01)

  ## Point estimates only: joint posterior mode by ICM, no sampling.
  pt <- rglmerb(
    design       = design,
    pfamily_list = pfamily_list(ps),
    family       = binomial(),
    simulate     = FALSE,
    verbose      = FALSE
  )
  pt$popef.mode

  ## Draws.  Non-Gaussian families always run a pilot stage before the main
  ## chains, and dispprior_list must stay NULL for binomial().
  set.seed(1)
  fit <- rglmerb(
    n            = 10L,
    design       = design,
    pfamily_list = pfamily_list(ps),
    family       = binomial(),
    verbose      = FALSE,
    progbar      = FALSE
  )

  print_groupef(fit, draws = 1:3, groups = 1:3)
}

Simulation Functions for Generalized Linear Mixed Models

Description

Posterior draws for Bayesian generalized linear mixed models, given design matrices y, D, W and population priors pfamily_list. The model is the two-stage structure documented in rLMM_reg with a link function applied within groups; see rglmerb for the model statement and for how to read the returned draws.

These are the engine-level entry points, normally reached through rglmerb (or glmerb() in lmebayes). Gaussian responses use the rLMM_reg engines instead. Each returned draw is one replicate chain's state at its final sweep.

Usage

## S3 method for class 'rGLMM_reg'
print(
  x,
  digits = max(3, getOption("digits") - 3),
  components = NULL,
  draws = NULL,
  ...
)

rGLMM_reg_known_vcov(
  n,
  y,
  D,
  group,
  W,
  pfamily_list,
  dispprior_list,
  icm_tol = 1e-10,
  icm_maxit = 200L,
  offset = NULL,
  weights = 1,
  family = gaussian(),
  gap_tol = 0.0196,
  tv_tol = 0.01,
  mode_gap_max = 1,
  Gridtype = 2,
  n_envopt = NULL,
  use_parallel = TRUE,
  use_opencl = FALSE,
  verbose = FALSE,
  progbar = FALSE,
  stage_verbose = FALSE,
  rate_calibration = NULL,
  collect_block1 = TRUE
)

rGLMM_reg_estimated_vcov(
  n,
  y,
  D,
  group,
  W,
  pfamily_list,
  dispprior_list,
  icm_tol = 1e-10,
  icm_maxit = 200L,
  offset = NULL,
  weights = 1,
  family = gaussian(),
  gap_tol = 0.0196,
  tv_tol = 0.01,
  mode_gap_max = 1,
  Gridtype = 2,
  n_envopt = NULL,
  use_parallel = TRUE,
  use_opencl = FALSE,
  verbose = FALSE,
  progbar = FALSE,
  stage_verbose = FALSE,
  rate_calibration = NULL,
  collect_block1 = TRUE
)

rGLMM_reg(
  n,
  y,
  D,
  group,
  W,
  pfamily_list,
  dispprior_list,
  icm_tol = 1e-10,
  icm_maxit = 200L,
  offset = NULL,
  weights = 1,
  family = gaussian(),
  gap_tol = 0.0196,
  tv_tol = 0.01,
  mode_gap_max = 1,
  Gridtype = 2,
  n_envopt = NULL,
  use_parallel = TRUE,
  use_opencl = FALSE,
  verbose = FALSE,
  progbar = FALSE,
  stage_verbose = FALSE,
  rate_calibration = NULL,
  collect_block1 = TRUE
)

Arguments

x

For print methods, a simfunc return object.

digits

Number of significant digits for printed numeric values.

components

For print methods: NULL (default; print all population components in names(x$popef)), a character vector of component names (e.g. "(Intercept)", slopes), or integer indices.

draws

For print methods and print_groupef: NULL (default; all draws) or integer indices / logical of length n, as in rnorm(n)[1:10]. Only the printed rows are subset; the object is unchanged. Example: print(x, draws = 1:10).

...

further arguments passed to or from other methods.

n

Number of stored main-stage draws. If length(n) > 1, the length is used.

y

Response vector of length l2 (= nrow(D)).

D

Level-1 design matrix (l2 x p_re). Must have unique, non-empty colnames(D): these are the random-effect coefficient names used to key W and pfamily_list (there is no separate groupef.names argument to override them).

group

Grouping factor of length l2 (must be a factor; levels(group) fixes the row order of groupef draws – there is no separate group_levels argument. To use a level order/superset not present in the observed data, construct group as factor(observed_group, levels = full_superset) yourself. The name used for the grouping column in groupef (group_name) is resolved from attr(group, "group_name") if set, otherwise from group's own variable name via substitute() – this only works when group is passed as a bare variable (e.g. group = school_id); otherwise attach the name yourself via attr(group, "group_name") <- "school_id".

W

Named list of group-level design matrices (J x q_k), one per column of D.

pfamily_list

Named list of population pfamily objects. One τk2\tau^2_k plug-in per component (fixed dispersion for dNormal, prior mean rate/(shape−1)rate/(shape - 1) for dIndependent_Normal_Gamma) is assembled into the diagonal group random-effect prior precision.

dispprior_list

Observation-dispersion prior container: dispersion (required for gaussian()), optional ddef. For non-Gaussian families this is often an empty list. The group random-effect prior precision (formerly a separate P/Sigma field) is always derived internally from pfamily_list; dispprior_list must not contain P or Sigma.

icm_tol, icm_maxit

ICM convergence controls for the internal population start.

offset, weights

Observation offset and prior weights (glmbayes-style formals: offset = NULL, weights = 1). Normalized to length length(y) and echoed on the return as offset/ offset2/prior.weights. Not yet used by the mixed-model sampling path (ICM / sweeps still assume unit weights and zero offset).

family

Likelihood family (length-l2 recycling rules follow the group-level likelihood).

gap_tol

Legacy mode–mean gap for pilot chain count when tv_tol is NULL.

tv_tol

Total-variation tolerance for Theorem~3 calibration. n_pilot, m_convergence_pilot, and main-stage m_convergence are derived internally.

mode_gap_max

Pilot inner-sweep calibration when a pilot stage runs.

Gridtype, n_envopt, use_parallel, use_opencl

Reserved (not yet forwarded).

verbose

Print stage headers and convergence calibration lines.

progbar

Progress bars during sampling.

stage_verbose

Print pilot chi-squared and post-pilot UB diagnostics.

rate_calibration

Optional rate object for stage_verbose.

collect_block1

Collect groupef draws from main chains.

Value

An object of class c("<route>", "rGLMM_reg", "list"), where <route> is "rGLMM_reg_known_vcov" or "rGLMM_reg_estimated_vcov". Components use package group/population names (see ‘inst/notation.md’), in glm/glmbayes-style order:

groupef

Draws of non-centered group coefficients βj\beta_j (when collect_block1 is TRUE): grouping column plus one column per colnames(D).

groupef.mode

J×preJ \times p_{re} matrix of ICM group coefficients β^j\hat\beta_j. Not lme4's mean-zero uju_j.

groupef.iters

Optional group-level envelope iteration counts.

popef

Named list of n x q_k matrices of population coefficient draws γk\gamma_k.

popef.mode, popef.init

ICM population point estimates and main-stage starts.

popef.dispersion, popef.iters

Optional population per-draw diagnostics when produced by the sampler.

pfamily_list, dispprior_list

Population and observation-dispersion priors that were used.

prior.weights, offset, offset2

Normalized copies of the weights/offset arguments (glmbayes naming). Not yet consumed by sampling.

any_non_normal

Whether any population component is not dNormal.

family, design, n

Likelihood family, matrix inputs (including echoed weights/offset), and chain count.

call

Matched call.

m_convergence

Inner Gibbs sweeps per stored main-stage draw.

convergence_info

Theorem~3 / UB calibration details, including draw_engine and icm_info.

pilot

When a pilot ran: list with n, m_convergence, chisq, and draws; otherwise NULL.

sweep_history

Main-stage sweep history when collected.

Functions

  • rGLMM_reg_known_vcov(): All population components dNormal (known τk2\tau^2_k). Non-Gaussian: pilot always (unless n_pilot = 0L); standard fixed-dispersion eigenvalue rate bounds (no ING disp_lower path).

  • rGLMM_reg_estimated_vcov(): ING population components (estimated τk2\tau^2_k). Non-Gaussian: pilot always (unless n_pilot = 0L); conservative disp_lower eigenvalue bounds and pilot-updated τ2\tau^2 starts.

  • rGLMM_reg(): Route by population pfamily_list to known or estimated τ2\tau^2 simulation routes.

What the prior choices mean

Callers never name a route. Given a non-Gaussian family, the only remaining modelling decision is whether the between-group variances τk2\tau^2_k are known or estimated:

τk2\tau^2_k Engine
all known (every component dNormal) rGLMM_reg_known_vcov
any estimated (an dIndependent_Normal_Gamma component) rGLMM_reg_estimated_vcov

A gaussian() family is handled by the rLMM_reg engines instead.

Because the observation-level dispersion is fixed by the family for poisson() and binomial(), there is no residual-variance decision to make: the second axis present in rLMM_reg collapses.

How this differs from the Gaussian case

Three properties hold for every GLMM route and are worth knowing before interpreting the output:

No exact draws

The posterior is never jointly Gaussian, so there is no closed form to sample from. Draws always come from the final sweep of a calibrated chain.

popef.mode is a mode, not a mean

With a skewed posterior the two differ, so the reported mode should not be read as a posterior mean the way it can be on the exact Gaussian route.

The sweep count is a budget, not a certificate

The calibration is derived for Gaussian targets and is applied here at local curvature near the mode. It is a well-motivated approximation; convergence_info$method records which flavour was used.

The same fact — mode ≠\neq mean — is why a short pilot stage always runs first: its cross-chain average provides a starting point closer to the target than the mode is, which shortens the main stage. Pilot draws are discarded and are never part of the returned sample. See vignette("Chapter-B03", package = "lmebayesCore") and vignette("Chapter-B06", package = "lmebayesCore").

See Also

rGLMM_sweep, rLMM_reg, rglmerb, print_groupef

glmbayes Simulation Functions rGLMM_Re_Draw(), rGLMM_sweep(), rLMMNormal_joint_iid(), rLMM_reg, two_block_optimize_pilot_cost(), two_block_rNormal_reg(), two_block_rate(), two_block_tv_bound()

Examples

## rGLMM_reg_known_vcov() — binomial, dNormal population priors on book_banning
##
## Same data / formula path as lmebayes demo
## demo("Ex_16_glmerb_book_banning", package = "lmebayes"), restricted to the
## eight largest states so the example stays quick under R CMD check.
## Requires suggested package bayesrules.

if (requireNamespace("bayesrules", quietly = TRUE)) {
  data(book_banning, package = "bayesrules")
  dat <- book_banning[, c("state", "removed", "violent")]
  dat <- dat[stats::complete.cases(dat), ]
  dat$removed_i <- as.integer(dat$removed == 1L | dat$removed == "1")
  dat$violent_i <- as.integer(
    dat$violent == TRUE | dat$violent == 1L | dat$violent == "TRUE"
  )
  dat$state <- factor(dat$state)

  ## Keep the largest states (avoids singular glmer fits on tiny subsets).
  keep <- names(sort(table(dat$state), decreasing = TRUE))[seq_len(8L)]
  dat <- droplevels(subset(dat, state %in% keep))

  form <- removed_i ~ violent_i + (1 + violent_i || state)

  design <- model_setup(form, data = dat, family = binomial())
  ps <- Prior_Setup_GLMM(form, data = dat, family = binomial(), pop.pwt = 0.01)
  pf <- pfamily_list(ps)

   design$y
  
  set.seed(1)
  fit <- rGLMM_reg_known_vcov(
    n = 10L,
    y = design$y,
    D = design$D,
    group = design$group,
    W = design$W,
    pfamily_list = pf,
    dispprior_list = list(),
    family = binomial(),
    progbar = FALSE,
    verbose = FALSE
  )

  print(fit, draws = 1:5)
  print_groupef(fit, draws = 1:5, groups = 1:3)
}

The Bayesian Linear Mixed-Effects Model Distribution

Description

rlmerb draws from the posterior of a Bayesian linear mixed model with a single grouping factor and uncorrelated random effects, given a design from model_setup and priors for the population level (pfamily_list) and the residual variance (dispprior_list).

Usage

rlmerb(
  n,
  design,
  pfamily_list,
  dispprior_list,
  offset = NULL,
  weights = 1,
  tv_tol = 0.01,
  progbar = TRUE,
  verbose = TRUE,
  print_icm_table = TRUE,
  gap_tol = 0.0196,
  mode_gap_max = 1,
  diag_sweeps = FALSE,
  sim_method = "DEFAULT",
  simulate = TRUE
)

Arguments

n

Integer. Number of stored draws (each draw is one full pass through m_convergence inner Gibbs sweeps). Required when simulate = TRUE; ignored when simulate = FALSE.

design

A model_setup object (from model_setup) supplying y, D, group, W, group_name, and groupef.names.

pfamily_list

Named list of Block~2 pfamily objects, one per random-effect coefficient in design$groupef.names (same as rLMMNormal_reg).

dispprior_list

Block~1 observation-dispersion prior, in the same shapes accepted by the rLMM* engines: list(dispersion = ) for fixed σ2\sigma^2 (scalar or per-group vector); a dGamma() pfamily (pooled ING); or a named list of dGamma() pfamilies (per-group ING). A bare positive numeric scalar/vector is also accepted (treated as fixed dispersion).

offset, weights

Observation offset and prior weights, as in rLMMNormal_reg (offset = NULL, weights = 1). When omitted, inherit design$offset / design$weights from model_setup if present. Explicit values always override the design. Echoed on the fit as prior.weights / offset / offset2; not yet used in Gibbs sweeps.

tv_tol

Single numeric in (0, 1). Total variation tolerance used for convergence calibration. Default 0.01. Inner Gibbs sweeps per stored draw are derived from Theorem~3. Ignored when simulate = FALSE.

progbar

Logical. Show a text progress bar during sampling. Default TRUE. Ignored when simulate = FALSE.

verbose

Logical. Print the reference-vs-ICM table and the convergence calibration line. Default TRUE.

print_icm_table

Logical. When FALSE, skip the reference-vs-ICM table. The convergence calibration line from the Core sampler still follows verbose. Default TRUE.

gap_tol

Legacy mode–mean gap tolerance for the pilot stage when any population component uses dIndependent_Normal_Gamma and tv_tol is NULL. Ignored for all-dNormal models. Ignored when simulate = FALSE.

mode_gap_max

Pilot inner-sweep calibration for ING population models (default 1.0). Ignored for all-dNormal models. Ignored when simulate = FALSE.

diag_sweeps

Diagnostic flag for ING population models with a pilot stage. When TRUE, auto-print one combined population chain-mean table per stage when each stage finishes; sweep_history is always stored on the fit. Default FALSE. Ignored when simulate = FALSE.

sim_method

Simulation method: "DEFAULT" or "TWO_BLOCK_GIBBS". Only affects the fixed-dispersion, known-variance-components route (scalar or per-group fixed dispprior_list, all population dNormal()): "DEFAULT" draws directly from the exact multivariate-normal posterior via rLMMNormal_reg_known_vcov_iid (no Gibbs sweeps, no burn-in); "TWO_BLOCK_GIBBS" forces two-block Gibbs sampling (rLMMNormal_reg_known_vcov_two_bg) instead. Every other route (dGamma()/dIndependent_Normal_Gamma components, variance components estimated) only supports two-block Gibbs, so both values behave identically there. Ignored when simulate = FALSE.

simulate

Logical (default TRUE). When TRUE, draw posterior samples. When FALSE, return point estimates only via rlmerb_point (no MCMC).

Details

The returned sample is n mutually independent draws — one per replicate chain, taken at that chain's final sweep, where the number of sweeps is calibrated in advance so the final sweep is within tv_tol of the target posterior. It is therefore analyzed like an iid sample rather than like an MCMC trace.

This is the matrix-level interface. For a formula interface, use lmerb() in lmebayes. The Poisson/binomial/Gamma counterpart is rglmerb, and the non-hierarchical analogue is rlmb.

The model being estimated

rlmerb estimates the two-stage Gaussian mixed model in a centered (Lindley–Smith) parameterization (Lindley and Smith 1972). For group j=1,…,Jj = 1, \dots, J:

yj∣βj,σj2∼N(Djβj,  σj2Inj)(within group)y_j \mid \beta_j, \sigma^2_j \sim N(D_j \beta_j,\; \sigma^2_j I_{n_j}) \qquad \textrm{(within group)}

βj∣γ,Ψ∼N(Wjγ,  Ψ)(across groups)\beta_j \mid \gamma, \Psi \sim N(\mathcal{W}_j \gamma,\; \Psi) \qquad \textrm{(across groups)}

Read the second line as the model for how groups differ: each group's own coefficient vector βj\beta_j is drawn around a population expectation Wjγ\mathcal{W}_j\gamma, with Ψ\Psi measuring how far groups scatter from it. The estimand is therefore the pair (γ,Ψ)(\gamma, \Psi) at the population level together with all JJ group vectors βj\beta_j, and σ2\sigma^2 for the residual scale.

What distinguishes this from the lme4 presentation is that the full coefficient βj\beta_j appears in the likelihood, not a mean-zero deviation. Marginalizing recovers the familiar form with Xj=DjWjX_j = D_j\mathcal{W}_j, Zj=DjZ_j = D_j, and bj=βj−Wjγb_j = \beta_j - \mathcal{W}_j\gamma, so γ\gamma corresponds to lme4's fixed effects and bjb_j to its random effects. The practical consequence is that groupef holds βj\beta_j directly (comparable to lme4::coef()), not bjb_j.

Ψ\Psi is diagonal, Ψ=diag(τ12,…,τpre2)\Psi = \mathrm{diag}(\tau^2_1, \ldots, \tau^2_{p_{re}}), because model_setup accepts only uncorrelated (||) random-effect terms and a single grouping factor. There is no random-effect correlation parameter to estimate.

What you supply

Three arguments carry the statistical content; the rest are controls.

design

A model_setup object, which fixes yy, DjD_j, the grouping factor, and the level-2 design Wj\mathcal{W}_j. It also reports whether the data can identify the model at all; see vignette("Chapter-B08", package = "lmebayesCore").

pfamily_list

The prior on the population level, one component per random-effect coefficient. This is where you declare whether the between-group variance τk2\tau^2_k is known (dNormal, treating the supplied dispersion as fixed) or estimated (dIndependent_Normal_Gamma, placing a Gamma prior on the precision 1/τk21/\tau^2_k).

dispprior_list

The prior on the residual variance σ2\sigma^2: a fixed value (scalar, or one per group), a single dGamma() to estimate one pooled σ2\sigma^2, or a named list of dGamma() objects to estimate a separate σj2\sigma^2_j per group.

Both priors are normally produced from one Prior_Setup_GLMM object, via pfamily_list() and either ps$group.dispersion or dGamma_list(ps).

Those two choices are also what determine which internal engine runs. You never name an engine; it follows from the priors. The statistically meaningful consequence is that when σ2\sigma^2 is fixed and every component is dNormal, the posterior is exactly multivariate normal and can be sampled directly; every other combination requires iterative sampling. See vignette("Chapter-B01", package = "lmebayesCore") for the full mapping and vignette("Chapter-B04", package = "lmebayesCore") for the exact route.

What the returned draws are

This is the part most likely to differ from other samplers, and it governs how the output should be analyzed.

rlmerb runs n independent replicate chains and keeps one draw from each — the state after that chain's final sweep. It does not run one long chain and thin it. Two consequences:

  1. The n draws are mutually independent. They are a genuine iid sample, not an autocorrelated series. Ordinary Monte Carlo standard errors apply directly; there is no effective sample size to compute, nothing to thin, and no trace plot or R^\hat{R} to inspect on the returned sample.

  2. Each draw comes from a distribution deliberately made close to the target. The sweep count m_convergence is not a burn-in guess. It is calibrated before sampling so that the distribution reached at the final sweep is within tv_tol of the exact posterior in total variation. tv_tol is a per-draw guarantee, and it is reported on the fit.

So n and tv_tol control different errors and can be set independently: n controls Monte Carlo noise, tv_tol controls the residual bias of each draw. To certify the whole n-draw sample jointly at level α\alpha, pass tv_tol = alpha / n; because cost grows only logarithmically in 1/1/tv_tol, this typically adds a few sweeps.

In the exactly-Gaussian case (fixed σ2\sigma^2, all dNormal) the draws are exact rather than approximate: m_convergence is 1 and no approximation is involved.

How m_convergence is derived, and what to do when a warning reports strong cross-block coupling, is covered in vignette("Chapter-B02", package = "lmebayesCore") and vignette("Chapter-B03", package = "lmebayesCore").

Point estimates without sampling

simulate = FALSE returns only the posterior mean of (γ,β)(\gamma, \beta) at fixed variance-component plug-ins, computed by iterated conditional modes. No sweeps run and no RNG is consumed, so the result is deterministic and fast — useful when iterating on model specification. All draw and convergence slots are NULL. This same quantity is where the replicate chains start when simulate = TRUE, which is why it is always computed.

Value

An object of class c("rlmerb", "list"). Each draw slot holds one row (or one value) per replicate chain, taken at that chain's final sweep; see the "What the returned draws are" section above.

Population level (γ\gamma, the analogue of lme4 fixed effects):

popef

Named list, one entry per random-effect coefficient. Entry kk is an n ×qk\times q_k matrix of draws, columns named by colnames(design$W[[k]]).

popef.means, popef.mode

Named lists in the same shape: the mean of the draws, and the posterior mode used to start every chain. popef.init records the starting values.

Group level (βj\beta_j, one vector per group):

groupef

Data frame in long format with n ×J\times J rows: a draw index, a column named for the grouping factor, then one column per random-effect coefficient. These are full coefficients, comparable to lme4::coef(), not mean-zero deviations.

groupef.mode

J×preJ \times p_{re} matrix of posterior modes, rows named by group level.

Variance components:

popef.dispersion

n ×pre\times p_{re} matrix of between-group variance draws τk2\tau^2_k. Constant down a column for a dNormal component (the value was treated as known); varying for an dIndependent_Normal_Gamma component. popef.dispersion.mean is the column mean.

group.dispersion

Residual variance σ2\sigma^2: a fixed scalar or per-group vector, or draws when a dGamma measurement prior was used. group.dispersion.mean is the corresponding mean.

Convergence reporting:

m_convergence

Integer sweeps run per chain; 1 on the exact route.

convergence_info

List with method ("exact_iid" when the draws are exact), lambda_star and eigenvalues (the contraction rate behind the sweep count), draw_engine, sim_method_used, and icm_info for the mode solve. convergence is the same list.

Inputs echoed back: design, family, n, call, pfamily_list, dispprior_list, prior.weights, offset, plus the packed prior and thin Prior summaries.

When simulate = FALSE, every draw and convergence slot is NULL and only the mode fields are populated.

References

Lindley DV, Smith AFM (1972). “Bayes Estimates for the Linear Model.” Journal of the Royal Statistical Society. Series B (Methodological), 34(1), 1–41. doi:10.1111/j.2517-6161.1972.tb00899.x.

See Also

rglmerb, rlmerb_point, rLMMNormal_reg_known_vcov, rLMMNormal_reg_estimated_vcov, rLMMindepNormalGamma_reg_known_vcov, rLMMindepNormalGamma_reg_estimated_vcov, Prior_Setup_GLMM, rlmb

Examples

## rlmerb() — matrix-level Gaussian LMM sampler on lme4::sleepstudy
##
## Reaction time versus centered days of sleep deprivation, with a random
## intercept and slope by Subject.  lmebayesCore requires uncorrelated
## random-effect terms, hence the `||` form.  For the formula interface see
## lmebayes::lmerb(), which wraps this function.

data(sleepstudy, package = "lme4")
dat <- sleepstudy
dat$Days_c <- dat$Days - mean(dat$Days)

form <- Reaction ~ Days_c + (1 + Days_c || Subject)

design <- model_setup(form, data = dat)
ps     <- Prior_Setup_GLMM(form, data = dat, pop.pwt = 0.01)

## Point estimates only: exact joint Gaussian posterior mean, no sampling.
pt <- rlmerb(
  design          = design,
  pfamily_list    = pfamily_list(ps),
  dispprior_list  = list(dispersion = ps$group.dispersion),
  simulate        = FALSE,
  verbose         = FALSE,
  print_icm_table = FALSE
)
pt$popef.mode

## Draws: fixed sigma^2 and known tau^2_k, so the joint posterior is exactly
## multivariate normal and sim_method = "DEFAULT" samples it directly (iid).
set.seed(1)
fit <- rlmerb(
  n               = 20L,
  design          = design,
  pfamily_list    = pfamily_list(ps),
  dispprior_list  = list(dispersion = ps$group.dispersion),
  progbar         = FALSE,
  verbose         = FALSE,
  print_icm_table = FALSE
)

fit$convergence_info$sim_method_used
print_groupef(fit, draws = 1:5, groups = 1:3)

Simulation Functions for Linear Mixed Models

Description

Posterior draws for Bayesian linear mixed models, given design matrices y, D, W and population priors pfamily_list. The model is stated under “Model and notation” below.

These are the engine-level entry points, normally reached through rlmerb (or lmerb() in lmebayes), which is also where the returned object is documented field by field. Each returned draw is one independent replicate chain's state at its final sweep, except on the fully-known-variance route, where draws are exact.

Usage

## S3 method for class 'rLMMNormal_reg'
print(
  x,
  digits = max(3, getOption("digits") - 3),
  components = NULL,
  draws = NULL,
  ...
)

## S3 method for class 'rLMMindepNormalGamma_reg'
print(
  x,
  digits = max(3, getOption("digits") - 3),
  components = NULL,
  draws = NULL,
  ...
)

rLMMNormal_reg(
  n,
  y,
  D,
  group,
  W,
  pfamily_list,
  dispprior_list,
  offset = NULL,
  weights = 1,
  icm_tol = 1e-10,
  icm_maxit = 200L,
  tv_tol = 0.01,
  progbar = TRUE,
  verbose = FALSE,
  sim_method = "DEFAULT"
)

rLMMNormal_reg_known_vcov(
  n,
  y,
  D,
  group,
  W,
  pfamily_list,
  dispprior_list,
  offset = NULL,
  weights = 1,
  icm_tol = 1e-10,
  icm_maxit = 200L,
  tv_tol = 0.01,
  progbar = TRUE,
  verbose = FALSE,
  sim_method = "DEFAULT"
)

rLMMNormal_reg_known_vcov_iid(
  n,
  y,
  D,
  group,
  W,
  pfamily_list,
  dispprior_list,
  offset = NULL,
  weights = 1,
  icm_tol = 1e-10,
  icm_maxit = 200L,
  tv_tol = 0.01,
  progbar = TRUE,
  verbose = FALSE
)

rLMMNormal_reg_known_vcov_two_bg(
  n,
  y,
  D,
  group,
  W,
  pfamily_list,
  dispprior_list,
  offset = NULL,
  weights = 1,
  icm_tol = 1e-10,
  icm_maxit = 200L,
  tv_tol = 0.01,
  progbar = TRUE,
  verbose = FALSE
)

rLMMNormal_reg_estimated_vcov(
  n,
  y,
  D,
  group,
  W,
  pfamily_list,
  dispprior_list,
  offset = NULL,
  weights = 1,
  icm_tol = 1e-10,
  icm_maxit = 200L,
  tv_tol = 0.01,
  progbar = TRUE,
  verbose = FALSE,
  gap_tol = 0.0196,
  mode_gap_max = 1,
  diag_sweeps = FALSE,
  stage_verbose = FALSE,
  sim_method = "DEFAULT"
)

rLMMindepNormalGamma_reg(
  n,
  y,
  D,
  group,
  W,
  pfamily_list,
  dispprior_list,
  offset = NULL,
  weights = 1,
  icm_tol = 1e-10,
  icm_maxit = 200L,
  tv_tol = 0.01,
  progbar = TRUE,
  verbose = FALSE
)

rLMMindepNormalGamma_reg_known_vcov(
  n,
  y,
  D,
  group,
  W,
  pfamily_list,
  dispprior_list,
  offset = NULL,
  weights = 1,
  icm_tol = 1e-10,
  icm_maxit = 200L,
  tv_tol = 0.01,
  progbar = TRUE,
  verbose = FALSE
)

rLMMindepNormalGamma_reg_estimated_vcov(
  n,
  y,
  D,
  group,
  W,
  pfamily_list,
  dispprior_list,
  offset = NULL,
  weights = 1,
  icm_tol = 1e-10,
  icm_maxit = 200L,
  tv_tol = 0.01,
  progbar = TRUE,
  verbose = FALSE,
  gap_tol = 0.0196,
  mode_gap_max = 1,
  diag_sweeps = FALSE,
  stage_verbose = FALSE
)

rLMMindepNormalGamma_reg_known_vcov_v2(
  n,
  y,
  D,
  group,
  W,
  pfamily_list,
  dispprior_list,
  offset = NULL,
  weights = 1,
  icm_tol = 1e-10,
  icm_maxit = 200L,
  tv_tol = 0.01,
  progbar = TRUE,
  verbose = FALSE
)

rLMMindepNormalGamma_reg_estimated_vcov_v2(
  n,
  y,
  D,
  group,
  W,
  pfamily_list,
  dispprior_list,
  offset = NULL,
  weights = 1,
  icm_tol = 1e-10,
  icm_maxit = 200L,
  tv_tol = 0.01,
  progbar = TRUE,
  verbose = FALSE,
  gap_tol = 0.0196,
  mode_gap_max = 1,
  diag_sweeps = FALSE,
  stage_verbose = FALSE
)

Arguments

x

For print methods, a simfunc return object.

digits

Number of significant digits for printed numeric values.

components

For print methods: NULL (default; print all population components in names(x$popef)), a character vector of component names (e.g. "(Intercept)", slopes), or integer indices.

draws

For print methods and print_groupef: NULL (default; all draws) or integer indices / logical of length n, as in rnorm(n)[1:10]. Only the printed rows are subset; the object is unchanged. Example: print(x, draws = 1:10).

...

further arguments passed to or from other methods.

n

Number of stored draws. If length(n) > 1, the length is used.

y

Response vector of length l2 (= nrow(D)).

D

Level-1 design matrix (l2 x p_re); this is DD (row-stacked DjD_j) in “Model and notation” below. Must have unique, non-empty colnames(D): these are the random-effect coefficient names used to key W and pfamily_list (there is no separate groupef.names argument to override them).

group

Grouping factor of length l2 (must be a factor; levels(group) fixes the row order of groupef draws – there is no separate group_levels argument. To use a level order/superset not present in the observed data, construct group as factor(observed_group, levels = full_superset) yourself. The name used for the grouping column in groupef (group_name) is resolved from attr(group, "group_name") if set, otherwise from group's own variable name via substitute() – this only works when group is passed as a bare variable (e.g. group = school_id); otherwise attach the name yourself via attr(group, "group_name") <- "school_id".

W

Named list of group-level design matrices (J x q_k), one per column of D; this is W\mathcal{W} in “Model and notation” below: W[[k]] is WkW_k and W[[k]][j, ] is the block WkjW_{kj} of Wj\mathcal{W}_j.

pfamily_list

Named list of population pfamily objects. The group random-effect prior precision (formerly a separate P argument) is always derived internally from pfamily_list: one τk2\tau^2_k plug-in per component (fixed dispersion for dNormal, prior mean rate/(shape−1)rate/(shape - 1) for dIndependent_Normal_Gamma), assembled into a diagonal precision matrix – this is Ψ−1\Psi^{-1} in “Model and notation” below. There is no way to supply a precision inconsistent with pfamily_list.

dispprior_list

Observation-dispersion prior: list(dispersion = sigma2) for fixed σ2\sigma^2 routes (sigma2 a single positive scalar, pooled across groups, or – for rLMMNormal_reg/rLMMNormal_reg_known_vcov/ rLMMNormal_reg_estimated_vcov only – a numeric vector of length nlevels(group) giving one fixed, known dispersion per group, matched to levels(group) either positionally or by name); dGamma() fields for legacy rLMMindepNormalGamma_reg; or, for rLMMindepNormalGamma_reg_known_vcov/ rLMMindepNormalGamma_reg_estimated_vcov, one of three shapes for the ING observation-dispersion prior: (1) a single dGamma() pfamily (pooled σ2\sigma^2 shared across groups) or (2) a named list of dGamma() pfamilies, one per group level, as returned by dGamma_list() (per-group σj2\sigma^2_j) – both preferred, since shape/rate/disp_lower/disp_upper are read straight from each pfamily's own prior_list – or (3) the legacy flat list itself (shape/rate/disp_upper/ disp_lower, or shape_group/rate_group/ disp_lower_group/disp_upper_group). In every case, group-level mu and Sigma for the random-effect coefficients are not read from dispprior_list: mu is always Wjγ\mathcal{W}_j\gamma (recomputed every sweep), and Sigma is always derived internally as solve(P) from pfamily_list.

offset, weights

Observation offset and prior weights (glmbayes-style formals: offset = NULL, weights = 1). Normalized to length length(y) and echoed on the return as offset/ offset2/prior.weights. Not yet used by the mixed-model sampling path (ICM / sweeps still assume unit weights and zero offset).

icm_tol, icm_maxit

ICM convergence controls for the internal population start.

tv_tol

Total-variation tolerance in (0, 1) for calibration. Inner Gibbs sweeps per stored draw (m_convergence) are derived from Theorem~3 at the ICM population start; pilot chain counts likewise.

progbar

Show a text progress bar during sampling.

verbose

Print convergence calibration / ICM lines.

sim_method

Simulation method for rLMMNormal_reg_known_vcov: "DEFAULT" (exact iid draws, see rLMMNormal_reg_known_vcov_iid) or "TWO_BLOCK_GIBBS" (two-block Gibbs sweeps, see rLMMNormal_reg_known_vcov_two_bg). Accepted but has no effect on rLMMNormal_reg_estimated_vcov (only TWO_BLOCK_GIBBS is implemented there): kept as a formal purely so callers may pass the same argument to either route without branching.

gap_tol, mode_gap_max, diag_sweeps, stage_verbose

Pilot-stage controls for rLMMNormal_reg_estimated_vcov and ING estimated routes (see route docs).

Value

An object of class c("<route>", "rLMMNormal_reg", "list") (or c("rLMMindepNormalGamma_reg", "list") for the legacy outer-loop engine), where <route> is the specific export that ran (e.g. "rLMMNormal_reg_known_vcov"). Components use package group/population names (see ‘inst/notation.md’), in glm/glmbayes-style order:

groupef

Draws of non-centered group coefficients βj\beta_j: a data frame with the grouping column plus one column per colnames(D). Each row is one group in one stored draw.

groupef.mode

J×preJ \times p_{re} matrix of ICM (or exact posterior-mean) group coefficients β^j\hat\beta_j; rows are levels(group), columns are colnames(D). Not lme4's mean-zero uju_j.

groupef.iters

Optional group-level envelope iteration counts.

popef

Named list of n x q_k matrices of population coefficient draws γk\gamma_k.

popef.mode, popef.init

ICM (or exact) population point estimates and main-stage starts.

popef.dispersion, popef.iters

Optional population per-draw diagnostics when produced by the sampler.

group.dispersion

Observation residual variance σ2\sigma^2/σj2\sigma^2_j when present (fixed scalar/vector or draws under a Gamma measurement prior). Optional group.dispersion.mean / group.dispersion.iters. Distinct from popef.dispersion (τk2\tau^2_k).

pfamily_list, dispprior_list

Population (pfamily_list) and observation-dispersion (dispprior_list) priors that were used.

prior.weights, offset, offset2

Normalized copies of the weights/offset arguments (glmbayes naming). Not yet consumed by sampling.

any_non_normal

Whether any population component is not dNormal.

family, design, n

Likelihood family, matrix inputs (y, D, group, W, groupef.names, group_name, plus echoed weights/offset), and chain count.

call

Matched call.

m_convergence

Inner Gibbs sweeps per stored draw (always 1L for the exact-iid known-vcov route).

convergence_info

Calibration details, including draw_engine, sim_method_used, icm_info, and optional pilot UB fields.

pilot

When a pilot ran: list with n, m_convergence, chisq, and draws; otherwise NULL.

sweep_history

Main-stage sweep history when collected.

Functions

  • rLMMNormal_reg(): Dispatcher for fixed σ2\sigma^2: routes to rLMMNormal_reg_known_vcov or rLMMNormal_reg_estimated_vcov by population pfamily_list.

  • rLMMNormal_reg_known_vcov(): Fixed σ2\sigma^2; all population components dNormal (known τk2\tau^2_k). Dispatches on sim_method: "DEFAULT" draws directly from the exact multivariate-normal posterior via rLMMNormal_reg_known_vcov_iid (no Gibbs sweeps, no burn-in, no autocorrelation between draws); "TWO_BLOCK_GIBBS" runs rLMMNormal_reg_known_vcov_two_bg instead (two-block Gibbs with Theorem~3 rate calibration – the only option available for rLMMNormal_reg_estimated_vcov and the GLMM routes).

  • rLMMNormal_reg_known_vcov_iid(): Fixed σ2\sigma^2; all population components dNormal; exact iid draws from the closed-form multivariate-normal posterior via rLMMNormal_joint_iid (sim_method = "DEFAULT" route of rLMMNormal_reg_known_vcov).

  • rLMMNormal_reg_known_vcov_two_bg(): Fixed σ2\sigma^2; all population components dNormal (known τk2\tau^2_k). Exact Theorem~3 rate calibration; two-block Gibbs sweeps (sim_method = "TWO_BLOCK_GIBBS" route of rLMMNormal_reg_known_vcov).

  • rLMMNormal_reg_estimated_vcov(): Fixed σ2\sigma^2; ING population components (estimated τk2\tau^2_k). Optional pilot stage; conservative disp_lower rate bound. sim_method is accepted but has no effect: variance components are estimated here, so the joint posterior is not exactly Gaussian and only two-block Gibbs sampling is implemented.

  • rLMMindepNormalGamma_reg(): Legacy outer-loop sampler: draws σ2\sigma^2 via rGamma_reg, then calls rLMMNormal_reg each replicate. Prefer rLMMindepNormalGamma_reg_known_vcov or rLMMindepNormalGamma_reg_estimated_vcov for the ING observation-dispersion path used by rlmerb.

  • rLMMindepNormalGamma_reg_known_vcov(): Random σ2\sigma^2 (pooled or per-group ING observation prior, drawn jointly with group coefficients); all population components dNormal. Used by rlmerb when group.dispersion is dGamma() / dGamma_list() and population priors are all dNormal.

  • rLMMindepNormalGamma_reg_estimated_vcov(): Random σ2\sigma^2 (pooled or per-group ING observation prior, drawn jointly with group coefficients); ING population components. Pilot/UB calibration via the sweep-outer sampler. Default path for rlmerb with dGamma() / dGamma_list() group.dispersion and ING population priors.

  • rLMMindepNormalGamma_reg_known_vcov_v2(): v2 (stub). Random σ2\sigma^2 (pooled or per-group), but drawn with the population block; group block is Gaussian at fixed Ω=1/σ2\Omega=1/\sigma^2. Same formals as rLMMindepNormalGamma_reg_known_vcov. Sampling not yet implemented; see ‘inst/DERIVATION_sigma2_with_block2_v2.md’.

  • rLMMindepNormalGamma_reg_estimated_vcov_v2(): v2 (stub). Random σ2\sigma^2 with the population block (and ING population components). Same formals as rLMMindepNormalGamma_reg_estimated_vcov. Sampling not yet implemented; see ‘inst/DERIVATION_sigma2_with_block2_v2.md’.

Model and notation

For group j=1,…,Jj = 1, \ldots, J (J=J =length(group_levels)), with njn_j observations in group jj and pre=p_{re} = ncol(D) group-varying coefficients:

Likelihood (group level):

yj∣βj,σj2 ∼ N(Djβj, σj2Inj)y_j \mid \beta_j, \sigma_j^2 \ \sim\ N\bigl(D_j\beta_j,\ \sigma_j^2 I_{n_j}\bigr)

Hierarchical prior (population level):

βj∣γ,Ψ ∼ N(Wjγ, Ψ)\beta_j \mid \gamma, \Psi \ \sim\ N\bigl(\mathcal{W}_j\gamma,\ \Psi\bigr)

The full, non-centered coefficient vector βj\beta_j – not a mean-zero deviation – appears directly in the likelihood. This is a centered parameterization in the sense of (Lindley and Smith 1972): βj\beta_j is the group parameter, γ\gamma the population (hyper-mean) parameter, and Ψ\Psi the population covariance. Wj\mathcal{W}_j is block-diagonal across the prep_{re} coefficient dimensions,

Wj=blockdiag(W1j,…,Wprej),Wkj∈R1×qk,\mathcal{W}_j = \mathrm{blockdiag}\bigl(W_{1j}, \ldots, W_{p_{re}j}\bigr), \qquad W_{kj} \in \mathbb{R}^{1 \times q_k},

and each coefficient kk may have its own number of level-2 predictors qkq_k (no padding needed, unlike a 3-index array).

Correspondence to these functions' arguments/return values:

DD (row-stacked DjD_j)

D: the l2×prel_2 \times p_{re} level-1 design matrix, one row per observation; DjD_j is the submatrix D[group == jj, ].

Wj\mathcal{W}_j

Assembled on demand from W: block WkjW_{kj} is row jj of W[[k]], so W is list(W1,…,WpreW_1, \ldots, W_{p_{re}}), one J×qkJ \times q_k matrix per RE column kk.

γ\gamma

popef/popef.mode in the returned object (one entry per RE column, named by colnames(W[[k]])).

βj\beta_j

The group-jj row of groupef/ groupef.mode – not lme4's mean-zero bjb_j (see below).

Ψ\Psi

Diagonal, Ψ=diag(τ12,…,τpre2)\Psi = \mathrm{diag}(\tau^2_1, \ldots, \tau^2_{p_{re}}), one τk2\tau^2_k plug-in per pfamily_list component (see pfamily_list below); Ψ−1\Psi^{-1} is the internally-derived group prior precision P.

group index jj

group (levels(group) fixes the order j=1,…,Jj = 1, \ldots, J).

Correspondence to lme4/Laird-Ware: marginalizing (substituting the population equation into the group likelihood) recovers the standard lme4 form

yj=Dj(Wjγ+uj)+εj=(DjWj)⏟Xjγ+Dj⏟Zjuj+εj,y_j = D_j(\mathcal{W}_j\gamma + u_j) + \varepsilon_j = \underbrace{(D_j\mathcal{W}_j)}_{X_j}\gamma + \underbrace{D_j}_{Z_j}u_j + \varepsilon_j,

where uj:=βj−Wjγu_j := \beta_j - \mathcal{W}_j\gamma is the mean-zero deviation lme4 calls bjb_j. DjD_j itself has no single lme4 counterpart: it is the common ancestor of both XjX_j (fixed-effects design, after right-multiplying by Wj\mathcal{W}_j) and ZjZ_j (random-effects design, used as-is). γ\gamma corresponds to lme4's β\beta (fixed effects), and Ψ\Psi to the per-group block of lme4's GG matrix (G=IJ⊗ΨG = I_J \otimes \Psi); the functions documented here never materialize uju_j/bjb_j – βj\beta_j is sampled directly against yjy_j at every group-level Gibbs update (the per-group/shared-γ\gamma conjugate updates for βj\beta_j and γ\gamma are the ZjZ_j/bjb_j-lettered formulas in lmebayes_posterior_icm, with Zj≡DjZ_j \equiv D_j and bj≡βjb_j \equiv \beta_j there). When every Wj\mathcal{W}_j is intercept-only (qk=1q_k = 1 for all kk, i.e. no level-2 predictors), the model reduces to a standard random-intercept/random-slope lmer model: γ^\hat\gamma should then recover lme4's fixed effects and β^j\hat\beta_j should track lme4::coef() (lme4::ranef() tracks β^j−γ^\hat\beta_j - \hat\gamma), giving a natural automated equivalence check.

For the full write-up (including the block-diagonal Gibbs full-conditional derivation) see notation.md in system.file(package = "lmebayesCore") (source: inst/notation.md).

What the prior choices mean

Callers never name a route. It follows from two modelling decisions, and the decisions — not the routing — are what matter statistically.

1. Is the residual variance σ2\sigma^2 known or estimated? Supplying a positive scalar (or one value per group) treats it as known and conditions on it. Supplying a dGamma() prior — one pooled, or a dGamma_list with one per group — estimates it.

2. Are the between-group variances τk2\tau^2_k known or estimated? A dNormal component treats τk2\tau^2_k as known; a single dIndependent_Normal_Gamma component anywhere in pfamily_list makes that variance a parameter to be estimated.

The cross of the two selects one of four engines:

σ2\sigma^2 τk2\tau^2_k Engine
known all known rLMMNormal_reg_known_vcov
known any estimated rLMMNormal_reg_estimated_vcov
estimated all known rLMMindepNormalGamma_reg_known_vcov
estimated any estimated rLMMindepNormalGamma_reg_estimated_vcov

The consequence that shows up in the output is exactness. When both variances are known the posterior is exactly multivariate normal, so draws are exact, popef.mode is the true posterior mean, and m_convergence is 1. As soon as either variance is estimated the posterior is no longer Gaussian: draws come from the final sweep of a calibrated chain, popef.mode is a conditional mode rather than a mean, and a short pilot stage runs first.

sim_method is therefore meaningful only in the fully-known case, the one route offering a choice between drawing directly from the closed form ("DEFAULT") and running the sweep loop on the same target ("TWO_BLOCK_GIBBS", which exists mainly to cross-check the two against each other). Elsewhere the argument is ignored.

The rLMMindepNormalGamma_reg_*_v2 functions are stubs for the same priors with σ2\sigma^2 updated alongside the population parameters; they are not yet implemented.

One input you cannot supply: the group prior precision Ψ−1\Psi^{-1}. It is always derived from pfamily_list, and a caller-supplied P/Sigma is rejected, so it cannot silently disagree with the population priors. See vignette("Chapter-B01", package = "lmebayesCore") for the engine map and vignette("Chapter-B04", package = "lmebayesCore") for the exact route.

Dispatchers

rLMMNormal_reg and rLMMindepNormalGamma_reg validate inputs and delegate to the appropriate route (or, for the legacy outer-loop rLMMindepNormalGamma_reg, draw σ2\sigma^2 then call rLMMNormal_reg).

References

Lindley DV, Smith AFM (1972). “Bayes Estimates for the Linear Model.” Journal of the Royal Statistical Society. Series B (Methodological), 34(1), 1–41. doi:10.1111/j.2517-6161.1972.tb00899.x.

See Also

rGLMM_reg, rlmerb, print_groupef, rindepNormalGamma_reg, lmebayes_posterior_icm

glmbayes Simulation Functions rGLMM_Re_Draw(), rGLMM_reg(), rGLMM_sweep(), rLMMNormal_joint_iid(), two_block_optimize_pilot_cost(), two_block_rNormal_reg(), two_block_rate(), two_block_tv_bound()

Examples

## rLMMNormal_reg_known_vcov_iid() — exact iid draws on big_word_club
##
## Same data / formula path as the Ex_10 / Ex_13b demos; uses only the
## fixed-dispersion, known-vcov iid sampler. Requires suggested package
## bayesrules.

if (requireNamespace("bayesrules", quietly = TRUE)) {
  data(big_word_club, package = "bayesrules")
  dat <- big_word_club
  dat$school_id <- factor(dat$school_id)
  dat <- subset(
    dat,
    !is.na(score_ppvt) &
      !is.na(invalid_ppvt) & invalid_ppvt == 0L &
      complete.cases(dat[, c(
        "score_ppvt", "distracted_a1", "distracted_ppvt",
        "private_school", "title1", "free_reduced_lunch", "school_id"
      )])
  )

  form_lmer <- score_ppvt ~
    private_school + title1 + free_reduced_lunch +
    distracted_ppvt + distracted_a1 +
    free_reduced_lunch:distracted_a1 +
    (1 + distracted_ppvt + distracted_a1 || school_id)

  design <- model_setup(form_lmer, data = dat)
  ps <- Prior_Setup_GLMM(form_lmer, data = dat, pop.pwt = 0.01)
  pf <- pfamily_list(ps)

  ## Exact iid draws (fixed sigma^2, known tau^2_k / all dNormal).
  ## design$group already carries attr(*, "group_name").
  set.seed(1)
  fit <- rLMMNormal_reg_known_vcov_iid(
    n = 20L,
    y = design$y,
    D = design$D,
    group = design$group,
    W = design$W,
    pfamily_list = pf,
    dispprior_list = list(dispersion = ps$group.dispersion),
    progbar = FALSE,
    verbose = FALSE
  )

  ## Print a few population draws (object unchanged).
  print(fit, draws = 1:5)
  print_groupef(fit, draws = 1:5, groups = 1:3)
}

Gaussian blockwise draw with widetilde-B rejection

Description

Like rNormal_reg_group, but when beta_set is supplied draws independent unrestricted joint samples via rNormal_reg_group, stacks group coefficients, and keeps only draws with beta_in_marginal_safe_set. Additional batches use the observed acceptance rate until n accepted joint draws are collected.

Usage

rNormal_reg_group_safe(
  n,
  y,
  x,
  group,
  prior_list = NULL,
  prior_lists = NULL,
  beta_set = NULL,
  offset = NULL,
  weights = 1,
  max_tries = NULL,
  mc_seed = NULL,
  Gridtype = 2L
)

Arguments

n

Number of iid draws per group (n = 1 typical for Gibbs).

y

Response vector of length nrow(x).

x

Design matrix nrow(x) by ncol(x); same ncol in every group.

group

Group partition: factor/integer length l2, l2_blocks counts summing to l2, or list of row index vectors.

prior_list

Single prior specification recycled to all groups, or with mu as l1 by k matrix or blocks sublist.

prior_lists

Optional list of length k (or 1) of per-group prior_list objects.

beta_set

Optional beta_marginal_safe_set; when NULL, behaves as rNormal_reg_group.

offset

Optional numeric vector (length 1 or length(y)); partitioned across groups like y.

weights

Optional weights; same recycling and blocking as offset.

max_tries

Maximum attempts when beta_set is set.

mc_seed

Optional RNG seed.

Gridtype

Passed to each group's sampler (Armadillo Gridtype).

Value

Like rNormal_reg_group, plus safe-set fields n_accepted, n_target, n_tried, accept_rate, restricted, beta_set, and coefficients_all when n > 1 accepted draws.

See Also

rNormalGLM_reg_group_safe, beta_in_marginal_safe_set


GLM blockwise draw with widetilde-B rejection

Description

Non-Gaussian counterpart of rNormal_reg_group_safe using rNormalGLM_reg_group for each unrestricted joint attempt, then joint rejection on beta_set.

Usage

rNormalGLM_reg_group_safe(
  n,
  y,
  x,
  group,
  prior_list = NULL,
  prior_lists = NULL,
  family,
  beta_set = NULL,
  offset = NULL,
  weights = 1,
  max_tries = NULL,
  mc_seed = NULL,
  Gridtype = 2L,
  n_envopt = NULL,
  use_parallel = TRUE,
  use_opencl = FALSE,
  verbose = FALSE,
  progbar = FALSE
)

Arguments

n

Number of iid draws per group (n = 1 typical for Gibbs).

y

Response vector of length nrow(x).

x

Design matrix nrow(x) by ncol(x); same ncol in every group.

group

Group partition: factor/integer length l2, l2_blocks counts summing to l2, or list of row index vectors.

prior_list

Single prior specification recycled to all groups, or with mu as l1 by k matrix or blocks sublist.

prior_lists

Optional list of length k (or 1) of per-group prior_list objects.

family

GLM family (not gaussian()).

beta_set

Optional beta_marginal_safe_set; when NULL, behaves as rNormal_reg_group.

offset

Optional numeric vector (length 1 or length(y)); partitioned across groups like y.

weights

Optional weights; same recycling and blocking as offset.

max_tries

Maximum attempts when beta_set is set.

mc_seed

Optional RNG seed.

Gridtype

Passed to each group's sampler (Armadillo Gridtype).

n_envopt

Passed to each group; defaults to 1 when NULL.

use_parallel, use_opencl, verbose, progbar

Passed to each group's GLM sampler.

Value

Like rNormalGLM_reg_group, plus safe-set metadata (see rNormal_reg_group_safe).

See Also

rNormal_reg_group_safe, beta_in_marginal_safe_set


Foster / Rosenthal drift constants from floor coupling eigenvalues.

Description

Foster / Rosenthal drift constants from floor coupling eigenvalues.

Usage

rosenthal_drift_constants(
  eigenvalues,
  gamma_0 = NULL,
  mode = NULL,
  display_mode = c("sharp", "general"),
  d = NULL,
  V_sup = NULL
)

Arguments

eigenvalues

Output of floor_coupling_eigenvalues (or any list with kappa_lb and q).

gamma_0

Start state for V(γ0)V(\gamma_0) in general mode.

mode

Optional population_mode result for general mode.

display_mode

"sharp" (default) or "general".

d

Deficiency level for Vsup⁡(d)V_{\sup}(d) when V_sup is omitted.

V_sup

Override for Vsup⁡V_{\sup} in general mode.

Value

A list with kappa_lb, lambda_lb, b_drift, C_beta_plus, U, drift numerator/denominator, and Lyapunov values.

See Also

rosenthal_tv_bound, floor_coupling_eigenvalues


Rosenthal total-variation bound at fixed tuning parameter alpha.

Description

Evaluates the Rosenthal (1995) bound at sweep count k, minorization eps, floor eigenvalues, and fixed alpha. To optimize alpha or k, use optimal_rosenthal_tv_bound.

Usage

rosenthal_tv_bound(
  k,
  alpha,
  eps,
  eigenvalues,
  drift = NULL,
  gamma_0 = NULL,
  mode = NULL,
  display_mode = c("sharp", "general"),
  d = NULL,
  V_sup = NULL
)

Arguments

k

Number of Gibbs sweeps (k≥1k \ge 1).

alpha

Tuning parameter in (λLB,1)(\lambda^{\mathrm{LB}}, 1) (required).

eps

Minorization constant (typically epsilon_star()$eps_star).

eigenvalues

Output of floor_coupling_eigenvalues.

drift

Optional output of rosenthal_drift_constants; if NULL, computed from eigenvalues.

gamma_0

Start state for V(γ0)V(\gamma_0) in general mode.

mode

Optional population_mode result for general mode.

display_mode

"sharp" (default) or "general".

d

Deficiency level for Vsup⁡(d)V_{\sup}(d) when V_sup is omitted.

V_sup

Override for Vsup⁡V_{\sup} in general mode.

Value

A list with bound, minorization, drift, k, alpha, eps, and drift_constants.

See Also

optimal_rosenthal_tv_bound


Conditionally independent row-group simulation (Gibbs / product likelihood)

Description

Draw from groupwise full conditionals when the posterior factorizes across observation groups. rNormal_reg_group uses the Gaussian rNormalReg() path per group; rNormalGLM_reg_group uses the GLM envelope path per group. Typical use is block Gibbs (n = 1 per outer step); n > 1 gives iid draws from the product conditional.

Usage

rNormalGLM_reg_group(
  n,
  y,
  x,
  group,
  prior_list = NULL,
  prior_lists = NULL,
  offset = NULL,
  weights = 1,
  family = gaussian(),
  Gridtype = 2L,
  n_envopt = NULL,
  use_parallel = TRUE,
  use_opencl = FALSE,
  verbose = FALSE,
  progbar = FALSE
)

rNormal_reg_group(
  n,
  y,
  x,
  group,
  prior_list = NULL,
  prior_lists = NULL,
  offset = NULL,
  weights = 1,
  Gridtype = 2L
)

Arguments

n

Number of iid draws per group (n = 1 typical for Gibbs).

y

Response vector of length nrow(x).

x

Design matrix nrow(x) by ncol(x); same ncol in every group.

group

Group partition: factor/integer length l2, l2_blocks counts summing to l2, or list of row index vectors.

prior_list

Single prior specification recycled to all groups, or with mu as l1 by k matrix or blocks sublist.

prior_lists

Optional list of length k (or 1) of per-group prior_list objects.

offset

Optional numeric vector (length 1 or length(y)); partitioned across groups like y.

weights

Optional weights; same recycling and blocking as offset.

family

GLM family (not gaussian()).

Gridtype

Passed to each group's sampler (Armadillo Gridtype).

n_envopt

Passed to each group; defaults to 1 when NULL.

use_parallel, use_opencl, verbose, progbar

Passed to each group's GLM sampler.

Details

Output layout: coefficients and coef.mode are matrices with rows = groups and columns = predictors.

Per-group prior mean: pass prior_list$mu as an l1 x k matrix (one column per group).

Dispersion: must be supplied via prior_list$dispersion (residual variance σ2\sigma^2).

Value

For rNormalGLM_reg_group, a list with class "rNormalGLM_reg_group" including coefficients, coef.mode, group_info, and group_results.

A list with class "rNormal_reg_group" including coefficients, coef.mode, dispersion, and group_info.

See Also

rNormal_reg_group_safe, rNormalGLM_reg_group_safe

Examples

## rNormalGLM_reg_group() — independent Poisson GLMs by group (toy)

set.seed(1)
group <- factor(rep(c("A", "B"), each = 15L))
x <- cbind(1, rnorm(length(group)))
colnames(x) <- c("(Intercept)", "x1")
eta <- x %*% c(-0.5, 0.3) + ifelse(group == "A", 0, 0.4)
y <- rpois(length(group), exp(eta))

prior_list <- list(
  mu = rep(0, ncol(x)),
  P  = diag(10, ncol(x)),
  dispersion = 1
)

out <- rNormalGLM_reg_group(
  n = 1L,
  y = y,
  x = x,
  group = group,
  prior_list = prior_list,
  family = poisson(),
  use_parallel = FALSE
)

out$coefficients
out$coef.mode
## rNormal_reg_group() — independent Gaussian regressions by group (toy)

set.seed(42)
n_groups <- 3L
n_per    <- 10L
school   <- rep(seq_len(n_groups), each = n_per)
x        <- cbind(1, rnorm(n_groups * n_per))
colnames(x) <- c("(Intercept)", "X1")
b_true   <- matrix(c(5, 0.5, 3, -0.2, 7, 0.3), nrow = n_groups, byrow = TRUE)
sigma2   <- 1.5
y        <- rowSums(x * b_true[school, ]) + rnorm(nrow(x), sd = sqrt(sigma2))
l1       <- ncol(x)

prior_list <- list(
  mu         = rep(0, l1),
  Sigma      = diag(100, l1),
  dispersion = sigma2,
  ddef       = FALSE
)

out <- rNormal_reg_group(
  n = 1L, y = y, x = x, group = school, prior_list = prior_list
)

out$coefficients
out$coef.mode

Summarizing Bayesian mixed model distribution functions

Description

These functions are methods for class rlmerb, rglmerb, or summary.rlmerb objects, following the layout of summary.rglmb extended to the two-block mixed model: Block~2 (population/level-2) tables are reported per random-effects component, and Block~1 (group-level) effects are summarized in aggregate.

Usage

## S3 method for class 'rlmerb'
summary(object, groups = NULL, ...)

## S3 method for class 'summary.rlmerb'
print(x, digits = max(3, getOption("digits") - 3), ...)

## S3 method for class 'rglmerb'
summary(object, groups = NULL, ...)

Arguments

object

An object of class "rlmerb" or "rglmerb".

groups

Optional character vector of grouping levels for which to include a per-group Block~1 detail table. When NULL (default) only the aggregate groupef_overview is returned.

x

An object of class "summary.rlmerb".

digits

The number of significant digits to use when printing.

...

Additional optional arguments.

Details

Like summary.rglmb, which fits its own glm maximum-likelihood reference, this method computes its reference comparison internally from the model_setup object carried on the fit — nothing needs to be supplied by the caller.

The reference is the lmer fit built by model_setup(), whose fixed effects are on a different footing from the Block~2 hyperparameters: γ\gamma is indexed by (random-effect component, level-2 parameter), whereas lmer reports one flat coefficient vector. The two are put on a common footing before being tabulated (see ‘Reference correspondence’ below), so the lmer column is the coefficient that estimates the same quantity, not merely one with a similar name.

Value

summary.rlmerb returns an object of class "summary.rlmerb", a list with components:

call, formula

The originating call and model formula.

n

Integer number of stored draws.

sim_method_used, method, draw_engine

Character labels for the engine that produced the draws.

m_convergence, lambda_star

Sweeps per stored draw, and the convergence rate λ∗\lambda^* (both NULL for the exact iid route).

n_obs, n_groups, group_name, groupef.names

Design dimensions.

varcor

The reference lmer variance-correlation object.

popef

Named list, one entry per random-effect component, each a list of matrices coefficients0 (Prior Mean, Prior.sd, lmer, lmer.se), coefficients (Post.Mode, Post.Mean, Post.Sd, MC Error, Pr(Prior_tail)) and Percentiles.

popef_prior_overview, popef_overview, popef_percentiles_overview

The same three tables stacked across components, with rows named component::parameter.

tau2_prior_overview, tau2_overview, tau2_percentiles_overview

Block~2 variance components τk2\tau^2_k: prior family and moments against the lmer estimate, posterior summaries, and posterior percentiles. A Residual row for the observation variance σ2\sigma^2 is appended when it is pooled.

sigma2_group_overview

Per-group σ2\sigma^2 table when the measurement dispersion is per-group, otherwise NULL.

groupef_overview

Distribution of the Block~1 posterior modes across groups, per component.

groupef_groups

Per-group detail for the levels named in groups, or NULL.

any_non_normal

TRUE when any Block~2 component uses a non-dNormal prior.

print.summary.rlmerb returns its argument invisibly and is called for the side effect of printing.

Reference correspondence

Writing kk for the random-effect component and pp for the level-2 parameter within that component, γk,p\gamma_{k,p} maps to an lmer fixed effect as follows:

kk and pp both the intercept

the lmer intercept.

pp the intercept, kk a slope

the main effect kk: the average of the random slope kk across groups.

kk the intercept, pp a covariate

the main effect pp: a group-level predictor of the random intercept.

both kk and pp slopes

the cross-level interaction p:kp{:}k, since pp moderates the random slope kk. The interaction column recorded in model_setup()$popef.moderation is used when available, and the p:kp{:}k / k:pk{:}p orderings are tried otherwise.

Coefficients with no counterpart are reported as NA rather than matched approximately.

See Also

rlmerb, rglmerb, model_setup, Prior_Setup_GLMM, summary.rglmb


Two-block Gibbs sampler convergence rate (Remark 8 eigenvalues)

Description

Computes the eigenvalues of A=P11−1/2P12P22−1P21P11−1/2A = P_{11}^{-1/2} P_{12} P_{22}^{-1} P_{21} P_{11}^{-1/2} for the joint Gaussian posterior targeted by two_block_rNormal_reg with family = gaussian() and fixed variance components. The maximal eigenvalue λ∗\lambda^* is the geometric contraction rate of the two-block Gibbs sampler ((Nygren 2020), Claim 2 / Remark 8 / Corollary 1): the total variation distance between the ll-step kernel and the target decays like (λ∗)l(\lambda^*)^l.

Usage

two_block_rate(
  x,
  group,
  x_hyper,
  prior_list_block1,
  prior_list_block2,
  weights = NULL,
  family = gaussian(),
  group_levels = levels(group),
  warn_slow = TRUE
)

## S3 method for class 'two_block_rate'
print(x, tols = c(0.01, 0.001, 1e-06), ...)

two_block_rate_from_pfamily_list(
  x,
  group,
  x_hyper,
  prior_list_block1,
  pfamily_list,
  weights = NULL,
  family = gaussian(),
  group_levels = levels(group),
  warn_slow = TRUE
)

two_block_rate_ing(
  x,
  group,
  x_hyper,
  prior_list_block1,
  prior_list_block2,
  lambda_ing = NULL,
  omega_ing = NULL,
  weights = NULL,
  family = gaussian(),
  group_levels = levels(group),
  warn_slow = TRUE
)

## S3 method for class 'two_block_rate_ing'
print(x, ...)

Arguments

x

Level-1 RE design matrix Z (l2 x p_re), as passed to two_block_rNormal_reg.

group

Grouping factor or row-group partition of length l2.

x_hyper

Named list of group-level design matrices X_k (J x q_k), one per column of x.

prior_list_block1

Block 1 prior: P or Sigma (p_re x p_re); dispersion required for gaussian() when weights is NULL.

prior_list_block2

Named list of Block 2 prior lists (each with P or Sigma, q_k x q_k).

weights

Optional per-observation working weights (length 1 or l2). Default for gaussian(): 1 / prior_list_block1$dispersion. Required for non-Gaussian families.

family

Response family (default gaussian()).

group_levels

Character vector defining group order (default levels(group)); must match the row order of x_hyper when rownames are absent.

warn_slow

If TRUE (default), issue a warning() when lambda_star exceeds 0.95 (strong cross-block coupling and potentially slow convergence). Set FALSE for repeated internal rate evaluations (e.g. pilot upper-bound scans).

tols

For print.two_block_rate: numeric vector of TV tolerances for the implied m_convergence table.

...

Ignored by the print methods.

pfamily_list

Named list of pfamily objects (one per RE component), as in two_block_rNormal_reg.

lambda_ing

For two_block_rate_ing: NULL (no random-effects-precision extension), or a named list (names a subset of names(x_hyper)) as documented in .two_block_S_P11_ing.

omega_ing

For two_block_rate_ing: NULL (no measurement-precision extension), or a list as documented in .two_block_S_P11_ing.

Details

Block convention: the paper's x1x_1 (the block updated second in each sweep) is the Block 2 hyper vector γ\gamma of dimension q=∑kqkq = \sum_k q_k; the paper's x2x_2 is the Block 1 random-effect stack. AA is therefore q×qq \times q and is computed without ever forming the Jpre×JpreJp_{re} \times Jp_{re} Block 1 precision: per group only pre×prep_{re} \times p_{re} solves are needed, followed by a single q×qq \times q symmetric eigendecomposition.

For non-Gaussian families the joint posterior is not normal; supplying IRLS-style weights evaluated at the posterior mode (via the internal two_block_mode_weights() helper used by rGLMM_reg) yields a local-Gaussian heuristic rate (no theorem applies).

Value

For two_block_rate() / two_block_rate_from_pfamily_list(), an object of class "two_block_rate": a list with lambda_star (the Remark 8 rate), eigenvalues (full spectrum a1≥⋯≥aqa_1 \ge \dots \ge a_q in [0,1)[0,1)), m_for_tol (function: iterations needed so that (λ∗)m≤(\lambda^*)^m \le tol), S (P12P22−1P21P_{12}P_{22}^{-1}P_{21}), P11, dims, re_names, gamma_names, group_levels, family, weights_source, and call. For two_block_rate_ing(), see the value description under that function below.

Methods (by generic)

  • print(two_block_rate): Print a "two_block_rate" object (eigenvalues, λ∗\lambda^*, and implied sweeps for common TV tolerances).

Functions

  • two_block_rate_from_pfamily_list(): Convergence rate from a pfamily_list Block~2 spec: thin wrapper that accepts Block~2 priors as pfamily objects. For dNormal components the fixed dispersion is used; for dIndependent_Normal_Gamma components the conservative disp_lower plug-in is used (the rate is then an upper bound over the truncated tau^2 range).

  • two_block_rate_ing(): Extended (λ,Ω)(\lambda,\Omega)-aware local rate diagnostic (not a certified TV bound). Computes the Remark~8-style eigenvalues twice from the same prior_list_block1/prior_list_block2 inputs two_block_rate() would use: once as the plain (γ\gamma,β\beta)-only eigenvalues_base/ lambda_star_base, and once with the joint (γ,β,λING,ΩING)(\gamma,\beta,\lambda_{\mathrm{ING}},\Omega_{\mathrm{ING}}) Hessian extension of inst/BLOCK_GIBBS_ERGODICITY_ING.md (Sections 5–6 / 14) as eigenvalues/lambda_star. The extended matrix is evaluated at a single reference state (lambda_ing/omega_ing) and is state-dependent; it does not set sampler m_convergence. Not exported; call via lmebayesCore:::two_block_rate_ing(). Returns an object of class "two_block_rate_ing" with lambda_star/eigenvalues (extended), lambda_star_base/eigenvalues_base (plain), extended S/P11/P12/P21/P22, ext_names, n_lambda, n_omega, lambda_names, omega_scope, and the usual metadata fields.

  • print(two_block_rate_ing): Print a "two_block_rate_ing" object (base vs extended λ∗\lambda^* and their difference).

From lambda-star to sweeps

λ∗\lambda^* alone gives only the geometric proxy (λ∗)m≤(\lambda^*)^m \le tol, which is loose because it waits for the slowest eigendirection while ignoring that the others converged sweeps earlier. The sampler instead inverts the exact Theorem 3 bound, which sums closed-form per-eigendirection terms di(l)d_i^{(l)} over the full spectrum returned in eigenvalues:

TV(Kl(x(0),⋅), π)  ≤  ∑i=1qdi(l)  +  (λ∗)lD0/(2π),\mathrm{TV}\big(K^l(x^{(0)}, \cdot),\, \pi\big) \;\le\; \sum_{i=1}^{q} d_i^{(l)} \;+\; (\lambda^*)^l \sqrt{D_0 / (2\pi)},

capped at 1, with the eigenvalues taken in ascending order (Lemma 2) and D0D_0 the squared standardized distance of the chain start from the posterior mean. Because rlmerb starts every replicate chain at the exact ICM posterior mean, D0=0D_0 = 0 and the mean term vanishes identically; only the variance-convergence sum has to be certified.

The pipeline is therefore two_block_rate() →\rightarrow two_block_tv_bound() →\rightarrow two_block_l_for_tv(), and the sampler adds one sweep to the result: the bound applies to the block updated second in each sweep (the hyper vector γ\gamma), and the stored Block 1 draw lags by a half-step. Calling print() on the returned object shows all three answers — geometric proxy, Corollary 1 envelope, and Theorem 3 — side by side for a range of tolerances.

Cost grows only logarithmically in 1/1/tol, so certifying an entire n-draw sample at level α\alpha (pass tol = alpha / n) usually costs a handful of extra sweeps. When chains cannot start at the posterior mean — non-Gaussian families, or sampled variance components — a pilot stage supplies a start whose D0D_0 is certified from the pilot size; see two_block_optimize_pilot_cost and vignette("Chapter-B03", package = "lmebayesCore").

References

Nygren KN (2020). “On the Total Variation Distance Between Multivariate Normal Densities with Applications to Two-Block Gibbs Samplers.” Working manuscript. Claims 1–3, Theorem 3, Remarks 3, 5, 8 and 17, and Corollary 1 are cited throughout the lmebayesCore two-block ergodicity code and vignettes.

See Also

two_block_tv_bound, two_block_rNormal_reg, two_block_optimize_pilot_cost; vignette("Chapter-B02", package = "lmebayesCore") for the derivation and vignette("Chapter-B03", package = "lmebayesCore") for the sweep calibration.

glmbayes Simulation Functions rGLMM_Re_Draw(), rGLMM_reg(), rGLMM_sweep(), rLMMNormal_joint_iid(), rLMM_reg, two_block_optimize_pilot_cost(), two_block_rNormal_reg(), two_block_tv_bound()

Examples

## two_block_rate() — Remark 8 contraction rate for a balanced
## random-intercept model, where lambda* has a closed form.
##
## For J groups of n_j observations each, p_re = q = 1, and a diffuse Block 2
## prior,  lambda* = sigma^2 / (sigma^2 + n_j * tau^2).

J      <- 12L
n_j    <- 8L
sigma2 <- 1.0
tau2   <- 0.5

group   <- factor(rep(seq_len(J), each = n_j))
Z       <- matrix(1, J * n_j, 1L, dimnames = list(NULL, "(Intercept)"))
x_hyper <- list("(Intercept)" = matrix(
  1, J, 1L, dimnames = list(levels(group), "(Intercept)")
))

rate <- two_block_rate(
  x                 = Z,
  group             = group,
  x_hyper           = x_hyper,
  prior_list_block1 = list(Sigma = matrix(tau2), dispersion = sigma2),
  prior_list_block2 = list("(Intercept)" = list(mu = 0, Sigma = matrix(100)))
)

c(computed = rate$lambda_star,
  closed_form = sigma2 / (sigma2 + n_j * tau2))

## print() adds the sweeps implied by each bound: the geometric (lambda*)^m
## proxy, the Corollary 1 envelope, and the exact Theorem 3 terms the sampler
## actually uses.
rate

## Larger groups carry more likelihood precision and mix faster; the rate does
## not depend on the number of groups.
lambda_for <- function(n_j) {
  g <- factor(rep(seq_len(J), each = n_j))
  two_block_rate(
    x       = matrix(1, J * n_j, 1L, dimnames = list(NULL, "(Intercept)")),
    group   = g,
    x_hyper = list("(Intercept)" = matrix(
      1, J, 1L, dimnames = list(levels(g), "(Intercept)")
    )),
    prior_list_block1 = list(Sigma = matrix(tau2), dispersion = sigma2),
    prior_list_block2 = list("(Intercept)" = list(mu = 0, Sigma = matrix(100)))
  )$lambda_star
}
round(vapply(c(2L, 8L, 32L), lambda_for, numeric(1)), 4)