rtmb_lmer: RTMB-based Linear Mixed Model (LMM) wrapper function

View source: R/wrapper_lm_glm_lmer.R

rtmb_lmerR Documentation

RTMB-based Linear Mixed Model (LMM) wrapper function

Description

RTMB-based Linear Mixed Model (LMM) wrapper function

Usage

rtmb_lmer(
  formula,
  data = NULL,
  laplace = TRUE,
  prior = prior_flat(),
  y_range = NULL,
  init = NULL,
  fixed = NULL,
  gmc = NULL,
  centering = NULL,
  cwc = NULL,
  std = FALSE,
  view = NULL,
  sigma_by = NULL,
  factors = NULL,
  contrasts = "sum",
  resid_corr = NULL,
  resid_time = NULL,
  resid_group = NULL,
  within = NULL,
  missing = c("listwise", "fiml"),
  WAIC = FALSE,
  ...
)

Arguments

formula

Formula

data

Optional data frame. If omitted, variables are resolved from the formula environment. In this mode, use bare variable names; formulas using $, [[, or . require an explicit data argument.

laplace

Logical; whether to marginalize random effects using Laplace approximation

prior

An object of class '"rtmb_prior"'. Use 'prior_flat()' for no prior, 'prior_normal()' for default normal/exponential priors, 'prior_jzs()' for JZS regression priors, or 'prior_weak()', 'prior_rhs()', 'prior_ssp()' for weakly informative or regularized Bayesian inference. Default is 'prior_flat()'.

y_range

Theoretical minimum and maximum values of the response variable

init

Initial values

fixed

Optional named list of fixed values for specific parameters.

gmc

Character vector of variable names for GMC

centering

Alias for 'gmc'.

cwc

List for CWC

std

Logical; if 'TRUE', also report post-hoc standardized fixed-effect coefficients. All fixed-effect design-matrix columns are standardized.

view

Character vector of parameter names to prioritize in summary.

sigma_by

Character vector specifying variables to group residual variance by (heteroscedasticity).

factors

Character vector of variable names to be treated as factors.

contrasts

Character string specifying the contrast type ("treatment" or "sum"). The default is '"sum"' for compatibility with ANOVA-style use of 'rtmb_lmer()'.

resid_corr

Residual correlation structure (e.g., "ar1", "cs", "un", "toep").

resid_time

Variable name for time points in residual correlation.

resid_group

Variable name for grouping in residual correlation.

within

Optional list for wide-to-long conversion. For repeated measures data in wide format, specify the factor names and their levels, e.g., list(Time = 4) or list(A = 2, B = 3). The total number of levels must match the number of columns in cbind() on the LHS. If omitted and the LHS is cbind(), the within-factor name is inferred from RHS variables not present in the data.

missing

Missing value handling strategy: "listwise".

WAIC

Logical; if TRUE, add pointwise 'log_lik' to the generate block for WAIC.

...

Additional arguments passed to rtmb_glmer().

Value

RTMB_Model object

Examples

  # --- 1. Linear Regression (rtmb_lm) ---
  # Fit a linear regression model using the debate dataset
  data(debate, package = "BayesRTMB")
  fit_lm <- rtmb_lm(sat ~ talk + perf, data = debate)
  map_lm <- fit_lm$optimize()
  map_lm$summary()

  # --- 2. Generalized Linear Model (rtmb_glm) ---
  # Fit a logistic regression model using the debate dataset
  data(debate, package = "BayesRTMB")
  fit_glm <- rtmb_glm(cond ~ talk + sat, data = debate, family = "bernoulli")
  map_glm <- fit_glm$optimize()
  map_glm$summary()

  # --- 3. Generalized Linear Mixed Model (rtmb_glmer) ---
  # Fit a linear mixed-effects model using the debate dataset
  data(debate, package = "BayesRTMB")
  fit_glmer <- rtmb_glmer(talk ~ cond + (1 | group), data = debate, family = "gaussian")

  # MAP estimation using Laplace approximation for random effects
  map_glmer <- fit_glmer$optimize(laplace = TRUE)
  map_glmer$summary()

  # MCMC sampling (chains and iterations reduced for faster execution)
  
  mcmc_glmer <- fit_glmer$sample(sampling = 500, warmup = 500, chains = 2)
  mcmc_glmer$summary()
  

  # --- 4. Linear Mixed Model (rtmb_lmer) ---
  # A convenient wrapper for Gaussian mixed models (identical to rtmb_glmer with family="gaussian")
  fit_lmer <- rtmb_lmer(sat ~ talk + (1 | group), data = debate)
  map_lmer <- fit_lmer$optimize()
  map_lmer$summary()

  # --- 5. Regularized Regression (Variable Selection) ---
  # You can apply regularization to the fixed effects to shrink noise variables towards zero.
  # Use prior = prior_rhs() for the Regularized Horseshoe prior,
  # or prior_ssp() for the Spike-and-Slab prior.
  # Note: When using regularization, you must specify 'y_range' (the theoretical minimum and maximum
  # values of the response variable) to automatically set up the required weakly informative priors.

  # Fit a linear regression using debate predictors with the Horseshoe prior
  fit_rhs <- rtmb_lm(
    sat ~ talk + perf + skill,
    data = debate,
    prior = prior_rhs(),
    y_range = c(1, 5)
  )
  map_rhs <- fit_rhs$optimize()
  # Summarize only the fixed effects (slopes)
  map_rhs$summary("b")

  # Fit a linear regression with the Spike-and-Slab prior
  fit_ssp <- rtmb_lm(
    sat ~ talk + perf + skill,
    data = debate,
    prior = prior_ssp(),
    y_range = c(1, 5)
  )
  map_ssp <- fit_ssp$optimize()
  map_ssp$summary("b")

BayesRTMB documentation built on Nov. 24, 2026, 9:06 a.m.