#--------------------------------------
# Predictors in a single-level regression
dat.sl <- data.frame(x1 = c(4, 2, 5, 6, 3, 4, 1, 3, 4),
x2 = c(3, 1, 2, 6, 4, 8, 3, 2, 1),
y = c(5, 3, 6, 3, 4, 5, 2, 6, 5))
# Center predictor at the sample mean
center(dat.sl$x1)
# Center predictors at the sample mean and attach to 'dat.sl'
dat.sl <- data.frame(dat.sl,
center(dat.sl[, c("x1", "x2")]))
# Center predictor at the value 3
center(dat.sl$x1, value = 3)
# Center predictors at the value 3 and attach to 'dat.sl'
dat.sl <- data.frame(dat.sl,
center(dat.sl[, c("x1", "x2")], value = 3, names = ".v"))
#--------------------------------------
# Predictors in a multilevel regression
dat.ml <- data.frame(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
cluster = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
x1.l1 = c(4, 2, 5, 6, 3, 4, 1, 3, 4),
x2.l1 = c(1, 4, 2, 3, 5, 7, 8, 7, 5),
x1.l2 = c(4, 4, 4, 1, 1, 1, 3, 3, 3),
x2.l2 = c(5, 5, 5, 2, 2, 2, 7, 7, 7),
y = c(5, 3, 6, 3, 4, 5, 2, 6, 5))
# Center level-1 predictor at the grand mean (CGM)
center(dat.ml$x1.l1)
# Center level-1 predictors at the grand mean (CGM) and attach to 'dat.ml'
dat.ml<- cbind(dat.ml,
center(dat.ml[, c("x1.l1", "x2.l1")], names = ".cgm"))
# Center level-1 predictor within cluster (CWC)
center(dat.ml$x1.l1, type = "CWC", cluster = dat.ml$cluster)
# Center level-1 predictors within cluster (CWC) and attach to 'dat.ml'
dat.ml <- cbind(dat.ml,
center(dat.ml[, c("x1.l1", "x2.l1")], type = "CWC",
cluster = dat.ml$cluster, names = ".cwc"))
# Center level-2 predictor at the grand mean (CGM)
center(dat.ml$x1.l2, type = "CGM", cluster = dat.ml$cluster)
# Center level-2 predictors at the grand mean (CGM) and attach to 'dat.ml'
dat.ml <- cbind(dat.ml,
center(dat.ml[, c("x1.l2", "x2.l2")], type = "CGM",
cluster = dat.ml$cluster, names = ".cgm"))
Run the code above in your browser using DataLab