Package 'lmebayes'

Title: Bayesian Linear Mixed-Effects Models via Two-Block Gibbs Sampling
Description: Provides Bayesian linear and generalized linear mixed-effects model fitting with near-independent posterior samples. The main functions mirror the 'lme4' lmer() and glmer() interfaces (X/beta for fixed effects, Z/b for random effects, D for the random-effects covariance) while adding prior family specifications for Gaussian, Poisson, binomial, and Gamma models with log-concave likelihoods. Sampling uses efficient two-block Gibbs sampling procedures with parallel chains and theoretically driven stopping rules. Gibbs sampling engines are provided by the 'lmebayesCore' package.
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-02 21:45:27 UTC
Source: https://github.com/knygren/lmebayes

Help Index


lmebayes: Bayesian Linear Mixed-Effects Models via Two-Block Gibbs Sampling

Description

Two-block Gibbs samplers for Bayesian linear and generalized linear mixed-effects models, following lme4 notation. Builds on glmbayes for iid GLM sampling within blocks.

Details

Row-block interfaces include lmbBlock and glmbBlock; mixed-model setup from lme4 formulas via model_setup. Lower-level simulation uses simfunction and envelope utilities from lmebayesCore.

See the package README at https://github.com/knygren/lmebayes for examples.

Vignette index

The vignettes are a curriculum, meant to be read roughly in order. Each assumes the previous ones.

vignette("Chapter-00")

Roadmap: what the package does and which chapter answers which question.

vignette("Chapter-01")

Getting started: one model end to end on lme4::sleepstudy.

vignette("Chapter-02")

From lm/glm to mixed models: pooling, the centered parameterization, and the model-class restrictions.

vignette("Chapter-03")

The prior workflow: model_setup, Prior_Setup_GLMM, pfamily_list.

vignette("Chapter-04")

Linear mixed models with group-level covariates and cross-level moderation.

vignette("Chapter-05")

Reading the output: every accessor and container.

vignette("Chapter-06")

Convergence and tv_tol.

vignette("Chapter-07")

Estimated variance components and the pilot stage.

vignette("Chapter-08")

Observation dispersion, including per-group σj2\sigma^2_j.

vignette("Chapter-09")

Poisson GLMMs.

vignette("Chapter-10")

Binomial GLMMs.

vignette("Chapter-11")

Point estimates without simulation.

vignette("Chapter-12")

Row-block models.

vignette("Chapter-13")

Comparison with lme4 and glmmTMB.

Engine internals, derivations, and convergence theory are documented in the Chapter-B* vignettes of lmebayesCore; the iid GLM stack underneath is documented in glmbayes.

OpenCL startup checks

In interactive sessions, attaching the package with library(lmebayes) may emit a short packageStartupMessage when has_opencl() is FALSE (typical for CRAN binaries) but a GPU or OpenCL stack appears available on the host. OpenCL modelling paths require a source install with OpenCL at compile time; has_opencl() then reports whether that build succeeded. Set options(glmbayes.quiet_opencl_startup = TRUE) to suppress attach notes (recommended for CI and R CMD check).

Author(s)

Kjell Nygren

References

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

See Also

lmerb, model_setup, lmbBlock, glmbBlock; simfunction, EnvelopeBuild; lmb and glmb for fixed-effects-only Bayesian linear and generalized linear models; rlmb and rglmb for iid posterior draws.

Useful links:

Examples

set.seed(333)
## Dobson (1990) Page 93: Randomized Controlled Trial :
counts <- c(18, 17, 15, 20, 10, 20, 25, 13, 12)
outcome <- gl(3, 1, 9)
treatment <- gl(3, 3)
print(d.AD <- data.frame(treatment, outcome, counts))

glm.D93 <- glm(counts ~ outcome + treatment, family = poisson())

## Plain iid GLM fitting engine (rglmb) is not re-exported from lmebayes;
## it lives in glmbayesCore, so we source it from there directly.
ps <- Prior_Setup(counts ~ outcome + treatment, family = poisson())

rglmb.D93 <- glmbayesCore::rglmb(
  n = 200,
  y = ps$y,
  x = as.matrix(ps$x),
  pfamily = dNormal(mu = ps$mu, Sigma = ps$Sigma),
  family = poisson(),
  weights = rep(1, nrow(ps$x))
)
print(rglmb.D93)
summary(rglmb.D93)

Amitriptyline overdose data

Description

Data with information on 17 overdoses of the drug amitriptyline

Usage

data(AMI)

Format

This data frame contains the following columns:

TOT

total TCAD plasma level

AMI

amount of amitriptyline present in the TCAD plasma level

GEN

gender (male = 0, female = 1)

AMT

amount of drug taken at time of overdose

PR

PR wave measurement

DIAP

diastolic blood pressure

QRS

QRS wave measurement

Details

Each row is one overdose episode. Variables include total tricyclic antidepressant level, amitriptyline component, gender, reported amount ingested, and ECG-related measures (PR interval, QRS duration, diastolic blood pressure). The dataset is used in package examples for binomial and related regression; see (Dobson 1990) for analogous generalized linear modelling of clinical outcomes.

References

Dobson A~J (1990). An Introduction to Generalized Linear Models. Chapman and Hall, London.

Examples

############################### Start of AMI dataset example ####################

data(AMI)
summary(AMI)

###############################################################################
## End of AMI dataset example
###############################################################################

Coerce ranef.lmerb objects to long data.frame format

Description

Coerce ranef.lmerb objects to long data.frame format

Usage

## S3 method for class 'ranef.lmerb'
as.data.frame(x, ...)

Arguments

x

An object of class "ranef.lmerb".

...

Ignored.

Value

A data.frame with columns grpvar, term, grp, condval, and condsd (when postVar is available on x).

See Also

ranef.


Bike Sharing Dataset (Processed)

Description

A processed version of the UCI Bike Sharing Dataset (hourly data). The data include derived variables for part of day, quarter, and Fourier terms for cyclic effects of hour and month.

Usage

BikeSharing

Format

A data frame with 17,379 observations and 25 variables (original plus derived):

instant

Record index.

dteday

Date.

season

Season (1: spring, 2: summer, 3: fall, 4: winter).

yr

Year (0: 2011, 1: 2012).

mnth

Month (1–12).

hr

Hour (0–23).

holiday

Whether the day is a holiday (0/1).

weekday

Day of week (0–6).

workingday

Working day (0/1).

weathersit

Weather situation (1–4).

temp

Normalized temperature.

atemp

Normalized feeling temperature.

hum

Normalized humidity.

windspeed

Normalized wind speed.

casual

Count of casual users.

registered

Count of registered users.

cnt

Total count (casual + registered).

hr_num

Hour as numeric (same as hr).

month_num

Month as integer.

part_of_day

Factor: Night (0–5h), Morning (6–11h), Afternoon (12–17h), Evening (18–23h).

quarter

Factor: Q1–Q4.

hr_sin

Sine term for 24-hour cycle.

hr_cos

Cosine term for 24-hour cycle.

mon_sin

Sine term for 12-month cycle.

mon_cos

Cosine term for 12-month cycle.

Source

UCI Machine Learning Repository: Bike Sharing Dataset. https://archive.ics.uci.edu/dataset/275/bike+sharing+dataset

Examples

############################### Start of BikeSharing dataset example ####################

data("BikeSharing")
head(BikeSharing)
dim(BikeSharing)

###############################################################################
## End of BikeSharing dataset example
###############################################################################

Boston housing data with mean-centered predictors

Description

A copy of Boston where all predictors (every column except medv) have been mean-centered (subtract column means, no scaling).

Usage

data("Boston_centered")

Format

A data frame with 506 observations and 14 variables (same names as Boston). See ?MASS::Boston for variable descriptions.

Source

Derived from MASS::Boston. Original data described in Harrison and Rubinfeld (1978); see ?Boston in MASS.

Examples

############################### Boston_centered dataset example ####################

data("Boston_centered")
head(Boston_centered)
summary(Boston_centered)

## Predictors are mean-centered (column means ~0)
predictors <- setdiff(names(Boston_centered), "medv")
colMeans(Boston_centered[predictors])

form <- medv ~
  crim + zn +
  indus + chas + nox + age + dis + rad + tax + ptratio + black + lstat + rm

lm.boston <- lm(form, data = Boston_centered, x = TRUE, y = TRUE)
summary(lm.boston)

###############################################################################
## End of Boston_centered dataset example
###############################################################################

Canadian Automobile Insurance Claims for 1957-1958

Description

The data give the Canadian automobile insurance experience for policy years 1956 and 1957 as of June 30, 1959. The data includes virtually every insurance company operating in Canada and was collated by the Statistical Agency (Canadian Underwriters' Association - Statistical Department) acting under instructions from the Superintendent of Insurance. The data given here is for private passenger automobile liability for non-farmers for all of Canada excluding Saskatchewan.

Usage

data(carinsca)

Format

A data frame with 20 observations on the following 6 variables:

Merit

Merit Rating:
3 - licensed and accident free 3 or more years
2 - licensed and accident free 2 years
1 - licensed and accident free 1 year
0 - all others

Class

1 - pleasure, no male operator under 25
2 - pleasure, non-principal male operator under 25
3 - business use
4 - unmarried owner or principal operator under 25
5 - married owner or principal operator under 25

Insured

Earned car years

Premium

Earned premium in 1000's
(adjusted to what the premium would have been had all cars been written at 01 rates)

Claims

Number of claims

Cost

Total cost of the claim in 1000's of dollars

Details

One could apply Poisson regression to the number of claims and gamma regression to the cost per claim.

Source

Bailey, R. A., and Simon, LeRoy J. (1960). Two studies in automobile insurance ratemaking. ASTIN Bulletin, 192-217.

References

Data downloaded from http://www.statsci.org/data/general/carinsca.html. That site also contains classical Poisson and Gamma regression examples.

Examples

############################### Start of carinsca dataset example ####################

data(carinsca)
str(carinsca)
head(carinsca)

###############################################################################
## End of carinsca dataset example
###############################################################################

Cleveland Heart Disease Dataset

Description

A cleaned version of the Cleveland heart disease dataset from the UCI Machine Learning Repository. This version contains only complete cases and includes a derived binary outcome variable hd indicating the presence ("Yes") or absence ("No") of heart disease.

Usage

data("Cleveland")

Format

A data frame with 297 observations and 15 variables:

age

Age in years (numeric).

sex

Sex (0 = female, 1 = male).

cp

Chest pain type (numeric code 1–4).

trestbps

Resting blood pressure (mm Hg).

chol

Serum cholesterol (mg/dl).

fbs

Fasting blood sugar > 120 mg/dl (1 = true, 0 = false).

restecg

Resting electrocardiographic results (numeric code).

thalach

Maximum heart rate achieved.

exang

Exercise-induced angina (1 = yes, 0 = no).

oldpeak

ST depression induced by exercise relative to rest.

slope

Slope of the peak exercise ST segment.

ca

Number of major vessels colored by fluoroscopy (0–3).

thal

Thalassemia status (numeric code).

num

Original UCI disease score (0–4).

hd

Binary heart disease indicator: "No" (num = 0) or "Yes" (num > 0).

Source

UCI Machine Learning Repository: Heart Disease Data Set. https://archive.ics.uci.edu/dataset/45/heart+disease

Examples

############################### Start of Cleveland dataset example ####################

data("Cleveland")
head(Cleveland)
summary(Cleveland)

###############################################################################
## End of Cleveland dataset example
###############################################################################

Extract combined coefficients (fixed + random) by group

Description

Mirrors coef.merMod: Block~1 stored coefficients (ranef.lmerb) are already on the per-group coefficient scale used internally by the sampler (not lme4-style deviations).

Usage

## S3 method for class 'lmerb'
coef(object, type = c("mean", "mode"), ...)

## S3 method for class 'glmerb'
coef(object, type = c("mean", "mode"), ...)

Arguments

object

An lmerb or glmerb object.

type

"mean" (default when draws are stored) or "mode" (ICM / groupef.mode). When simulate = FALSE, "mean" falls back to "mode" with a warning.

...

Ignored.

Value

A named list (one element per grouping factor) of data.frames with the same layout as coef(merMod).

See Also

coef.merMod, ranef.lmerb.


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

Examples

## dGamma_list() — per-group dGamma from Prior_Setup_GLMM (Ex_13b data)
##
## Requires dispformula = ~school_id so group.ing_prior is per-group.
## 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)

  design0 <- model_setup(form_lmer, data = dat)
  dat <- subset(dat, school_id %in% names(design0$groupef.rank)[design0$groupef.rank])
  dat$school_id <- droplevels(dat$school_id)

  ps <- Prior_Setup_GLMM(
    form_lmer,
    data = dat,
    pop.pwt = 0.01,
    dispformula = ~school_id,
    group.max_disp_perc = 0.8,
    group.dispersion.pwt = 0.1,
    group.alpha_target = NULL
  )

  disp_pf <- dGamma_list(ps)

  ## Group names (school_id levels)
  g <- names(disp_pf)
  utils::head(g, 10L)

  ## Print a subset (recommended when J is large)
  print(disp_pf, groups = c("2", "18", "47"))

  ## Variation: rebuild truncation windows; print one group
  disp_pf_tight <- dGamma_list(ps, max_disp_perc_measurement = 0.9)
  print(disp_pf_tight, groups = "2")

  ## Downstream attributes (not shown by print.dGamma_list)
  names(attributes(disp_pf))
  utils::head(attr(disp_pf, "window_diagnostics"), 3L)
}

Directional Tail Diagnostic

Description

Computes the directional tail probability based on posterior draws and prior mean, using whitening transformation and projection onto the direction of disagreement. This diagnostic identifies directional disagreement between posterior and prior, and is especially useful for visualizing rejection regions in whitened space. The whitening uses Mahalanobis distance (Mahalanobis 1936) in posterior-precision-scaled coordinates.

Usage

directional_tail(fit, mu0 = NULL)

Arguments

fit

A fitted model object of class 'glmb' or 'lmb'

mu0

An optional argument containing a reference vector relative to which the directional tail is computed. Defaults to the prior mean.

Value

An object of class 'directional_tail' containing:

mahalanobis_shift

Measures the standardized Mahalanobis distance between the posterior and prior means, using posterior precision for scaling. In the Gaussian case, this directly determines the directional tail probability via Phi(-||w||).

p_directional

Directional tail probability (proportion of draws in the direction of disagreement)

delta

Mean shift in whitened space

draws

List containing whitened draws, raw draws, and tail flags

References

Mahalanobis PC (1936). “On the generalized distance in statistics.” Proceedings of the National Institute of Sciences of India, 2(1), 49–55.

Nygren K (2025). “Chapter A04: Directional Tail Diagnostics for Prior-Posterior Disagreement.” Vignette in the glmbayes R package. R vignette name: Chapter-A04.

See Also

summary.glmb, anova.glmb


Extract fixed effects from lmerb / glmerb fits

Description

By default returns fixef from the reference lmer/glmer fit (population-level regression coefficients). Use type = "hyper" or "hyper.mean" for Block~2 hyperparameter summaries stored on the Bayesian fit.

Usage

## S3 method for class 'lmerb'
fixef(object, type = c("mer", "hyper", "hyper.mean"), ...)

## S3 method for class 'glmerb'
fixef(object, type = c("mer", "hyper", "hyper.mean"), ...)

Arguments

object

An lmerb or glmerb object.

type

"mer" (default), "hyper" (popef.mode), or "hyper.mean" (popef.means; requires stored draws).

...

Ignored.

Value

A named numeric vector.

See Also

fixef, coef.lmerb.


Fitting Bayesian Generalized Linear Models

Description

glmb is used to fit Bayesian generalized linear models, specified by giving a symbolic descriptions of the linear predictor, the error distribution, and the prior distribution.

Usage

glmb(
  formula,
  family = binomial,
  pfamily = dNormal(mu, Sigma, dispersion = 1),
  n = 1000,
  data,
  weights,
  use_parallel = TRUE,
  use_opencl = FALSE,
  verbose = FALSE,
  subset,
  offset,
  na.action,
  Gridtype = 2,
  n_envopt = NULL,
  start = NULL,
  etastart,
  mustart,
  control = list(...),
  model = TRUE,
  method = "glm.fit",
  x = FALSE,
  y = TRUE,
  contrasts = NULL,
  ...
)

Arguments

formula

an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted. The details of model specification are given under ‘Details’.

family

a description of the error distribution and link function to be used in the model. For glm this can be a character string naming a family function, a family function or the result of a call to a family function. For glm.fit only the third option is supported. (See family for details of family functions.)

pfamily

a description of the prior distribution and associated constants to be used in the model. This should be a pfamily function (see pfamily for details of pfamily functions).

n

number of draws to generate. If length(n) > 1, the length is taken to be the number required.

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.

use_parallel

Logical. Whether to use parallel processing during simulation.

use_opencl

Logical. Whether to use OpenCL acceleration during Envelope construction.

verbose

Logical. Whether to print progress messages.

subset

an optional vector specifying a subset of observations to be used in the fitting process.

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 documentation for model.offset at model.extract.

na.action

a function which indicates what should happen when the data contain NAs. The default is set by the na.action setting of options, and is na.fail if that is unset. The ‘factory-fresh’ default is stats{na.omit}. Another possible value is NULL, no action. Value stats{na.exclude} can be useful.

Gridtype

an optional argument specifying the method used to determine the number of tangent points used to construct the enveloping function.

n_envopt

Effective sample size passed to EnvelopeOpt for grid construction. Defaults to match n. Larger values encourage tighter envelopes.

start

starting values for the parameters in the linear predictor.

etastart

starting values for the linear predictor.

mustart

starting values for the vector of means.

control

a list of parameters for controlling the fitting process. For glm.fit this is passed to glm.control.

model

a logical value indicating whether model frame should be included as a component of the returned value.

method

the method to be used in fitting the model. The default method "glm.fit" uses iteratively reweighted least squares (IWLS): the alternative "model.frame" returns the model frame and does no fitting.

User-supplied fitting functions can be supplied either as a function or a character string naming a function, with a function which takes the same arguments as glm.fit. If specified as a character string it is looked up from within the stats namespace.

x, y

For glm: logical values indicating whether the response vector and model matrix used in the fitting process should be returned as components of the returned value.

For glm.fit: x is a design matrix of dimension n * p, and y is a vector of observations of length n.

contrasts

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

...

For glm: arguments to be used to form the default control argument if it is not supplied directly.

For weights: further arguments passed to or from other methods.

Details

The function glmb is a Bayesian version of the classical glm function. The original R implementation of glm was written by Simon Davies (under Ross Ihaka at the University of Auckland) and has since been extensively rewritten by members of the R Core Team; its design was inspired by the S function described in (Hastie and Pregibon 1992), which in turn relies on the formula framework described in (Wilkinson and Rogers 1973).

Setup (including the use of formulas and families) mirrors that of glm but adds a required pfamily argument to specify the prior distribution. The design of the pfamily family of functions was created by Kjell Nygren and is modeled on how glm uses family to specify the likelihood.

For any implemented combination of family, link, and pfamily, glmb generates independent draws from the posterior density- no MCMC chains are required. Results can be printed or summarized with methods that mirror those for glm (e.g.\ print.glmb, summary.glmb), as well as all the usual glm/lm generics (predict, residuals, etc.).

A helper, Prior_Setup, assists users in choosing prior parameters. It ships with sensible defaults but also allows full customization. In particular, the default for dNormal is a reparameterization of Zellner's g-prior (Zellner 1986).

Currently supported response families are gaussian (identity link), poisson and quasipoisson (log link), gamma (log link), and binomial and quasibinomial (logit, probit, cloglog). All families support a dNormal prior; the Gaussian family also offers dNormalGamma and dIndependent_Normal_Gamma. Two conjugate pfamilies add closed-form IID sampling for intercept-only models with an identity link: dBeta for binomial(link = "identity") (Beta–Binomial conjugacy) and dGamma(Inv_Dispersion = FALSE) for poisson(link = "identity") and Gamma(link = "identity") (Gamma–Poisson and Gamma–Gamma rate conjugacy). For dispersion estimation with fixed coefficients, dGamma (default Inv_Dispersion = TRUE) places a Gamma prior on the inverse dispersion for Gaussian and Gamma(log) models.

For the Gaussian family, draws under dNormal and dNormalGamma come from posterior distributions resulting from conjugate prior distributions (Raiffa and Schlaifer 1961). For all other priors or response families, we use an accept-reject sampler built on the likelihood-subgradient envelope method (Nygren and Nygren 2006). The Gridtype argument controls how many tangent points are used in the envelope-trading off envelope tightness against construction cost-and iters reports candidate counts before acceptance.

By default, glmb draws n = 1000 samples, uses parallel CPU simulation, and-if use_opencl = TRUE-GPU-accelerated envelope building. "glmb" comes with many of the same kinds of method functions that come with "glm" and "lm", so you can still call extractAIC, fitted.values, or any other standard method.

The lmb function is a Bayesian version of the lm function that can be used to estimate models from the Gaussian family without the need for a family argument.

rglmb and rlmb are functions with more minimalistic interfaces for estimating the same models without most of the internal overhead (these functions are called internally by glmb and lmb). The reduced overhead may be beneficial for Gibbs sampling implementations.

Value

glmb returns an object of class "glmb". The function summary (i.e., summary.glmb) can be used to obtain or print a summary of the results. The generic accessor functions coefficients, fitted.values, residuals, and extractAIC can be used to extract various useful features of the value returned by glmb.

An object of class "glmb" is a list containing at least the following components:

glm

an object of class "glm" containing the output from a call to the function glm

coefficients

a matrix of dimension n by length(mu) with one sample in each row

coef.means

a vector of length(mu) with the estimated posterior mean coefficients

coef.mode

a vector of length(mu) with the estimated posterior mode coefficients

dispersion

Either a constant provided as part of the call, or a vector of length n with one sample in each row.

Prior

A list with the priors specified for the model in question. Items in list may vary based on the type of prior

fitted.values

a matrix of dimension n by length(y) with one sample for the mean fitted values in each row

family

the family object used.

linear.predictors

an n by length(y) matrix with one sample for the linear fit on the link scale in each row

deviance

an n by 1 matrix with one sample for the deviance in each row

pD

An Estimate for the effective number of parameters

Dbar

Expected value for minus twice the log-likelihood function

Dthetabar

Value of minus twice the log-likelihood function evaluated at the mean value for the coefficients

DIC

Estimated Deviance Information criterion

prior.weights

a vector of weights specified or implied by the model

y

a vector with the dependent variable

x

a matrix with the implied design matrix for the model

model

if requested (the default),the model frame

call

the matched call

formula

the formula supplie

terms

the terms object used

data

the data argument

famfunc

Family functions used during estimation process

iters

an n by 1 matrix giving the number of candidates generated before acceptance for each sample.

contrasts

(where relevant) the contrasts used.

xlevels

(where relevant) a record of the levels of the factors used in fitting

digits

the number of significant digits to use when printing.

In addition, non-empty fits will have (yet to be implemented) components qr, R and effects relating to the final weighted linear fit for the posterior mode. Objects of class "glmb" are normall of class c("glmb","glm","lm"), that is inherit from classes glm and lm and well-designed methods from those classed will be applied when appropriate.

If a binomial glmb model was specified by giving a two-column response, the weights returned by prior.weights are the total number of cases (factored by the supplied case weights) and the component of y of the result is the proportion of successes.

References

Hastie T~J, Pregibon D (1992). “Generalized Linear Models.” In Chambers J~M, Hastie T~J (eds.), Statistical Models in S, chapter 6. Wadsworth & Brooks/Cole, Belmont, CA.

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

Raiffa H, Schlaifer R (1961). Applied Statistical Decision Theory. Clinton Press, Inc., Boston.

Wilkinson GN, Rogers CE (1973). “Symbolic Descriptions of Factorial Models for Analysis of Variance.” Applied Statistics, 22(3), 392–399. doi:10.2307/2346786.

Zellner A (1986). “On Assessing Prior Distributions and Bayesian Regression Analysis with g‐Prior Distributions.” In Goel P~K, Zellner A (eds.), Bayesian Inference and Decision Techniques: Essays in Honor of Bruno de Finetti, volume 6 of Studies in Bayesian Econometrics and Statistics, 233–243. Elsevier. Dobson A~J (1990). An Introduction to Generalized Linear Models. Chapman and Hall, London.

See Also

lm, glm, family, formula for classical modeling functions, family objects, and formula syntax

pfamily for documentation of pfamily functions used to specify priors.

Prior_Setup, Prior_Check for functions used to initialize and to check priors,

EnvelopeBuild for envelope construction methods.

Further reading: (Nygren and Nygren 2006); (Nygren 2025, 2025); OpenCL/GPU: (Nygren 2025, 2025).

summary.glmb, predict.glmb, residuals.glmb, simulate.glmb, extractAIC.glmb, dummy.coef.glmb and methods(class="glmb") for glmb and the methods and generic functions for classes glm and lm from which class glmb inherits.

glmbayes Modeling Functions lmb()

Examples

set.seed(333)
## Dobson (1990) Page 93: Randomized Controlled Trial :
counts <- c(18,17,15,20,10,20,25,13,12)
outcome <- gl(3,1,9)
treatment <- gl(3,3)
print(d.AD <- data.frame(treatment, outcome, counts))

## Classical Model
glm.D93 <- glm(counts ~ outcome + treatment, family = poisson(link=log))
summary(glm.D93)

## Poisson Prior and Model
ps=Prior_Setup(counts ~ outcome + treatment,family = poisson())
mu=ps$mu
V=ps$Sigma

# Step 2: Call the glmb function
glmb.D93<-glmb(counts ~ outcome + treatment, family=poisson(), 
               pfamily=dNormal(mu=mu,Sigma=V))
summary(glmb.D93)


# Menarche Binomial Data Example 
data(menarche,package="MASS")
Age2=menarche$Age-13

## Classical Model

glm.out<-glm(cbind(Menarche, Total-Menarche) ~ Age2, family=binomial(logit), data=menarche)
summary(glm.out)

## Logit Prior and Model
ps1=Prior_Setup(cbind(Menarche, Total-Menarche) ~ Age2,family=binomial(logit), data=menarche)

glmb.out1<-glmb(cbind(Menarche, Total-Menarche) ~ Age2,
family=binomial(logit),pfamily=dNormal(mu=ps1$mu,Sigma=ps1$Sigma), data=menarche)
summary(glmb.out1)

## Posterior mean fitted probabilities on the response scale (see also
## \code{vignette("Chapter-05", package = "glmbayes")})
require(graphics)
pred1 <- predict(glmb.out1, type = "response")
pred1_m <- colMeans(pred1)
plot(
  Menarche / Total ~ Age,
  data = menarche,
  main = "Proportion with menarche (data and posterior mean fit)"
)
lines(menarche$Age, pred1_m, col = "blue", lwd = 2)

## Probit Prior and Model
ps2=Prior_Setup(cbind(Menarche, Total-Menarche) ~ Age2,family=binomial(probit), data=menarche)

glmb.out2<-glmb(cbind(Menarche, Total-Menarche) ~ Age2,
                family=binomial(probit),pfamily=dNormal(mu=ps2$mu,Sigma=ps2$Sigma), data=menarche)
summary(glmb.out2)

## clog-log Prior and Model
ps3=Prior_Setup(cbind(Menarche, Total-Menarche) ~ Age2,family=binomial(cloglog), data=menarche)

glmb.out3<-glmb(cbind(Menarche, Total-Menarche) ~ Age2,
                family=binomial(cloglog),pfamily=dNormal(mu=ps3$mu,Sigma=ps3$Sigma), data=menarche)
summary(glmb.out3)

## Comparison of DIC Statistics

DIC_Out=rbind(extractAIC(glmb.out1),extractAIC(glmb.out2),extractAIC(glmb.out3))
rownames(DIC_Out)=c("logit","probit","clog-log")
colnames(DIC_Out)=c("pD","DIC")
DIC_Out

### Gamma regression

data(carinsca)
carinsca$Merit <- ordered(carinsca$Merit)
carinsca$Class <- factor(carinsca$Class)
oldopt <- options(contrasts = c("contr.treatment", "contr.treatment"))
Claims=carinsca$Claims
Insured=carinsca$Insured
Merit=carinsca$Merit
Class=carinsca$Class
Cost=carinsca$Cost


out <- glm(Cost/Claims~Merit+Class,family=Gamma(link="log"),weights=Claims,x=TRUE)
summary(out)
disp=gamma.dispersion(out)
ps=Prior_Setup(Cost/Claims~Merit+Class,family=Gamma(link="log"),weights=Claims)
mu=ps$mu
V=ps$Sigma

out3 <- glmb(Cost/Claims~Merit+Class,family=Gamma(link="log"),
             pfamily=dNormal(mu=mu,Sigma=V,dispersion=disp),weights=Claims)

summary(out)
summary(out3)

options(oldopt)


## glmb with dGamma prior (dispersion-only; coefficients fixed)
ctl <- c(4.17, 5.58, 5.18, 6.11, 4.50, 4.61, 5.17, 4.53, 5.33, 5.14)
trt <- c(4.81, 4.17, 4.41, 3.59, 5.87, 3.83, 6.03, 4.89, 4.32, 4.69)
group <- gl(2, 10, 20, labels = c("Ctl", "Trt"))
weight <- c(ctl, trt)
ps_dg <- Prior_Setup(weight ~ group, family = gaussian())
rate_dg <- if (!is.null(ps_dg$rate_gamma)) ps_dg$rate_gamma else ps_dg$rate
out_glmb_dGamma <- glmb(weight ~ group, family = gaussian(),
  pfamily = dGamma(shape = ps_dg$shape, rate = rate_dg, beta = ps_dg$coefficients))
summary(out_glmb_dGamma)

Fitting Blocked Bayesian Generalized Linear Models

Description

Fits one glmb per observation block. Same row partition as lmbBlock, but supports GLM family objects. Counterpart to lmbBlock; see summary.bglmb for the summary method and rNormalGLM_reg_group for Gibbs sampling.

Usage

glmbBlock(
  formula,
  block,
  family = gaussian(),
  pfamily = NULL,
  pfamily_list = NULL,
  n = 1000,
  data,
  subset,
  weights,
  na.action,
  offset,
  contrasts = NULL,
  model = TRUE,
  x = FALSE,
  y = TRUE,
  Gridtype = 2,
  n_envopt = NULL,
  use_parallel = TRUE,
  use_opencl = FALSE,
  verbose = FALSE,
  ...
)

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

Arguments

formula

an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted. The details of model specification are given under ‘Details’.

block

Block 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. For glm this can be a character string naming a family function, a family function or the result of a call to a family function. For glm.fit only the third option is supported. (See family for details of family functions.)

pfamily

Recycled to all blocks, or use pfamily_list of length k.

pfamily_list

Optional list of pfamily objects, one per block.

n

number of draws to generate. If length(n) > 1, the length is taken to be the number required.

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 lm is called.

subset

an optional vector specifying a subset of observations to be used in the fitting process.

weights

an optional vector of weights to be used in the fitting process. Should be NULL or a numeric vector. If non-NULL, weighted least squares is used with weights weights (that is, minimizing sum(w*e^2)); otherwise ordinary least squares is used. See also ‘Details’,

na.action

a function which indicates what should happen when the data contain NAs. The default is set by the na.action setting of options, and is na.fail if that is unset. The ‘factory-fresh’ default is stats{na.omit}. Another possible value is NULL, no action. Value stats{na.exclude} can be useful.

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 or matrix of extents matching those of the response. One or more offset terms can be included in the formula instead or as well, and if more than one are specified their sum is used. See model.offset.

contrasts

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

model, x, y

For glmbBlock, logicals passed to each block glmb fit (see glm). For print, x is the "bglmb" object.

Gridtype

an optional argument specifying the method used to determine the number of tangent points used to construct the enveloping function.

n_envopt

Effective sample size passed to EnvelopeOpt for grid construction. Defaults to match n. Larger values encourage tighter envelopes.

use_parallel

Logical. Whether to use parallel processing during simulation.

use_opencl

Logical. Whether to use OpenCL acceleration during Envelope construction.

verbose

Logical. Whether to print progress messages.

...

For lm(): additional arguments to be passed to the low level regression fitting functions (see below).

digits

Number of significant digits to use when printing.

Value

A named list of class "bglmb" (list of "glmb" fits from glmbayes).

Functions

  • glmbBlock(): glmb fit per row block.

See Also

glmbayes Modeling Functions lmbBlock()


Fit a Bayesian generalized linear mixed-effects model (GLMM) to data, via two-Block Gibbs sampling

Description

Bayesian generalized linear mixed-effects model fit

Usage

glmerb(
  formula,
  data = NULL,
  family = gaussian(),
  pfamily_list,
  dispersion_ranef = NULL,
  dispformula = ~1,
  n = 1000L,
  gap_tol = 0.0196,
  mode_gap_max = 1,
  tv_tol = 0.01,
  simulate = TRUE,
  REML = TRUE,
  control = NULL,
  start = NULL,
  verbose = 0L,
  subset,
  weights,
  na.action,
  offset,
  contrasts = NULL,
  devFunOnly = FALSE,
  fixef = NULL,
  progbar = FALSE,
  sim_method = "DEFAULT",
  ...
)

## S3 method for class 'glmerb'
print(
  x,
  digits = max(3L, getOption("digits") - 3L),
  sweep_history = FALSE,
  sweep_history_stage = c("main", "pilot", "both"),
  max_sweeps = Inf,
  ...
)

Arguments

formula

Mixed-model formula (single grouping factor; same constraints as model_setup).

data

Data frame containing all variables in formula.

family

A family object describing the response distribution and link. Defaults to gaussian().

pfamily_list

Required named list of pfamily objects, one per random-effect coefficient (names must match the random-effect coefficient names, any order). Supplies the Block~2 hyperpriors (mu, Sigma) and the Block~1 random-effect variances τk2\tau^2_k. dNormal components treat τk2\tau^2_k (the pfamily dispersion) as known and make conjugate γk\gamma_k draws. dIndependent_Normal_Gamma components place a Gamma prior on the Block~2 precision 1/τk21/\tau^2_k: Block~2 then makes a joint (γk,τk2)(\gamma_k, \tau^2_k) draw via the likelihood-subgradient envelope sampler (rindepNormalGamma_reg), and the sampled τk2\tau^2_k feeds back into the Block~1 prior precision. ING components must supply both truncation bounds: each τk2\tau^2_k draw is hard-truncated to [disp_lower, disp_upper], fixed across all inner Gibbs sweeps, with disp_lower doubling as the conservative τk2\tau^2_k plug-in for the eigenvalue / TV calibration (smaller τ2\tau^2 increases the contraction rate λ∗\lambda^*, so the bound holds for every dispersion in the truncated support). They must also satisfy the prior-vs-data guard nprior≤Jn_{\mathrm{prior}} \le J (pwt_dispersion ≤0.5\le 0.5). Typically built with pfamily_list from a Prior_Setup_GLMM object.

dispersion_ranef

Observation-level measurement dispersion, treated as known during sampling. For families with a dispersion parameter (e.g. gaussian()): a positive scalar, a single pooled dGamma(), or (with dispformula = ~<group_name>) a dGamma_list(...) or a named numeric vector of positive, fixed per-group values (names must match the random-effects grouping factor's levels exactly). Must be NULL (default) for poisson() and binomial(). Typically Prior_Setup_GLMM(...)$dispersion_ranef. Which shapes are accepted depends on dispformula (see below). The fixed numeric vector shape currently only takes effect for family = gaussian() (shared code path with lmerb); observation-level dispersion is not yet wired into the sampler for the other dispersion families (Gamma(), quasipoisson(), quasibinomial()).

dispformula

One-sided formula selecting the measurement-dispersion structure: ~1 (default, pooled) requires dispersion_ranef to be a fixed scalar, NULL, or a single (pooled) dGamma(); ~<group_name>, matching the random-effects grouping factor exactly, requires dispersion_ranef to be a dGamma_list(...) or a named numeric vector, and errors for families without a dispersion parameter. ~1 never fits an extra reference model. ~<group_name> with a dGamma_list(...) additionally requires a glmmTMB reference fit (glmmTMB must be installed), stored as dispersion_fit (reused from dispersion_ranef's "dispersion_fit" attribute when dispersion_ranef was built via dGamma_list(Prior_Setup_GLMM(..., dispformula = dispformula)), rather than re-fitting glmmTMB); ~<group_name> with a fixed numeric vector never fits one, since the per-group dispersion is directly user-supplied, not a prior to calibrate. glmer is always the plain glmer fit regardless of dispformula.

n

Number of iid draws per group (default 1000L, as in lmb).

gap_tol

Legacy mode–mean gap tolerance. When tv_tol is NULL, the number of pilot chains is derived as ceiling((qnorm(0.975) / gap_tol)^2) (default gap_tol = 0.0196 gives n_pilot = 10000). When tv_tol is set (default), n_pilot is instead chosen by two_block_optimize_pilot_cost to minimize total inner-sweep cost. Set NULL to skip the pilot unless tv_tol is set. Ignored for gaussian() without ING Block~2 components.

mode_gap_max

Maximum per-coordinate mode–mean gap (in posterior standard deviation units) used to calibrate pilot inner sweeps (default 1.0). Applies only to non-Gaussian families when gap_tol is not NULL. The pilot chains start at the ICM mode, which is at Mahalanobis distance Dmax⁡=p × mode_gap_maxD_{\max} = \sqrt{p}\,\times\,\texttt{mode\_gap\_max} from the posterior mean (assuming mode_gap_max SDs per coordinate across pp fixed-effect dimensions). The number of pilot sweeps is the smallest ll satisfying erf1(0.5 λ∗l Dmax⁡/2)≤tv_tol\mathrm{erf}_1(0.5\,\lambda^{*l}\,D_{\max}/\sqrt{2}) \le \texttt{tv\_tol} (Nygren 2020, Theorem 3 mean-shift term), floored at m_min. Set mode_gap_max = NULL to fall back to the Theorem~3 minimum sweep count. Ignored for family = gaussian() without ING Block~2 components.

tv_tol

Total variation tolerance per stored draw, in (0, 1) (default 0.01). For family = gaussian() the joint posterior is exactly multivariate normal and the number of inner Gibbs sweeps per stored draw is calibrated exactly as in lmerb (Nygren 2020, Theorem 3). For non-Gaussian families the same calibration is applied to the local-Gaussian approximation of the posterior at its mode: per-observation likelihood precisions are evaluated at the ICM posterior mode (two_block_mode_weights() in lmebayesCore) and fed to two_block_rate_from_pfamily_list. The derived sweep count is then the minimum number of iterations required to converge to that hypothetical multivariate normal approximation – a lower bound for the true (non-normal) posterior, not a guarantee.

simulate

Logical (default TRUE). Passed through to rlmerb. When TRUE the two-block Gibbs sampler is run for n iterations and posterior draws are stored. When FALSE, rlmerb returns fixed-VC plug-in point estimates only (exact joint Gaussian posterior mean). Simulation-only fields (groupef, popef.means, popef) are NULL when simulate = FALSE.

REML

Logical; passed to model_setup.

control

Optional glmerControl settings passed to the reference glmer fit. Defaults to NULL (lme4 defaults). When family = gaussian(), lme4's glmer shortcut to lmer does not accept an explicit glmerControl; leave control = NULL or pass lmerControl.

start

Optional starting values; passed to model_setup.

verbose

Verbosity flag; passed to model_setup.

subset

Optional subset; passed to model_setup.

weights

Optional weights; passed to model_setup.

na.action

Missing-data handler; passed to model_setup.

offset

Optional offset; passed to model_setup.

contrasts

Optional contrasts; passed to model_setup.

devFunOnly

If TRUE, return deviance function only; passed to model_setup.

fixef

Optional named list of hyper-parameter vectors (Block 2 state). When NULL (default), iter-0 means are taken from the pfamily_list prior means.

progbar

Logical. When TRUE, show text progress bars during pilot and main replicate sampling inside rglmerb. Default FALSE.

sim_method

Sampling engine: "DEFAULT" or "TWO_BLOCK_GIBBS". Only changes behavior for family = gaussian() with fixed dispersion_ranef (a scalar or a named per-group vector) and all-dNormal() pfamily_list components (known variance components) – see lmerb's sim_method for details. Every other glmerb model (non-Gaussian families, or any dIndependent_Normal_Gamma component, or a sampled variance component) only has the two-block Gibbs engine, so both values behave identically there.

...

Reserved for future use.

x

Object of class "glmerb".

digits

Number of significant digits to use when printing.

sweep_history

If TRUE, print stored Block~2 sweep history (see $sweep_history) after the usual summary.

sweep_history_stage

Which stage to print when sweep_history = TRUE: "main", "pilot", or "both".

max_sweeps

Passed to print() on sweep-history objects; limits how many inner sweeps are shown.

Details

Draft entry point for lmebayes GLMM models with a glmer-like interface, analogous to lmerb for Gaussian responses and to glmb for fixed-effects GLMs.

It takes the same arguments as lmerb plus family. With family = gaussian() it fits the same model as lmerb, differing only in that the embedded reference fit comes from glmer rather than lmer.

The model being estimated. For group j=1,…,Jj = 1, \dots, J with link gg,

yj∣βj∼family(g−1(Djβj)),βj∣γ,Ψ∼N(Wjγ,  Ψ),y_j \mid \beta_j \sim \mathrm{family}\big(g^{-1}(D_j \beta_j)\big), \qquad \beta_j \mid \gamma, \Psi \sim N(\mathcal{W}_j \gamma,\; \Psi),

where γ\gamma collects the level-2 fixed effects, Ψ\Psi is diagonal with entries τk2\tau^2_k (one per random-effect coefficient), and Wj\mathcal{W}_j is built by model_setup from the level-2 predictors. As in lmerb, only a single grouping factor and uncorrelated (||) random-effect terms are supported. Marginalizing recovers the usual glmer form with Xj=DjWjX_j = D_j \mathcal{W}_j, Zj=DjZ_j = D_j, and mean-zero random effects bj=βj−Wjγb_j = \beta_j - \mathcal{W}_j \gamma.

Supported families are gaussian(), poisson(), binomial(), and Gamma() with the standard links. What they have in common is a log-concave likelihood, which is the condition the envelope accept-reject sampler requires (Nygren and Nygren 2006).

Because the link is applied within groups, βj\beta_j and γ\gamma live on the link scale — log rates for poisson(), log-odds for binomial() — so any summary on the response scale must apply the inverse link to the draws rather than to their averages.

What the priors decide. pfamily_list carries one component per random-effect coefficient and declares whether the between-group variance τk2\tau^2_k is known (dNormal) or estimated (dIndependent_Normal_Gamma, under a Gamma prior on 1/τk21/\tau^2_k truncated to [disp_lower, disp_upper]). dispersion_ranef is the residual variance and applies to gaussian() only; it must be NULL for poisson() and binomial(), which have no free dispersion parameter. Both are normally built from one Prior_Setup_GLMM object.

What the returned draws are. As in lmerb, glmerb runs n independent replicate chains and stores one draw from each, taken at that chain's final sweep. The draws are therefore mutually independent — an iid sample, not an autocorrelated trace — so ordinary Monte Carlo standard errors apply and there is nothing to thin or diagnose for autocorrelation.

The accompanying guarantee is weaker here than for a Gaussian response, and the difference matters. With family = gaussian() and fixed variances the posterior is exactly normal and the sweep count is certified. A Poisson or binomial posterior is not normal, so the same calibration is applied at local curvature near the posterior mode. The resulting m_convergence is a well-motivated sweep budget rather than a proof, and tv_tol should be read that way. Tightening it is cheap: cost grows only logarithmically in 1/1/tv_tol.

The same fact — that the mode of a skewed posterior is not its mean — is why a short pilot stage runs first. Its cross-chain average gives the main chains a better starting point than the mode does, which shortens the main stage. Pilot draws are diagnostic only and are never part of the returned sample. gap_tol and mode_gap_max are legacy controls used when tv_tol is NULL.

Point estimates (simulate = FALSE). simulate = FALSE returns the joint posterior mode by iterated conditional modes (the exact Gaussian mean when family = 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.

Value

Object of class "glmerb": same fixef.* structure as "lmerb", including sigma2 and sigma2.mean for family = gaussian() (see lmerb), with additional family, glmer (reference glmer fit, always the plain pooled-dispersion fit regardless of dispformula), dispformula (as supplied), dispersion_fit (NULL unless dispformula requests per-group dispersion, in which case the diagnostic-only glmmTMB fit), fixef.init (main-chain start from pilot colMeans when a pilot runs; NULL when no pilot runs), pilot_chisq (Hotelling chi-squared test of pilot mean vs ICM mode), gap_tol, and mode_gap_max.

References

Nygren, K. (2020). On the total variation distance between multivariate normal densities with applications to two-block Gibbs samplers. Unpublished manuscript.

Nygren, K. N. and Nygren, L. M. (2006). Likelihood Subgradient Densities. Journal of the American Statistical Association 101(475), 1144–1156.

Jones, G. L. and Hobert, J. P. (2001). Honest exploration of intractable probability distributions via Markov chain Monte Carlo. Statistical Science 16, 312–334.

See Also

lmerb, glmerb_posterior_mode, glmb; demo for the full sampling workflow (demo("Ex_14_glmerb_airbnb_small", package = "lmebayes")).

Examples

## glmerb: neighborhood random effects on bayesrules::airbnb_small (Poisson)
##
## Fast man-page example: prior setup, glmer reference fit, and ICM posterior
## mode only (simulate = FALSE).  No Gibbs draws — suitable for R CMD check.
##
## The full sampling workflow (pilot stage, main draws, centering diagnostics)
## is preserved as a demo:
##   demo("Ex_14_glmerb_airbnb_small", package = "lmebayes")

if (requireNamespace("bayesrules", quietly = TRUE)) {

  data(airbnb_small, package = "bayesrules")
  dat <- airbnb_small
  dat$rating_c <- dat$rating - mean(dat$rating, na.rm = TRUE)
  dat$walk_c   <- dat$walk_score - mean(dat$walk_score, na.rm = TRUE)
  dat <- dat[complete.cases(dat[, c("reviews", "rating_c", "walk_c",
                                    "neighborhood")]), ]
  dat$neighborhood <- droplevels(factor(dat$neighborhood))

  form <- reviews ~ walk_c + rating_c + (1 + rating_c || neighborhood)

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

  fit <- glmerb(
    form,
    data         = dat,
    family       = poisson(),
    pfamily_list = pfamily_list(ps),
    simulate     = FALSE
  )

  lmebayes:::print_coef_means(fit)
  print(fit)
  lme4::fixef(fit$glmer)
}

OpenCL compile-time status for lmebayes

Description

Returns whether this lmebayes installation was built with OpenCL support (link-time / USE_OPENCL). This is independent of has_opencl() in opencltools, which probes the host runtime (ICD, drivers, headers).

For workstation diagnostics (drivers, PATH, ICD), use diagnose_glmbayes() from opencltools. For kernel loading from inst/cl/, use load_kernel_source() with the appropriate package argument once opencltools is installed.

Usage

has_opencl()

Value

Logical scalar.

See Also

opencltools, glmbayes


Fitting Bayesian Linear Models

Description

lmb is used to fit Bayesian linear models, specified by giving a symbolic descriptions of the linear predictor and the prior distribution.

Usage

lmb(
  formula,
  pfamily,
  n = 1000,
  data,
  subset,
  weights,
  na.action,
  method = "qr",
  model = TRUE,
  x = TRUE,
  y = TRUE,
  qr = TRUE,
  singular.ok = TRUE,
  contrasts = NULL,
  offset,
  Gridtype = 2,
  n_envopt = NULL,
  use_parallel = TRUE,
  use_opencl = FALSE,
  verbose = FALSE,
  ...
)

Arguments

formula

an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted. The details of model specification are given under ‘Details’.

pfamily

a description of the prior distribution and associated constants to be used in the model. For a single-response formula this should be a single pfamily object. For a multi-response formula (e.g. cbind(y1, y2) ~ x) this must be a list of pfamily objects with exactly one entry per response column; passing a single pfamily object is an error.

n

number of draws to generate. If length(n) > 1, the length is taken to be the number required.

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 lm is called.

subset

an optional vector specifying a subset of observations to be used in the fitting process.

weights

an optional vector of weights to be used in the fitting process. Should be NULL or a numeric vector. If non-NULL, weighted least squares is used with weights weights (that is, minimizing sum(w*e^2)); otherwise ordinary least squares is used. See also ‘Details’,

na.action

a function which indicates what should happen when the data contain NAs. The default is set by the na.action setting of options, and is na.fail if that is unset. The ‘factory-fresh’ default is stats{na.omit}. Another possible value is NULL, no action. Value stats{na.exclude} can be useful.

method

the method to be used in fitting the classical model during a call to glm. The default method glm.fit uses iteratively reweighted least squares (IWLS): the alternative "model.frame" returns the model frame and does no fitting. User-supplied fitting functions can be supplied either as a function or a character string naming a function, with a function which takes the same arguments as glm.fit. If specified as a character string it is looked up from within the stats namespace.

model, x, y, qr

logicals. If TRUE the corresponding components of the fit (the model frame, the model matrix, the response, the QR decomposition) are returned.

singular.ok

logical. If FALSE (the default in S but not in R) a singular fit is an error.

contrasts

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

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 or matrix of extents matching those of the response. One or more offset terms can be included in the formula instead or as well, and if more than one are specified their sum is used. See model.offset.

Gridtype

an optional argument specifying the method used to determine the number of tangent points used to construct the enveloping function.

n_envopt

Effective sample size passed to EnvelopeOpt for grid construction. Defaults to match n. Larger values encourage tighter envelopes.

use_parallel

Logical. Whether to use parallel processing during simulation.

use_opencl

Logical. Whether to use OpenCL acceleration during Envelope construction.

verbose

Logical. Whether to print progress messages.

...

For lm(): additional arguments to be passed to the low level regression fitting functions (see below).

Details

The function lmb is a Bayesian extension of the classical lm function. It retains the familiar formula interface and model setup used in lm, while introducing posterior simulation and prior specification via the pfamily argument. Internally, lmb calls lm to obtain the classical least squares fit, then generates independent draws from the posterior distribution using either multivariate normal simulation (for Gaussian priors) or accept-reject sampling via likelihood-subgradient envelopes (Nygren and Nygren 2006).

The symbolic formula interface follows (Wilkinson and Rogers 1973), and the overall design of lm was inspired by the S system (Chambers 1992). lmb comes with many of the same types of generic methods that are available to lm and glm, including predict, residuals, extractAIC, and summary. Many of these are inherited from glmb.

Prior specification is handled via the pfamily argument, which defines the prior mean, covariance, and dispersion. The design of the pfamily family of functions was created by Kjell Nygren and is modeled on how glm uses family to specify the likelihood. A helper function, Prior_Setup, assists users in choosing prior parameters. It ships with sensible defaults but also allows full customization. All models support the dNormal prior; the Gaussian family also supports dNormalGamma and dIndependent_Normal_Gamma, which allow for more flexible prior structures including independent priors on variance components.

Posterior draws are generated using the prior specification provided via pfamily. For Gaussian models with conjugate priors, draws are obtained directly from the posterior distribution using standard simulation procedures for multivariate normal densities (Raiffa and Schlaifer 1961). For non-conjugate setups, the function uses envelope-based accept-reject sampling, where the Gridtype parameter controls the granularity of the envelope construction. The number of candidates generated before acceptance is returned in the iters component.

The output includes both the classical lm fit and Bayesian diagnostics such as the Deviance Information Criterion (DIC), effective number of parameters (pD), and posterior summaries. The DIC, introduced by (Spiegelhalter et al. 2002), provides a Bayesian analog to AIC by balancing model fit and complexity using posterior expectations. This dual structure allows users to compare classical and Bayesian fits side-by-side, and to leverage familiar modeling workflows while gaining access to richer inferential tools.

The lmb function is a specialized version of glmb for Gaussian models, and does not require a family argument. For conjugate models, it uses standard simulation methods for posterior draws, avoiding the need for envelope construction or subgradient sampling. Like glmb, it returns objects compatible with many standard methods from lm and glm, including extractAIC, fitted.values, and residuals.

For more minimalistic workflows, rlmb and rglmb offer stripped-down interfaces for posterior sampling without the overhead of full model objects. rlmb is called from within lmb. The functions rlmb might be useful in Gibbs sampling or simulation-heavy contexts.

Value

lmb returns an object of class "lmb". The function summary (i.e., summary.glmb) can be used to obtain or print a summary of the results. The generic accessor functions coefficients, fitted.values, residuals, and extractAIC can be used to extract various useful features of the value returned by lmb.

An object of class "lmb" is a list containing at least the following components:

lm

an object of class "lm" containing the output from a call to the function lm

coefficients

a matrix of dimension n by length(mu) with one sample in each row

coef.means

a vector of length(mu) with the estimated posterior mean coefficients

coef.mode

a vector of length(mu) with the estimated posterior mode coefficients

dispersion

Either a constant provided as part of the call, or a vector of length n with one sample in each row.

Prior

A list with the priors specified for the model in question. Items in list may vary based on the type of prior

residuals

a matrix of dimension n by length(y) with one sample for the deviance residuals in each row

fitted.values

a matrix of dimension n by length(y) with one sample for the fitted values in each row

linear.predictors

an n by length(y) matrix with one sample for the linear fit on the link scale in each row

deviance

an n by 1 matrix with one sample for the deviance in each row

pD

An Estimate for the effective number of parameters

Dbar

Expected value for minus twice the log-likelihood function

Dthetabar

Value of minus twice the log-likelihood function evaluated at the mean value for the coefficients

DIC

Estimated Deviance Information criterion

weights

a vector of weights specified or implied by the model

prior.weights

a vector of weights specified or implied by the model

y

a vector of observations of length m.

x

a design matrix of dimension m * p

model

if requested (the default),the model frame

call

the matched call

formula

the formula supplied

terms

the terms object used

data

the data argument

famfunc

family functions used during estimation and post processing

iters

an n by 1 matrix giving the number of candidates generated before acceptance for each sample.

contrasts

(where relevant) the contrasts used.

xlevels

(where relevant) a record of the levels of the factors used in fitting

pfamily

the prior family specified

digits

the number of significant digits to use when printing.

In addition, non-empty fits will have (yet to be implemented) components qr, R and effects relating to the final weighted linear fit for the posterior mode. Objects of class "lmb" are normally of class c("lmb","glmb","glm","lm"), that is inherit from classes glmb. glm and lm and well-designed methods from those classed will be applied when appropriate.

References

Chambers JM (1992). “Linear Models.” In Chambers JM, Hastie TJ (eds.), Statistical Models in S, chapter 4, 85–124. Wadsworth & Brooks/Cole, Pacific Grove, CA.

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

Raiffa H, Schlaifer R (1961). Applied Statistical Decision Theory. Clinton Press, Inc., Boston.

Spiegelhalter DJ, Best NG, Carlin BP, van der Linde A (2002). “Bayesian Measures of Model Complexity and Fit.” Journal of the Royal Statistical Society: Series B (Statistical Methodology), 64(4), 583–639. doi:10.1111/1467-9868.00353.

Wilkinson GN, Rogers CE (1973). “Symbolic Descriptions of Factorial Models for Analysis of Variance.” Applied Statistics, 22(3), 392–399. doi:10.2307/2346786.

See Also

The classical modeling functions lm and glm.

glmb, rglmb, rlmb for related Bayesian GLM/linear interfaces; EnvelopeBuild for envelope construction when accept–reject sampling is used.

pfamily for documentation of pfamily functions used to specify priors.

Prior_Setup, Prior_Check for functions used to initialize and to check priors,

Further reading: (Nygren and Nygren 2006); (Nygren 2025, 2025); independent Normal–Gamma sampler: (Nygren 2025).

summary.glmb, predict.glmb, simulate.glmb, extractAIC.glmb, dummy.coef.glmb and methods(class="glmb") for methods inherited from class glmb and the methods and generic functions for classes glm and lm from which class lmb also inherits.

glmbayes Modeling Functions glmb()

Examples

## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
ctl <- c(4.17, 5.58, 5.18, 6.11, 4.50, 4.61, 5.17, 4.53, 5.33, 5.14)
trt <- c(4.81, 4.17, 4.41, 3.59, 5.87, 3.83, 6.03, 4.89, 4.32, 4.69)
group <- gl(2, 10, 20, labels = c("Ctl", "Trt"))
weight <- c(ctl, trt)

ps <- Prior_Setup(weight ~ group, gaussian())
# Prior_Setup supplies the prior mean and covariance components used below.

## Classical model
lm.D9 <- lm(weight ~ group, x = TRUE, y = TRUE)
summary(lm.D9)
vcov(lm.D9)
s <- summary(lm.D9)
disp_classical <- s$sigma^2
cat("Classical lm dispersion (sigma^2 = RSS/(n-p)):", disp_classical, "\n")

## Conjugate Normal Prior (fixed dispersion)
lmb.D9 <- lmb(
  weight ~ group,
  pfamily = dNormal(mu = ps$mu, ps$Sigma, dispersion = ps$dispersion)
)
summary(lmb.D9)
vcov(lmb.D9)

## Conjugate Normal_Gamma Prior (second argument is Sigma_0 from Prior_Setup)
lmb.D9_v2 <- lmb(
  weight ~ group,
  pfamily = dNormal_Gamma(
    ps$mu,
    Sigma_0 = ps$Sigma_0,
    shape = ps$shape,
    rate  = ps$rate
  )
)
summary(lmb.D9_v2)
vcov(lmb.D9_v2)

## Independent_Normal_Gamma_Prior (same mu, Sigma, rate as dNormal_Gamma; shape = ps$shape_ING)

lmb.D9_v3 <- lmb(
  weight ~ group,
  dIndependent_Normal_Gamma(
    ps$mu,
    ps$Sigma,
    shape = ps$shape_ING,
    rate  = ps$rate
  )
)
summary(lmb.D9_v3)


## anova 
anova(lmb.D9)

## lmb with dGamma prior (dispersion-only; coefficients fixed)
rate_dg <- if (!is.null(ps$rate_gamma)) ps$rate_gamma else ps$rate
out_lmb_dGamma <- lmb(
  weight ~ group,
  pfamily = dGamma(shape = ps$shape, rate = rate_dg, beta = ps$coefficients))
summary(out_lmb_dGamma)

Fitting Blocked Bayesian Linear Models

Description

Fits one lmb per observation block (SAS BY-style split on rows), sharing the same formula on each subset. Contrast with lmb on a cbind(...) response (several response columns) and rNormalGLM_reg_group (Gibbs conditional draws, matrix API).

Usage

lmbBlock(
  formula,
  block,
  pfamily = NULL,
  pfamily_list = NULL,
  n = 1000,
  data,
  subset,
  weights,
  na.action,
  method = "qr",
  model = TRUE,
  x = TRUE,
  y = TRUE,
  qr = TRUE,
  singular.ok = TRUE,
  contrasts = NULL,
  offset,
  Gridtype = 2,
  n_envopt = NULL,
  use_parallel = TRUE,
  use_opencl = FALSE,
  verbose = FALSE,
  ...
)

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

Arguments

formula

an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted. The details of model specification are given under ‘Details’.

block

Block 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).

pfamily

A single pfamily recycled to every block, or use pfamily_list of length k (number of blocks).

pfamily_list

Optional list of pfamily objects, one per block.

n

number of draws to generate. If length(n) > 1, the length is taken to be the number required.

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 lm is called.

subset

an optional vector specifying a subset of observations to be used in the fitting process.

weights

an optional vector of weights to be used in the fitting process. Should be NULL or a numeric vector. If non-NULL, weighted least squares is used with weights weights (that is, minimizing sum(w*e^2)); otherwise ordinary least squares is used. See also ‘Details’,

na.action

a function which indicates what should happen when the data contain NAs. The default is set by the na.action setting of options, and is na.fail if that is unset. The ‘factory-fresh’ default is stats{na.omit}. Another possible value is NULL, no action. Value stats{na.exclude} can be useful.

method

the method to be used in fitting the classical model during a call to glm. The default method glm.fit uses iteratively reweighted least squares (IWLS): the alternative "model.frame" returns the model frame and does no fitting. User-supplied fitting functions can be supplied either as a function or a character string naming a function, with a function which takes the same arguments as glm.fit. If specified as a character string it is looked up from within the stats namespace.

model, x, y, qr

For lmbBlock, logicals passed to each block lmb fit (see lm). For print, x is the "blmb" object.

singular.ok

logical. If FALSE (the default in S but not in R) a singular fit is an error.

contrasts

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

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 or matrix of extents matching those of the response. One or more offset terms can be included in the formula instead or as well, and if more than one are specified their sum is used. See model.offset.

Gridtype

an optional argument specifying the method used to determine the number of tangent points used to construct the enveloping function.

n_envopt

Effective sample size passed to EnvelopeOpt for grid construction. Defaults to match n. Larger values encourage tighter envelopes.

use_parallel

Logical. Whether to use parallel processing during simulation.

use_opencl

Logical. Whether to use OpenCL acceleration during Envelope construction.

verbose

Logical. Whether to print progress messages.

...

For lm(): additional arguments to be passed to the low level regression fitting functions (see below).

digits

Number of significant digits to use when printing.

Value

A named list of class "blmb" (list of "lmb" fits).

Functions

  • lmbBlock(): Gaussian lmb fit per row block.

See Also

glmbayes Modeling Functions glmbBlock()

Examples

## Row-block lmb (iris): BY Species — SAS-style separate regressions

set.seed(42)
data("iris", package = "datasets")

ps_block <- Prior_SetupGroup(
  Sepal.Length ~ Sepal.Width + Petal.Length,
  group = "Species",
  data = iris,
  family = gaussian()
)

out_blmb <- lmbBlock(
  Sepal.Length ~ Sepal.Width + Petal.Length,
  block = "Species",
  pfamily_list = pfamily_list(ps_block),
  data = iris,
  n = 150L,
  use_parallel = FALSE
)

print(out_blmb)
summary(out_blmb)

Fit a Bayesian linear mixed-effects model (LMM) to data, via two-Block Gibbs sampling

Description

Bayesian linear mixed-effects model fit

Usage

lmerb(
  formula,
  data = NULL,
  pfamily_list,
  dispersion_ranef,
  dispformula = ~1,
  n = 1000L,
  tv_tol = 0.01,
  gap_tol = 0.0196,
  mode_gap_max = 1,
  diag_sweeps = FALSE,
  progbar = NULL,
  sim_method = "DEFAULT",
  simulate = TRUE,
  REML = TRUE,
  control = lme4::lmerControl(),
  start = NULL,
  verbose = 0L,
  subset,
  weights,
  na.action,
  offset,
  contrasts = NULL,
  devFunOnly = FALSE,
  fixef = NULL,
  ...
)

## S3 method for class 'lmerb'
print(
  x,
  digits = max(3L, getOption("digits") - 3L),
  sweep_history = FALSE,
  sweep_history_stage = c("main", "pilot", "both"),
  max_sweeps = Inf,
  components = NULL,
  covariate = NULL,
  ...
)

Arguments

formula

Mixed-model formula (single grouping factor; same constraints as model_setup).

data

Data frame containing all variables in formula.

pfamily_list

Required named list of pfamily objects, one per random-effect coefficient (names must match the random-effect coefficient names, any order). Supplies the Block~2 hyperpriors (mu, Sigma) and the Block~1 random-effect variances τk2\tau^2_k. dNormal components treat τk2\tau^2_k (the pfamily dispersion) as known and make conjugate γk\gamma_k draws. dIndependent_Normal_Gamma components place a Gamma prior on the Block~2 precision 1/τk21/\tau^2_k: Block~2 then makes a joint (γk,τk2)(\gamma_k, \tau^2_k) draw via the likelihood-subgradient envelope sampler (rindepNormalGamma_reg), and the sampled τk2\tau^2_k feeds back into the Block~1 prior precision. ING components must supply both truncation bounds: each τk2\tau^2_k draw is hard-truncated to [disp_lower, disp_upper], fixed across all inner Gibbs sweeps, with disp_lower doubling as the conservative τk2\tau^2_k plug-in for the eigenvalue / TV calibration (smaller τ2\tau^2 increases the contraction rate λ∗\lambda^*, so the bound holds for every dispersion in the truncated support). They must also satisfy the prior-vs-data guard nprior≤Jn_{\mathrm{prior}} \le J (pwt_dispersion ≤0.5\le 0.5). Typically built with pfamily_list from a Prior_Setup_GLMM object.

dispersion_ranef

Observation-level measurement dispersion σ2\sigma^2 for Block~1. One of: a positive scalar (treated as known; typically Prior_Setup_GLMM(...)$dispersion_ranef), a single dGamma() pfamily (pooled σ2\sigma^2 across groups), a named list of dGamma() objects (one per group level) from dGamma_list(Prior_Setup_GLMM(...)), or a named numeric vector of positive, fixed per-group values (names must match the random-effects grouping factor's levels exactly; each group's σj2\sigma^2_j is then treated as known, like the pooled scalar case but allowed to vary by group). Which of these four shapes is accepted depends on dispformula (see below).

dispformula

One-sided formula selecting the measurement-dispersion structure: ~1 (default, pooled) requires dispersion_ranef to be a fixed scalar or a single (pooled) dGamma(); ~<group_name>, matching the random-effects grouping factor exactly, requires dispersion_ranef to be a dGamma_list(...) (one dGamma() per group level) or a named numeric vector (one fixed value per group level). Any other formula is an error. ~1 never fits an extra reference model. ~<group_name> with a dGamma_list(...) additionally requires a glmmTMB reference fit (glmmTMB must be installed), stored as dispersion_fit; ~<group_name> with a fixed numeric vector never fits one, since the per-group dispersion is directly user-supplied, not a prior to calibrate. When dispersion_ranef = dGamma_list(Prior_Setup_GLMM(..., dispformula = dispformula)), that call already fit the glmmTMB reference model to calibrate the priors, and lmerb() reuses it here rather than fitting glmmTMB a second time; keep dispformula identical between the two calls, since it is not re-validated against the reused fit. lmer is always the plain lmer fit regardless of dispformula; the sampler route (pooled vs. per-group) already follows from dispersion_ranef's shape alone.

n

Number of iid draws per group (default 1000L, as in lmb).

tv_tol

Total variation tolerance per stored draw, in (0, 1) (default 0.01, the conventional threshold of the honest-burn-in literature; Jones and Hobert 2001). The number of inner Gibbs sweeps per stored draw is derived so that each draw is within tv_tol of the exact joint posterior in total variation (Nygren 2020, Theorem 3; see Details). To certify the whole n-draw sample at level α\alpha pass tv_tol = alpha / n; the cost grows only logarithmically in 1/tv_tol.

gap_tol

Legacy mode–mean gap tolerance for the pilot stage when any Block~2 component uses dIndependent_Normal_Gamma and tv_tol is NULL. When tv_tol is set (default), the pilot chain count is chosen by cost optimization instead. Set NULL to skip the pilot unless tv_tol is set. Ignored for all-dNormal models.

mode_gap_max

Maximum per-coordinate mode–mean gap (in posterior standard deviation units) used to calibrate pilot inner sweeps when ING Block~2 components run a pilot stage (default 1.0). Ignored for all-dNormal models.

diag_sweeps

Temporary diagnostic flag (ING models with pilot). Non-dNormal sampling already runs via lmebayesCore::run_sweep_outer_chains_v6() (R sweep-outer; pilot then main). When TRUE, each stage auto-prints one combined Block~2 chain-mean table when that stage finishes (same layout as print() on $sweep_history). sweep_history is collected regardless. Default FALSE.

progbar

Logical. Show text progress bars during sampling (passed to rlmerb). Default NULL: FALSE when diag_sweeps = TRUE, otherwise TRUE.

sim_method

Sampling engine: "DEFAULT" or "TWO_BLOCK_GIBBS". Only changes behavior when dispersion_ranef is fixed (a scalar or a named per-group vector) and every pfamily_list component is dNormal() (known variance components) – the joint posterior is then exactly multivariate normal, and "DEFAULT" draws directly, iid, from that closed form (no Gibbs sweeps, no burn-in, no autocorrelation between draws); "TWO_BLOCK_GIBBS" forces the two-block Gibbs sampler described above instead. Every other model (any dIndependent_Normal_Gamma component, or a sampled/estimated variance component) only has the two-block Gibbs engine, so both values behave identically there. See rLMMNormal_reg_known_vcov.

simulate

Logical (default TRUE). Passed through to rlmerb. When TRUE the two-block Gibbs sampler is run for n iterations and posterior draws are stored. When FALSE, rlmerb returns fixed-VC plug-in point estimates only (exact joint Gaussian posterior mean). Simulation-only fields (groupef, popef.means, popef) are NULL when simulate = FALSE.

REML

Logical; passed to model_setup.

control

lmerControl settings; passed to model_setup.

start

Optional starting values; passed to model_setup.

verbose

Verbosity flag; passed to model_setup.

subset

Optional subset; passed to model_setup.

weights

Optional weights; passed to model_setup.

na.action

Missing-data handler; passed to model_setup.

offset

Optional offset; passed to model_setup.

contrasts

Optional contrasts; passed to model_setup.

devFunOnly

If TRUE, return deviance function only; passed to model_setup.

fixef

Optional named list of hyper-parameter vectors (Block 2 state). When NULL (default), iter-0 means are taken from the pfamily_list prior means.

...

Reserved for future use.

x

Object of class "lmerb".

digits

Number of significant digits to use when printing.

sweep_history

If TRUE, print stored Block~2 sweep history (see $sweep_history) after the usual summary: one combined table per stage with mode plus all inner sweeps.

sweep_history_stage

Which stage to print when sweep_history = TRUE: "main", "pilot", or "both".

max_sweeps

Passed to print() on sweep-history objects; limits how many inner sweeps are shown per stage.

components

Optional RE components passed to sweep-history print().

covariate

Optional covariate names passed to sweep-history print().

Details

Fits a Bayesian linear mixed model from an lmer-style formula, returning posterior draws. It is the mixed-model counterpart of lmb for fixed-effects models.

lmerb builds the design with model_setup, embeds the companion lmer fit for reference, and passes both to the lmebayesCore sampler. Priors are supplied as pfamily_list (population level) and dispersion_ranef (residual variance), normally built from one Prior_Setup_GLMM object.

The model being estimated

For group j=1,…,Jj = 1, \dots, J, lmerb estimates

yj∣βj,σ2∼N(Djβj,  σ2Inj)(within group)y_j \mid \beta_j, \sigma^2 \sim N(D_j \beta_j,\; \sigma^2 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)}

The second line is the model for how groups differ: each group's own coefficient vector βj\beta_j varies around a population expectation Wjγ\mathcal{W}_j\gamma, and Ψ\Psi measures how widely. This is a centered parameterization — the full coefficient βj\beta_j enters the likelihood, not a mean-zero deviation — so coef() on the fit reports βj\beta_j directly and ranef() reports the deviation βj−Wjγ\beta_j - \mathcal{W}_j\gamma that lme4 calls bjb_j. Marginalizing recovers the usual lme4 model, with γ\gamma playing the role of the fixed effects.

Ψ\Psi is diagonal because formula may only contain uncorrelated (||) random-effect terms and a single grouping factor; there is no random-effect correlation to estimate. See vignette("Chapter-02", package = "lmebayes") for the formula rules.

What the priors decide

The two prior arguments are where you declare which variances are known and which are estimated, and that choice determines what comes back.

pfamily_list

One component per random-effect coefficient. A dNormal component fixes the between-group variance τk2\tau^2_k at the supplied value; a dIndependent_Normal_Gamma component estimates it, under a Gamma prior on 1/τk21/\tau^2_k truncated to [disp_lower, disp_upper].

dispersion_ranef

The residual variance σ2\sigma^2: a fixed scalar, a pooled dGamma() prior to estimate one common value, a dGamma_list() to estimate a separate σj2\sigma^2_j per group, or a named vector of fixed per-group values. dispformula must agree with the shape supplied.

When every variance is fixed, the posterior is exactly multivariate normal and the draws are exact. Estimating any variance makes the posterior non-Gaussian and the draws approximate in the sense described next.

What the returned draws are

lmerb does not return one long chain. It runs n independent replicate chains and keeps one draw from each, taken at that chain's final sweep. Two things follow:

  1. The draws are mutually independent. They form an iid sample, so ordinary Monte Carlo standard errors apply. Nothing needs thinning, there is no effective sample size to compute, and trace plots or R^\hat{R} on the returned draws are not meaningful.

  2. Each draw comes from a distribution deliberately made close to the posterior. The sweep count 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. The resulting m_convergence is reported on the fit.

n and tv_tol therefore control different errors — Monte Carlo noise and per-draw bias — and can be set independently. To certify all n draws jointly at level α\alpha, pass tv_tol = alpha / n; cost grows only logarithmically in 1/1/tv_tol, so this usually adds a few sweeps.

The calibration itself, and what to do if a warning reports slow convergence, are covered in vignette("Chapter-06", package = "lmebayes").

Value

Object of class "lmerb": a list with the following components (parallel to glmb and lmb).

Every draw slot holds one row per replicate chain, taken at that chain's final sweep, so the rows are mutually independent and can be averaged directly. Accessors fixef(), ranef(), coef() and summary() are usually more convenient than reaching into these fields; see vignette("Chapter-05", package = "lmebayes").

call

The matched call.

formula

The formula supplied.

lmer

lmer fit from model_setup (full formula), embedded as a sub-object — analogous to glmb$glm and lmb$lm. Use coef(fit$lmer) for per-group classical coefficients. Always the plain pooled-dispersion lmer fit, regardless of dispformula.

dispformula

The dispformula supplied.

dispersion_fit

NULL when dispformula = ~1; otherwise the diagnostic-only glmmTMB fit with per-group residual dispersion (dispformula). Identical to glmmTMB below; kept for backward compatibility.

glmmTMB

Same value as dispersion_fit (an alias): NULL when dispformula = ~1, otherwise the glmmTMB per-group-dispersion reference fit. Prefer this field name in new code; downstream consumers that need a per-group-dispersion-aware reference fit (e.g.\ summary.lmerb()) use glmmTMB when non-NULL and fall back to lmer otherwise.

prior

Normalized prior container: pfamily_list (as supplied, reordered to the RE coefficient names), dispersion_ranef, the reconstructed Sigma_ranef, and the per-component prior_list (mu_fixef, Sigma_fixef, dispersion_fixef) — analogous to glmb$Prior.

model_setup

The model_setup object built inside lmerb from formula and data.

popef.mode

Named list of exact posterior mode (= mean, since the joint posterior is Gaussian) vectors for the level-2 fixed effects γk\gamma_k, computed by lmerb_posterior_mean (ICM).

groupef.mode

J×preJ \times p_{\mathrm{re}} numeric matrix of exact posterior mode random effects from ICM. Rows are group levels (levels(design$group)); columns are design$re_coef_names.

popef

Named list of n×qkn \times q_k matrices of population draws γk\gamma_k, one per random-effect coefficient. NULL when simulate = FALSE.

popef.means

Named list of posterior mean vectors, the column means of popef. NULL when simulate = FALSE.

groupef

data.frame with n * J rows: draw, the grouping-factor column, and one column per RE variable. These are full group coefficients βj\beta_j, comparable to lme4::coef(), not mean-zero deviations. Average over draw within each group for posterior means (see Examples). NULL when simulate = FALSE.

popef.dispersion

n×pren \times p_{\mathrm{re}} matrix of the between-group variances τk2\tau^2_k at each stored draw: sampled values for dIndependent_Normal_Gamma components, constant columns for dNormal components, where the value was treated as known. NULL when simulate = FALSE.

popef.dispersion.mean

Named vector of posterior means of τk2\tau^2_k (colMeans(fixef.dispersion)). NULL when simulate = FALSE.

group.dispersion

Observation-level residual variance σ2\sigma^2: a scalar when dispersion_ranef is fixed, a length-n vector of final-sweep draws when dispersion_ranef is dGamma(); NULL when not applicable. When simulate = FALSE, the prior plug-in scalar (prior$dispersion_ranef) is returned for Gaussian models.

group.dispersion.mean

Posterior mean of σ2\sigma^2; equals group.dispersion when fixed. NULL when group.dispersion is NULL.

draw_engine

Name of the Block~1 sampling engine used (e.g. "rGLMM_sweep_ing_block1_ind"). NULL when simulate = FALSE.

fixef.iters

n×pren \times p_{\mathrm{re}} matrix of the total number of Block~2 candidates generated per stored draw, summed over the m_convergence inner sweeps. dIndependent_Normal_Gamma components count envelope accept-reject candidates until acceptance (as iters in glmbayes samplers); dNormal components count exactly one conjugate draw per sweep. NULL when simulate = FALSE.

fixef.iters.mean

Named vector of average candidates per accepted Block~2 draw (colMeans(fixef.iters)/m_convergence; equals 1 for dNormal components, and approximately the reciprocal acceptance rate of the ING envelope sampler otherwise). NULL when simulate = FALSE.

fixef.mu

Numeric matrix p_re x J of Block 1 prior means at the final Gibbs state (from build_mu_all).

convergence

List describing the sweep-count calibration: method ("exact", "exact_iid" when sim_method_used = "DEFAULT", or "local_gaussian_mode" for non-Gaussian glmerb), tv_tol, lambda_star, eigenvalues, m_min (derived minimum sweeps), and m_convergence (sweeps actually used). NULL when simulate = FALSE.

sim_method_used

"DEFAULT" (exact iid draws) or "TWO_BLOCK_GIBBS" (two-block Gibbs sweeps), whichever engine actually ran. NULL when simulate = FALSE.

References

Nygren, K. (2020). On the total variation distance between multivariate normal densities with applications to two-block Gibbs samplers. Unpublished manuscript.

Jones, G. L. and Hobert, J. P. (2001). Honest exploration of intractable probability distributions via Markov chain Monte Carlo. Statistical Science 16, 312–334.

See Also

Prior_Setup_GLMM, model_setup, build_mu_all, two_block_rNormal_reg, lmerb_posterior_mean, rNormal_reg_group, lmb, glmb

Examples

## lmerb: school random effects on bayesrules::big_word_club
##
## Random intercept and distracted_ppvt random slope by school; the school's
## share of free/reduced-lunch students (free_reduced_lunch, constant within
## school) is a level-2 predictor of the school intercepts.
##
## Fast man-page example: prior setup, lmer reference fit, and ICM posterior
## mean only (simulate = FALSE).  No Gibbs draws — suitable for R CMD check.
##
## The full sampling workflows are preserved as demos:
##   demo("Ex_14_lmerb_Sleepstudy", package = "lmebayes")   # lme4-style small model
##   demo("Ex_12_lmerb_BigWordClub", package = "lmebayes")  # full formula

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)
  dat <- dat[complete.cases(dat[, c("score_ppvt", "distracted_ppvt",
                                    "free_reduced_lunch", "school_id")]), ]

  form <- score_ppvt ~ free_reduced_lunch + distracted_ppvt +
    (1 + distracted_ppvt || school_id)

  ## Default hyperpriors calibrated from a reference lmer fit (weak prior).
  ps <- Prior_Setup_GLMM(form, data = dat, pop.pwt = 0.01)

  fit <- lmerb(
    form,
    data             = dat,
    pfamily_list     = pfamily_list(ps),
    dispersion_ranef = ps$group.dispersion,
    simulate         = FALSE
  )
  ## lmer MLE vs ICM posterior mean (no MCMC means when simulate = FALSE).
  lmebayes:::print_coef_means(fit)
  print(fit)
  summary(fit)
}

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

Examples

## model_setup() — design + reference lmer (Ex_13b / big_word_club)
##
## Same formula and data cleaning as
## demo("Ex_13b_rLMM_estimated_dispersion_known_vcov_BigWordClub_PartVI").
## 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)

  ## Default: design matrices + reference lmer + identifiability
  design <- model_setup(form_lmer, data = dat)
  print(design)

  ## Full-rank filter (same as Ex_13b before prior setup / sampling)
  full_rank <- names(design$groupef.rank)[design$groupef.rank]
  dat_fr <- subset(dat, school_id %in% full_rank)
  dat_fr$school_id <- droplevels(dat_fr$school_id)
  design_fr <- model_setup(form_lmer, data = dat_fr)
  stopifnot(all(design_fr$groupef.rank))
  print(design_fr)

  ## Variation: design only (no reference merMod fit)
  design_only <- model_setup(form_lmer, data = dat_fr, fit_mer = FALSE)
  stopifnot(is.null(design_only$lmer))
  names(design_only$W)
}

Prior Family Objects for Bayesian Models

Description

Prior family objects provide a convenient way to specify the details of the priors used by matrix-input samplers such as rglmb and rlmb. See the documentation for rglmb and rlmb for the details of how such model fitting takes place.

Under a Beta(shape1, shape2) prior on the binomial probability θ\theta and a Binomial(nin_i, θ\theta) likelihood with identity link (θ=β\theta = \beta directly), the posterior is:

θ∣y∼Beta(shape1+∑niyi,  shape2+∑ni(1−yi)).\theta \mid y \sim \mathrm{Beta}(\texttt{shape1} + \sum n_i y_i,\; \texttt{shape2} + \sum n_i (1 - y_i)).

Usage

dNormal(mu, Sigma, dispersion = NULL)

dNormal_Gamma(mu, Sigma_0, shape, rate)

dIndependent_Normal_Gamma(
  mu,
  Sigma,
  shape,
  rate,
  max_disp_perc = 0.99,
  disp_lower = NULL,
  disp_upper = NULL
)

dGamma(
  shape,
  rate,
  beta,
  Inv_Dispersion = TRUE,
  lik_shape = 1,
  max_disp_perc = 0.99,
  disp_lower = NULL,
  disp_upper = NULL
)

Arguments

mu

a prior mean vector for the the modeling coefficients used in several pfamilies

Sigma

a prior variance-covariance matrix for dNormal() and dIndependent_Normal_Gamma().

dispersion

the dispersion to be assumed when it is not given a prior. Should be provided when the Normal prior is for the gaussian(), Gamma(), quasibinomial, or quasipoisson families. The binomial() and poisson() families do not have dispersion coefficients. Omitted or NULL uses the internal default 1 and sets ddef in prior_list (see Details).

Sigma_0

prior variance-covariance on the precision-weighted coefficient scale for dNormal_Gamma() only (Gaussian). Stored in prior_list$Sigma for compatibility with downstream samplers.

shape

The prior shape parameter for the gamma piece (inverse dispersion / precision). When taking defaults from Prior_Setup, use ps$shape with dNormal_Gamma() and dGamma(), and ps$shape_ING with dIndependent_Normal_Gamma() on the Gaussian calibrated path (see Details).

rate

The prior rate parameter paired with shape. With Gaussian Prior_Setup, dNormal_Gamma() and dIndependent_Normal_Gamma() use ps$rate; for dGamma() with fixed beta, prefer ps$rate_gamma when that field is non-NULL (see Details).

max_disp_perc

Specifies the percentile used to truncate the posterior dispersion distribution when constructing the envelope for accept-reject sampling. This determines the lower and upper bounds for the dispersion (σ2\sigma^2) used in the simulation. A value of 0.99 corresponds to using the central 98 percent of the posterior dispersion mass (i.e., excluding the outer 1 percent in each tail). Smaller values yield tighter bounds and may improve acceptance rates, while larger values allow broader dispersion support but may increase envelope complexity.

disp_lower

lower bound truncation for dispersion

disp_upper

upper bound truncation for dispersion

beta

Initial coefficient matrix (1 ×\times 1); typically set to the prior mean shape1/(shape1+shape2).

Inv_Dispersion

Logical (default TRUE). Controls which of the two Gamma prior roles dGamma() plays:

  • TRUE (default): prior on inverse dispersion (precision / shape parameter k=1/ϕk = 1/\phi). This is the classical path used for dispersion estimation in Gaussian and Gamma(log) regression (simfun = rGamma_reg).

  • FALSE: conjugate prior on the Gamma or Poisson rate β\beta directly (intercept-only, identity link). The posterior is a closed-form Gamma draw (simfun = rGamma_Conjugate_reg).

lik_shape

Known shape parameter k>0k > 0 of the Gamma likelihood. Only used when Inv_Dispersion = FALSE and family = Gamma(link = "identity"). The intercept coefficient is then the Gamma rate β\beta, and the conjugate posterior is β∣y∼Gamma(α0+nk,  β0+∑yi)\beta \mid y \sim \mathrm{Gamma}(\alpha_0 + n k,\; \beta_0 + \sum y_i). Defaults to 1 (exponential distribution). Ignored for Poisson families and whenever Inv_Dispersion = TRUE.

Details

pfamily is a generic with methods for fitted objects such as rglmb and rlmb. The dNormal() prior is supported for all response families. The gaussian() family additionally supports dNormal_Gamma(), dIndependent_Normal_Gamma(), and dGamma() (precision prior). Intercept-only models with an identity link support two closed-form conjugate priors: dBeta() for binomial(link = "identity") and dGamma(Inv_Dispersion = FALSE) for poisson(link = "identity") and Gamma(link = "identity").

A pfamily object represents a structured prior specification for use in Bayesian generalized linear modeling. Each constructor function (e.g., dNormal(), dGamma(), dNormal_Gamma(), dBeta()) returns an object of class "pfamily" containing the prior parameters, supported likelihood families, compatible link functions, and a simulation function for posterior sampling.

These priors are designed to integrate seamlessly with modeling functions such as rglmb() and rlmb() in the glmbayes package, which consume the pfamily object to define the prior distribution over model parameters. The pfamily() generic retrieves the embedded prior from a fitted model object, while print.pfamily() displays its structure.

prior_list, simfun, and pfun. The named list prior_list holds the hyperparameters for the chosen prior family. When a model function draws from the posterior, it passes prior_list into the element simfun (e.g., rNormal_reg, rGamma_reg) so the low-level sampler receives one consistent list structure regardless of which constructor built the pfamily. Each constructor also stores a matching pfun (see prior_simfuncs) for i.i.d.\ draws from the prior via pfun(n, prior_list, params), mirroring how simfun is used for the posterior.

Prior_Setup and default hyperparameters. Prior_Setup() fits an auxiliary GLM and returns default mu, Sigma / Sigma_0, dispersion, Gamma shape and rate, and related fields aligned with the data and prior-weight (pwt) choices. Those values can be supplied as arguments to the pfamily constructors when you want package-default priors on the same scale as the model matrix. Recommended use of shape and rate is not identical across constructors: for dIndependent_Normal_Gamma(), pass shape = ps$shape_ING from Prior_Setup (not the scalar ps$shape used by dNormal_Gamma()). For dGamma() with fixed coefficients (beta), pass rate = ps$rate_gamma when that field is present (otherwise ps$rate); see Prior_Setup and compute_gaussian_prior.

Prior Families

  • dNormal(): Specifies a multivariate normal prior over regression coefficients. It is conjugate for Gaussian likelihoods with an identity link function, and serves as the primary implemented prior for all other supported likelihood families in the current framework. This structure facilitates efficient posterior sampling and analytical tractability. The returned prior_list includes ddef: TRUE when dispersion was omitted or NULL (so the default 1 was used), FALSE when dispersion was supplied explicitly (including 1).

    For models with log-concave likelihood functions-such as Poisson, Binomial, and Gamma families- posterior sampling under a dNormal prior is performed using a (Nygren and Nygren 2006) likelihood subgradient approach. This method constructs tight enveloping functions around the posterior using subgradients of the log-likelihood, enabling efficient accept-reject sampling even in high dimensions.

    When the posterior distribution is approximately normal (typically the case for large sample sizes), the area under the enveloping function is bounded above by a constant factor-approximately 2/π≈1.1282 / \sqrt{\pi} \approx 1.128 in the univariate case, and (2/π)k(2 / \sqrt{\pi})^k in kk-dimensional models. These bounds ensure that the rejection rate remains manageable and that the sampler remains computationally efficient.

    The concept of conjugate priors was first formalized by (Raiffa and Schlaifer 1961), and further developed for regression models using g-prior structures by (Zellner 1986).

  • dGamma(): A Gamma prior with two distinct roles controlled by Inv_Dispersion:

    • Inv_Dispersion = TRUE (default): prior on the inverse dispersion (precision 1/ϕ1/\phi or shape kk). Used for dispersion estimation in Gaussian and Gamma(log) models, typically in a Gibbs step with beta held fixed (Gelman et al. 2013; Dobson 1990; McCullagh and Nelder 1989). With Gaussian Prior_Setup output, prefer rate_gamma for rate (see Details above).

    • Inv_Dispersion = FALSE: conjugate Gamma prior on the rate parameter β\beta directly. Supports intercept-only models with an identity link: Poisson (Gamma–Poisson conjugacy) and Gamma (Gamma–Gamma conjugacy). Posterior draws are closed-form IID samples via rGamma_Conjugate_reg. The lik_shape argument specifies the known Gamma likelihood shape (default 1, i.e.\ exponential). Prior_Setup returns calibrated conj_poisson hyperparameters for this path.

  • dBeta(): A Beta prior on the binomial probability θ\theta for intercept-only binomial(link = "identity") models. The posterior is a closed-form Beta draw (Beta–Binomial conjugacy) produced by rBeta_reg. Arguments shape1 and shape2 are the prior pseudo-success and pseudo-failure counts. Prior_Setup returns calibrated conj_beta hyperparameters for this path.

  • dNormal_Gamma(): Combines a multivariate normal prior on coefficients with a gamma prior on precision, forming a conjugate structure for Gaussian models with unknown variance. The second argument is Sigma_0 (precision-weighted scale); it is aliased internally to Sigma in prior_list. This formulation parallels classical Normal-Gamma models and is compatible with hierarchical extensions (Gelman et al. 2013; Raiffa and Schlaifer 1961).

  • dIndependent_Normal_Gamma(): Similar to dNormal_Gamma(), but assumes independence between the coefficient and precision priors. This structure is useful for models where prior independence is desired or analytically convenient. With Prior_Setup on a Gaussian model, pass shape_ING as the shape argument (see Details above).

Each pfamily object includes:

  • pfamily, prior_list, okfamilies, plinks, simfun, and pfun (see Value).

mu / Sigma: the surrogate Normal mean is shape1/(shape1+shape2) and the surrogate variance is the Beta variance shape1*shape2/((shape1+shape2)^2*(shape1+shape2+1)).

Value

An object of class "pfamily" (with a concise print method). A list with elements:

pfamily

Character string: the constructor name ("dNormal", "dGamma", "dNormal_Gamma", "dIndependent_Normal_Gamma", or "dBeta").

prior_list

Named list of prior hyperparameters. It is passed into simfun when sampling so the relevant low-level routine receives the prior in a fixed list form. Contents depend on the constructor:

dNormal:

mu, Sigma, dispersion, and logical ddef (TRUE if dispersion was omitted or NULL, so the default 1 was used; FALSE if set explicitly).

dGamma:

shape, rate, beta, Inv_Dispersion, max_disp_perc, disp_lower, disp_upper. When Inv_Dispersion = FALSE, also includes surrogate mu and Sigma (computed from the Gamma prior moments) and lik_shape.

dNormal_Gamma:

mu, Sigma (the Sigma_0 precision-weighted input), shape, rate.

dIndependent_Normal_Gamma:

mu, Sigma (coefficient-scale covariance), shape, rate, max_disp_perc, disp_lower, disp_upper.

dBeta:

shape1, shape2, beta, and surrogate mu and Sigma computed from the Beta prior moments (mu = shape1/(shape1+shape2), Sigma = shape1*shape2/((shape1+shape2)^2*(shape1+shape2+1))).

okfamilies

Character vector of implemented family names for which this pfamily may be used.

plinks

Function of one family argument returning allowed link names for that family.

simfun

Function used to generate posterior draws (e.g., rNormal_reg, rGamma_reg, rGamma_Conjugate_reg, rNormalGamma_reg, rindepNormalGamma_reg); for standard use these produce i.i.d.\ posterior samples for the implemented settings.

pfun

Prior-simulation function paired with this constructor (e.g., rNormal_prior, rGamma_prior, rNormal_Gamma_prior); called as pfun(n, prior_list, params). See prior_simfuncs.

References

Dobson A~J (1990). An Introduction to Generalized Linear Models. Chapman and Hall, London.

Gelman A, Carlin JB, Stern HS, Dunson DB, Vehtari A, Rubin DB (2013). Bayesian Data Analysis, 3rd edition. CRC Press.

McCullagh P, Nelder J~A (1989). Generalized Linear Models. Chapman and Hall, London.

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

Raiffa H, Schlaifer R (1961). Applied Statistical Decision Theory. Clinton Press, Inc., Boston.

Zellner A (1986). “On Assessing Prior Distributions and Bayesian Regression Analysis with g‐Prior Distributions.” In Goel P~K, Zellner A (eds.), Bayesian Inference and Decision Techniques: Essays in Honor of Bruno de Finetti, volume 6 of Studies in Bayesian Econometrics and Statistics, 233–243. Elsevier.

See Also

rglmb, rlmb for modeling functions that consume pfamily objects.

rNormal_reg, rNormalGamma_reg, rGamma_reg, rGamma_Conjugate_reg, rindepNormalGamma_reg for lower-level posterior sampling functions used by pfamily constructors.

prior_simfuncs for the paired prior-simulation functions (pfun).

Prior_Setup, Prior_Check for initializing and validating prior specifications.

EnvelopeBuild for envelope construction methods used in likelihood subgradient sampling (Nygren and Nygren 2006).

See also (Hastie and Pregibon 1992) for the original S modeling framework that inspired the design of pfamily.


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, ...)

Arguments

hist

Object of class "two_block_sweep_history", as carried by a fitted object's sweep_history (see print.two_block_sweep_history).

...

Passed to methods; see plot_mean_convergence.default.

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, ...)

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.

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


Print random effects in lme4 layout

Description

Print random effects in lme4 layout

Usage

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

Arguments

x

An object of class "ranef.lmerb".

...

Ignored.

Value

x, invisibly.


Setup Prior Objects

Description

Helper function to facilitate the Setup of Prior Distributions for glm models.

Usage

Prior_Setup(
  formula,
  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

an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted. The details of model specification are given under ‘Details’.

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

Weight on the prior relative to the likelihood function at the maximum likelihood estimate. If supplied, this value is used directly (scalar or one value per coefficient). If n_prior is provided and pwt is still a scalar and sd was not supplied, pwt is set to n_prior / (n_prior + n_effective). If length(pwt) > 1 (including from sd) or sd was supplied, n_prior does not overwrite pwt; it is used only as a scalar for Gamma / S_marg steps. If sd is provided, pwt is computed from the prior standard deviations. If none of these are supplied, pwt defaults to pwt_default_low for models with fewer than 14 coefficients, and pwt_default_high otherwise.

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.

n_prior

Optional scalar effective prior sample size (on the n_effective scale). If provided with scalar pwt and without sd, pwt is recomputed from n_prior. With vector pwt or with sd, pwt is left unchanged and n_prior is used for the Gamma prior on precision and related Gaussian calibration only. If missing and pwt is scalar, n_prior = (pwt/(1-pwt))*n_effective.

sd

Optional vector argument with the prior standard deviations for the coefficients

dispersion

Optional scalar dispersion override (default NULL). For now, this is documented as an optional argument used to scale the Sigma (variance-covariance) matrix; see Details for additional context.

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).

mu

Optional vector argument with the prior means for the coefficients

k

Scalar (default 1), non-negative (k≥0k \geq 0), with k+p≥2k + p \geq 2 where pp is the number of coefficients (columns of the model matrix). k controls the tail behavior and effective degrees of freedom of the variance prior. It does not change the posterior mean of σ2\sigma^2 or the covariance of β\beta, but larger k makes the prior and posterior for σ2\sigma^2 more concentrated and less heavy-tailed. Not yet used in calibration; passed through to compute_gaussian_prior for future use.

...

For glm: arguments to be used to form the default control argument if it is not supplied directly.

For weights: further arguments passed to or from other methods.

Details

Inputs to the function

The inputs to Prior_Setup() fall into three conceptual categories:

1. Model specification

  • formula: structure of the GLM (response and predictors).

  • family: error distribution and link.

  • data, weights, subset, na.action, offset, contrasts, control, ...: as in glm.

2. Prior variance–covariance specification

  • pwt: prior weight relative to the likelihood. If scalar, used to construct a Zellner-type g-prior. If vector, applied elementwise.

  • n_prior: optional scalar effective prior sample size. Replaces scalar pwt only when pwt is scalar and sd is not used; otherwise supplies precision-prior / calibration only.

  • sd: optional vector of prior standard deviations. If provided, used to compute pwt from the diagonal of vcov(glm_full).

  • pwt_default_low, pwt_default_high: defaults for pwt when not supplied.

3. Prior mean specification

  • intercept_source: method for setting the prior mean of the intercept ("null_model" or "full_model").

  • effects_source: method for setting the prior mean of the effects ("null_effects" or "full_model").

  • mu: optional user-specified prior mean vector; overrides other centering logic if provided.

Prior covariance and Zellner scaling

Let V0=vcov(β^)V_0 = \mathrm{vcov}(\hat\beta) be the covariance matrix of the full-model GLM coefficients. For non-Gaussian families, the prior covariance is:

Σ={1−pwtpwtV0,scalar pwt,V0∘[1−pwtipwti1−pwtjpwtj],vector pwt,\Sigma = \begin{cases} \dfrac{1 - \mathrm{pwt}}{\mathrm{pwt}} V_0, & \text{scalar pwt},\\[4pt] V_0 \circ \left[\sqrt{\dfrac{1 - \mathrm{pwt}_i}{\mathrm{pwt}_i}} \sqrt{\dfrac{1 - \mathrm{pwt}_j}{\mathrm{pwt}_j}}\right], & \text{vector pwt}, \end{cases}

where ∘\circ denotes elementwise multiplication.

Intercept-only Poisson(link = "identity") conjugate prior on the rate

When the design is a single column (intercept only), family = poisson(), link = "identity", scalar pwt, and offsets are zero, the effective conjugate prior observation count npriorn_{\mathrm{prior}} already satisfies nprior/(nprior+neffective)=pwtn_{\mathrm{prior}} / (n_{\mathrm{prior}} + n_{\mathrm{effective}}) = \mathrm{pwt} (otherwise conj_poisson remains NULL with a warning). Writing the weighted mean yˉw=∑iwiyi/∑iwi\bar{y}_w = \sum_i w_i y_i / \sum_i w_i, the output list component conj_poisson stores shape=nprioryˉw\texttt{shape} = n_{\mathrm{prior}} \bar{y}_w and rate=nprior\texttt{rate} = n_{\mathrm{prior}}, so the prior mean for the rate matches yˉw\bar{y}_w. Omitting optional mu resets the surrogate Normal summaries mu and the sole diagonal element of Sigma to these Gamma moments (Sigma_{11} = yˉw/nprior\bar{y}_w / n_{\mathrm{prior}}).

For Gaussian families, Prior_Setup() also constructs the dispersion-free covariance

Σ0=Σ/dispersion,\Sigma_0 = \Sigma / \texttt{dispersion},

which under scalar pwt and the default calibration reduces to

Σ0=1−pwtpwt(X⊤WX)−1.\Sigma_0 = \frac{1 - \mathrm{pwt}}{\mathrm{pwt}} (X^\top W X)^{-1}.

Gaussian Normal–Gamma calibration and SmargS_{\mathrm{marg}}

For family = gaussian(), the function performs the Normal–Gamma calibration described in (Nygren 2025). Let:

  • p=ncol(x)p = \texttt{ncol}(x),

  • neffective=∑iwin_{\mathrm{effective}} = \sum_i w_i,

  • β^\hat\beta the weighted least-squares estimator,

  • Σ0\Sigma_0 the dispersion-free prior covariance.

The marginal quadratic term is

Smarg=RSSw+(β^−μ)⊤(Σ0+(X⊤WX)−1)−1(β^−μ),S_{\mathrm{marg}} = \mathrm{RSS}_w + (\hat\beta - \mu)^\top \left(\Sigma_0 + (X^\top W X)^{-1}\right)^{-1} (\hat\beta - \mu),

where RSSw\mathrm{RSS}_w is the weighted residual sum of squares at β^\hat\beta. Under the default scalar-pwt Zellner mapping Σ0=1−pwtpwt(X⊤WX)−1\Sigma_0 = \frac{1 - \mathrm{pwt}}{\mathrm{pwt}}(X^\top W X)^{-1}, this simplifies to

Smarg=RSSw+pwt (β^−μ)⊤(X⊤WX)(β^−μ),S_{\mathrm{marg}} = \mathrm{RSS}_w + \mathrm{pwt}\, (\hat\beta - \mu)^\top (X^\top W X)(\hat\beta - \mu),

which makes the limiting behavior as pwt→0\mathrm{pwt} \to 0 transparent.

The calibrated dispersion is

dispersion=Smargneffective−p,\texttt{dispersion} = \frac{S_{\mathrm{marg}}}{n_{\mathrm{effective}} - p},

and the Normal–Gamma hyperparameters are

shape=nprior+k2,rate=12Smargnprior+k+p−2neffective−p.\text{shape} = \frac{n_{\mathrm{prior}} + k}{2},\qquad \text{rate} = \frac{1}{2} S_{\mathrm{marg}} \frac{n_{\mathrm{prior}} + k + p - 2}{n_{\mathrm{effective}} - p}.

The independent Normal–Gamma shape is

shapeING=shape+p2.\text{shape}_{ING} = \text{shape} + \frac{p}{2}.

Posterior summaries for the conjugate Normal–Gamma prior

Under the conjugate Normal–Gamma prior (used by dNormal_Gamma()), the posterior has:

  • Posterior mean

    E[β∣y]=(1−pwt) β^+pwt μ.E[\beta \mid y] = (1 - \mathrm{pwt})\,\hat\beta + \mathrm{pwt}\,\mu.

  • Posterior expectation of σ2\sigma^2

    E[σ2∣y]=Smargneffective−p.E[\sigma^2 \mid y] = \frac{S_{\mathrm{marg}}}{n_{\mathrm{effective}} - p}.

  • Posterior covariance

    Cov(β∣y)=E[σ2∣y] (Σ0−1+X⊤WX)−1.\mathrm{Cov}(\beta \mid y) = E[\sigma^2 \mid y]\, \left(\Sigma_0^{-1} + X^\top W X\right)^{-1}.

Weak-prior limits (Theorems 2 and 3)

As nprior→0+n_{\mathrm{prior}} \to 0^+ (equivalently pwt→0\mathrm{pwt} \to 0), Smarg→RSSwS_{\mathrm{marg}} \to \mathrm{RSS}_w, and the conjugate Normal–Gamma posterior converges to the classical weighted least-squares limit:

E[β∣y]→β^,E[σ2∣y]→RSSwneffective−p,Cov(β∣y)→RSSwneffective−p(X⊤WX)−1.E[\beta \mid y] \to \hat\beta,\qquad E[\sigma^2 \mid y] \to \frac{\mathrm{RSS}_w}{n_{\mathrm{effective}} - p},\qquad \mathrm{Cov}(\beta \mid y) \to \frac{\mathrm{RSS}_w}{n_{\mathrm{effective}} - p} (X^\top W X)^{-1}.

For the independent Normal–Gamma prior used by dIndependent_Normal_Gamma(), neither the posterior mean nor the posterior covariance is available in closed form; the posterior must be obtained by numerical integration or sampling (e.g., rindepNormalGamma_reg()). Theorem 3 in (Nygren 2025) shows that the ING posterior has the same weak-prior limit as the conjugate Normal–Gamma posterior:

E[β∣y]→β^,Cov(β∣y)→RSSwneffective−p(X⊤WX)−1.E[\beta \mid y] \to \hat\beta,\qquad \mathrm{Cov}(\beta \mid y) \to \frac{\mathrm{RSS}_w}{n_{\mathrm{effective}} - p} (X^\top W X)^{-1}.

Value

A list of class "PriorSetup" with components:

mu

Prior mean vector (length equal to the number of coefficients).

Sigma

Coefficient-scale prior variance–covariance matrix.

Sigma_0

For family = gaussian() only: dispersion-independent prior covariance on the precision-weighted coefficient scale (the Sigma_0 passed to compute_gaussian_prior). Under scalar pwt, Σ0−1=pwt1−pwtX⊤WX\Sigma_0^{-1} = \frac{\mathrm{pwt}}{1-\mathrm{pwt}} X^\top W X.

dispersion

Calibrated dispersion (Gaussian models only), equal to Smarg/(neffective−p)S_{\mathrm{marg}}/(n_{\mathrm{effective}} - p) under the default calibration.

shape

Derived prior Gamma shape parameter for the Normal–Gamma prior on precision (Gaussian only), (nprior+k)/2(n_{\mathrm{prior}} + k)/2.

shape_ING

For gaussian() only when shape is available: dedicated shape parameter for dIndependent_Normal_Gamma(), shape+p/2\texttt{shape} + p/2.

rate

Derived prior Gamma rate parameter (Gaussian only), using the calibrated SmargS_{\mathrm{marg}}.

rate_gamma

For gaussian() only, when Gaussian calibration runs: prior Gamma rate for dGamma() / fixed-β\beta use, based on RSSw(β⋆)\mathrm{RSS}_w(\beta_\star) at the Zellner blend.

coefficients

Named numeric vector of returned coefficient values. For gaussian() with scalar or vector pwt, this is the closed-form posterior-mean blend (1−pwt)β^+pwtμ(1-\mathrm{pwt})\hat\beta + \mathrm{pwt}\mu when inputs are valid; otherwise it falls back to the full-model GLM coefficients.

model

The model frame used to construct the design matrix (if model = TRUE).

x

The model matrix used (if x = TRUE).

y

The response vector used (if y = TRUE).

call

The matched call to Prior_Setup().

PriorSettings

A list containing prior configuration details, including pwt, n_prior, n_effective, n_likelihood, intercept_source, and effects_source.

conj_poisson

NULL unless family = poisson(link = "identity") with a single-column design (intercept-only), scalar pwt, finite positive scalar n_prior, and negligible offset. Then a named list with conjugate Gamma(shape, rate) hyperparameters on the Poisson rate, with shape=nprior yˉw\texttt{shape} = n_{\mathrm{prior}}\,\bar{y}_w, rate=nprior\texttt{rate} = n_{\mathrm{prior}}, and beta centered at the weighted sample mean yˉw\bar{y}_w; pass to dGamma with Inv_Dispersion = FALSE. See Details.

References

Nygren K (2025). “Core 10: Technical Derivations for Priors Returned by Prior_ Setup.” Vignette in the glmbayesCore R package. R vignette name: Core-10; formerly glmbayes appendix Chapter-A12.

See Also

pfamily for prior-family objects and the constructors dNormal, dNormal_Gamma, dGamma, and dIndependent_Normal_Gamma.

rglmb, rlmb for matrix-input fits with a pfamily built from Prior_Setup() output; rglmb, rlmb for matrix-based sampling that consumes the same prior structure; simfuncs for functions that take a prior_list assembled from those components (including rindepNormalGamma_reg for dIndependent_Normal_Gamma()). multi_prior_setup for a matrix/cbind response with Gaussian; use with rlmb Prior_Setup per column.

(Zellner 1986); (Raiffa and Schlaifer 1961); (Gelman et al. 2013); (McCullagh and Nelder 1989); (Nygren 2025); (Nygren 2025).

Other prior: Prior_Check(), multi_prior_setup()

Examples

## Not run: 
## Full runnable examples are maintained in \pkg{glmbayesCore}:
example(Prior_Setup, package = "glmbayesCore", ask = FALSE, echo = TRUE)

## End(Not run)

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

Examples

## Prior_Setup_GLMM() — Block~1 / Block~2 prior calibration (Ex_13b data)
##
## Same formula and cleaning as
## demo("Ex_13b_rLMM_estimated_dispersion_known_vcov_BigWordClub_PartVI").
## 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)

  ## Keep full-rank schools (sampler / per-group ING path)
  design0 <- model_setup(form_lmer, data = dat)
  dat <- subset(dat, school_id %in% names(design0$groupef.rank)[design0$groupef.rank])
  dat$school_id <- droplevels(dat$school_id)

  ## Pooled measurement dispersion (dispformula = ~1, default)
  ps_pooled <- Prior_Setup_GLMM(
    form_lmer,
    data = dat,
    pop.pwt = 0.01
  )
  print(ps_pooled)
  names(ps_pooled$pop.prior_list)

  ## Per-group Block~1 calibration (Ex_13b-style dispformula).
  ## group.alpha_target = NULL skips the ellipsoid pwt search (faster for examples);
  ## Ex_13b uses group.alpha_target = 0.01 with group.dispersion.pwt = 0.1 as floor.
  ps_group <- Prior_Setup_GLMM(
    form_lmer,
    data = dat,
    pop.pwt = 0.01,
    dispformula = ~school_id,
    group.max_disp_perc = 0.8,
    group.dispersion.pwt = 0.1,
    group.alpha_target = NULL
  )
  print(ps_group)
  stopifnot(is.list(ps_group$group.ing_prior[[1L]]))
  length(ps_group$group.ing_prior)
}

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


Extract random effects from Bayesian mixed model fits

Description

Methods mirroring ranef for lmerb and glmerb fits. Values summarize Block~1 random-effects draws bb stored in fit$groupef (posterior chain mean by default, or the exact ICM fit$groupef.mode).

Usage

## S3 method for class 'lmerb'
ranef(object, condVar = FALSE, drop = FALSE, type = c("mean", "mode"), ...)

## S3 method for class 'glmerb'
ranef(object, condVar = FALSE, drop = FALSE, type = c("mean", "mode"), ...)

Arguments

object

An lmerb or glmerb object.

condVar

Logical; if TRUE, attach a postVar attribute (posterior variance from stored draws, diagonal by RE term).

drop

Logical; if TRUE, convert single-column grouping data.frames to named vectors (as in lme4).

type

"mean" (default when draws are stored) or "mode" (ICM / groupef.mode). When simulate = FALSE, "mean" falls back to "mode" with a warning.

...

Ignored.

Value

An object of class "ranef.lmerb": a named list with one data.frame per grouping factor (rows = levels, columns = RE terms). Attributes include type, n_draws, and optionally postVar.

See Also

ranef, coef.lmerb, fixef.lmerb, lmerb.


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.

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


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.

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


Summarize per-group measurement dispersion (sigma^2)

Description

Per-group observation-level σ2\sigma^2 summaries when dispersion_ranef is per-group: either a named list of dGamma() priors (dispersion_mode == "gamma_list") or a named numeric vector of fixed, known values (dispersion_mode == "fixed_vector").

For "gamma_list", the layout mirrors the Block~2 τk2\tau^2_k tables in summary.lmerb: a prior reference table (with a common lmer/glmer residual column), an optional posterior overview from stored draws (including per-group Cand/draw, envelope candidates per inner sweep), and per-group distribution percentiles of the σ2\sigma^2 draws (1\ 99\ the prior table shows whether the simulation tends to pile up against the truncation bounds. The prior table also reports each group's implied effective prior sample size (n_prior, back-computed from shape_ING under the Prior_Setup() default k = 1 calibration) and observed data sample size (n_data); the overview table reports their sum (n_total).

For "fixed_vector", there is no prior to calibrate and nothing is sampled (each group's σj2\sigma^2_j is known and constant throughout), so only a single table of the fixed per-group values next to the lmer/glmer residual variance is returned; overview and percentiles are NULL.

Usage

summary_sigma2(object, ...)

## S3 method for class 'lmerb'
summary_sigma2(
  object,
  type = c("both", "prior", "overview"),
  digits = max(3L, getOption("digits") - 3L),
  ...
)

## S3 method for class 'glmerb'
summary_sigma2(
  object,
  type = c("both", "prior", "overview"),
  digits = max(3L, getOption("digits") - 3L),
  ...
)

## S3 method for class 'summary.sigma2.lmerb'
print(x, digits = NULL, ...)

## S3 method for class 'summary.sigma2.lmerb'
as.data.frame(x, row.names = NULL, ...)

Arguments

object

An lmerb or glmerb fit.

type

"both" (default), "prior", or "overview".

digits

Number of significant digits for printing.

x

An object of class "summary.sigma2.lmerb".

row.names

Ignored.

...

Ignored.

Value

An object of class "summary.sigma2.lmerb" with components prior, overview, percentiles (overview and percentiles are always NULL for "fixed_vector"), group_name, n_groups, simulated, mode, and mer_label. When window_diagnostics were attached by dGamma_list(), the prior table includes blup_infl, R_lo, R_hi, and asymmetric_window.

See Also

summary.lmerb, lmerb.


Summarize bglmb fits

Description

Summary method for row-block glmb fits (class "bglmb"). summary.bglmb applies summary.glmb to each block; print.summary.bglmb follows summary.mlmb / summary.blmb with per-block sections.

Usage

## S3 method for class 'bglmb'
summary(object, ...)

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

Arguments

object

An object of class "bglmb" from glmbBlock.

x

An object of class "summary.bglmb".

digits

Number of significant digits for printing.

...

Passed to summary.glmb or print methods.

Value

summary.bglmb returns a named list of "summary.glmb" objects with class "summary.bglmb".

See Also

glmbBlock, lmbBlock, summary.mlmb, summary.blmb, summary.glmb


Summarize blmb fits

Description

Summary method for row-block lmb fits (class "blmb").

Usage

## S3 method for class 'blmb'
summary(object, ...)

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

Arguments

object

An object of class "blmb" from lmbBlock.

x

An object of class "summary.blmb".

digits

Number of significant digits for printing.

...

Passed to summary.glmb or print methods.


Summarize Bayesian linear mixed model fits

Description

Methods for lmerb fits. summary.lmerb builds Block~2 (level-2 fixed effect) tables per random-effects component, following the layout of summary.glmb and the multi-response structure of summary.mlmb.

Usage

## S3 method for class 'lmerb'
summary(object, groups = NULL, digits = max(3L, getOption("digits") - 3L), ...)

## S3 method for class 'glmerb'
summary(object, groups = NULL, digits = max(3L, getOption("digits") - 3L), ...)

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

Arguments

object

An object of class "lmerb" or "glmerb".

groups

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

digits

Number of significant digits for printing.

x

An object of class "summary.lmerb".

...

Ignored.

Value

summary.lmerb returns an object of class "summary.lmerb", a list with components call, formula, n, simulated, varcor, fixef_prior_overview (stacked prior and glmer/lmer reference across RE components), fixef_overview (Block~2 hyperparameters with posterior summaries and Pr(Prior_tail)), fixef_percentiles_overview (stacked distribution percentiles across RE components, when simulated), popef (per-RE-component tables; not printed, available on the returned object), ranef_overview, ranef.iters.mean (Block~1 envelope candidates per inner sweep, averaged over groups; printed separately from the overview table), any_non_normal, tau2_prior_overview (per-component τk2\tau^2_k prior reference: Block~2 pfamily name (Prior), 1/E[1/τ2]1/E[1/\tau^2], E[τ2]E[\tau^2], truncation window, sqrt(E[tau2]), and lmer/glmer MLE), sim_method_used ("DEFAULT" for the exact-iid engine, "TWO_BLOCK_GIBBS" for the two-block Gibbs engine, or NULL when simulated is FALSE; see lmerb's sim_method), tau2_overview and tau2_percentiles_overview (posterior mode, mean, SD on the variance scale, Mean SD, and tau^2 quantiles when simulated), tau2_sd_percentiles_overview (2.5\ sqrt(tau^2) draws vs lmer/glmer SD), a Residual row for observation-level σ2\sigma^2 when dispersion_ranef is supplied (same columns as the τk2\tau^2_k rows; not used for per-group gamma_list priors — see summary_sigma2), and optionally ranef_groups.

See Also

lmerb, glmerb, print.lmerb, summary_sigma2, summary.glmb, summary.mlmb


Summarize ranef.lmerb objects

Description

Summarize ranef.lmerb objects

Usage

## S3 method for class 'ranef.lmerb'
summary(object, groups = NULL, digits = 4L, ...)

Arguments

object

An object of class "ranef.lmerb".

groups

Optional character vector of grouping levels to highlight.

digits

Number of digits for printing.

...

Ignored.

Value

A data.frame (invisibly) with long-format random effects.