mlm_center: Center variables for multilevel modeling

View source: R/mlm_center.R

mlm_centerR Documentation

Center variables for multilevel modeling

Description

Performs grand-mean centering, group-mean centering, or both (within-between decomposition) on one or more variables in a data frame. Group-mean centering is the standard preparation for cross-level interaction models.

Usage

mlm_center(
  data,
  vars,
  cluster = NULL,
  type = c("grand", "group", "both"),
  suffix_within = "_within",
  suffix_between = "_between"
)

Arguments

data

A data frame.

vars

Character vector of variable names to center.

cluster

Character scalar: name of the clustering variable (required when type is "group" or "both").

type

One of "grand", "group", or "both".

  • "grand": subtract the overall mean.

  • "group": subtract the cluster mean (within-person / within-school centering).

  • "both": return both the within-cluster-centered value and the cluster mean (between component), appended as new columns.

suffix_within

Suffix appended to within-centered variable names when type = "both". Default is "_within".

suffix_between

Suffix appended to between (cluster mean) variable names when type = "both". Default is "_between".

Value

The input data frame with new centered columns appended. Original columns are not modified.

Examples

data(school_data)

# Grand-mean center SES
d1 <- mlm_center(school_data, vars = "ses", type = "grand")
head(d1[, c("ses", "ses_c")])

# Group-mean center SES within schools
d2 <- mlm_center(school_data, vars = "ses", cluster = "school", type = "group")
head(d2[, c("ses", "ses_c")])

# Within-between decomposition
d3 <- mlm_center(school_data, vars = "ses", cluster = "school", type = "both")
head(d3[, c("ses", "ses_within", "ses_between")])


mlmoderator documentation built on April 4, 2026, 1:07 a.m.