#----------------------------------------------------------------------------
# Predictor Variables in Single-Level Data
# Example 1a: Center predictor 'disp' at the grand mean
center(mtcars, disp, append = FALSE)
# Alternative specification without using the '...' argument
center(mtcars$disp)
# Example 1b: Center predictors 'disp' and 'hp' at the grand mean and append to 'mtcars'
center(mtcars, disp, hp)
# Alternative specification without using the '...' argument
cbind(mtcars, center(mtcars[, c("disp", "hp")]))
# Example 1c: Center predictor 'disp' at the value 3
center(mtcars, disp, value = 3)
# Example 1d: Center predictors 'disp' and 'hp' and label with the suffix ".v"
center(mtcars, disp, hp, name = ".v")
#----------------------------------------------------------------------------
# Predictor Variables in Two-Level Data
# Load data set "Demo.twolevel" in the lavaan package
data("Demo.twolevel", package = "lavaan")
# Example 2a: Center L1 predictor 'y1' within cluster
center(Demo.twolevel, y1, cluster = "cluster")
# Alternative specification without using the '...' argument
center(Demo.twolevel$y1, cluster = Demo.twolevel$cluster)
# Example 2b: Center L2 predictor 'w2' at the grand mean
center(Demo.twolevel, w1, cluster = "cluster")
# Example 2c: Center L1 predictor 'y1' within cluster and L2 predictor 'w1' at the grand mean
center(Demo.twolevel, y1, w1, cluster = "cluster")
#----------------------------------------------------------------------------
# Predictor Variables in Three-Level Data
# Create arbitrary three-level data
Demo.threelevel <- data.frame(Demo.twolevel, cluster2 = Demo.twolevel$cluster,
cluster3 = rep(1:10, each = 250))
# Example 3a: Center L1 predictor 'y1' within L2 cluster
center(Demo.threelevel, y1, cluster = c("cluster3", "cluster2"))
# Example 3b: Center L1 predictor 'y1' within L3 cluster
center(Demo.threelevel, y1, cluster = c("cluster3", "cluster2"), cwc.mean = "L3")
# Example 3c: Center L1 predictor 'y1' within L2 cluster and L2 predictor 'w1' within L3 cluster
center(Demo.threelevel, y1, w1, cluster = c("cluster3", "cluster2"))
Run the code above in your browser using DataLab