Skip to contents

Converts an mlmr model specification into an lme4-compatible model formula, including centered predictor names, interactions, and random effects.

Usage

mlm_formula(spec)

Arguments

spec

An "mlm_spec" object created with mlm_spec.

Value

A model formula.

Details

The formula uses the effective predictor names created by the centering choices. For example, a predictor with center = "CWC" appears with a _CWC suffix. Correlated random slopes are represented with (1 + slope | group) syntax, whereas independent random slopes are represented with separate (1 | group) and (0 + slope | group) terms.

Examples

dat <- example_hsb(n_schools = 4, min_students = 5, max_students = 6)
spec <- mlm_spec(
  outcome = "mathscore",
  fixed = list(ses = list(center = "CWC")),
  grouping = list(schoolid = "schoolid"),
  random = list(schoolid = list(intercept = TRUE, slopes = "ses",
    correlation = TRUE)),
  data = dat
)
mlm_formula(spec)
#> mathscore ~ ses_CWC + (1 + ses_CWC | schoolid)
#> <environment: 0x55a9d20b7150>