#----------------------------------------------------------------------------
# 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")
#----------------------------------------------------------------------------
# Two-Level Data
# Load data set "Demo.twolevel" in the lavaan package
data("Demo.twolevel", package = "lavaan")
#.........................................
# Level-1 (L1) Predictor
# Example 2a: Center L1 predictor 'y1' within L2 clusters
center(Demo.twolevel, y1, cluster = "cluster", append = FALSE)
# Alternative specification without using the '...' argument
center(Demo.twolevel$y1, cluster = Demo.twolevel$cluster)
# Example 2b: Center L1 predictor 'y1' at the grand-mean
# Note that cluster ID is ignored when type = "CGM"
center(Demo.twolevel, y1, cluster = "cluster", type = "CGM")
# Alternative specification
center(Demo.twolevel, y1)
#.........................................
# Level-2 (L2) Predictor
# Example 2c: Center L2 predictor 'w2' at the average L2 cluster scores
# Note that cluster ID is needed
center(Demo.twolevel, w1, cluster = "cluster")
#.........................................
# L1 and L2 Predictors
# Example 2d: Center L1 predictor 'y1' within L2 clusters
# and L2 predictor 'w1' at the average L2 cluster scores
center(Demo.twolevel, y1, w1, cluster = "cluster")
#.........................................
# Two-Step Latent Mean Centering
# Example 2e: Decompose L1 predictor 'y1' as within-between components
center(Demo.twolevel, y1, cluster = "cluster", type = "latent")
# Example 2d: Decompose L1 predictor 'y1' as within-between components
# label variables as 'l1.y1' and 'l2.y1'
center(Demo.twolevel, y1, cluster = "cluster", type = "latent", name = c("l1.y1", "l2.y1"))
if (FALSE) {
#----------------------------------------------------------------------------
# Three-Level Data
# Load data set "Demo.twolevel" in the lavaan package
data("Demo.twolevel", package = "lavaan")
# Create arbitrary three-level data
Demo.threelevel <- data.frame(Demo.twolevel, cluster2 = Demo.twolevel$cluster,
cluster3 = rep(1:10, each = 250))
# Compute L3 cluster scores for the L2 predictor 'w1'
Demo.threelevel <- cluster.scores(Demo.threelevel, w1, cluster = "cluster3", name = "w1.l3")
#.........................................
# Level-1 (L1) Predictor
# Example 3a: Center L1 predictor 'y1' within L2 clusters (CWC L2)
# Note that L3 cluster IDs are ignored when type = "CWC"
center(Demo.threelevel, y1, cluster = c("cluster3", "cluster2"))
# Alternative specification when L2 cluster IDs are unique across L3 clusters
center(Demo.threelevel, y1, cluster = "cluster2")
# Example 3b: Center L1 predictor 'y1' within L3 clusters (CWC L3)
# Note that both L3 and L2 cluster IDs are needed
center(Demo.threelevel, y1, cluster = c("cluster3", "cluster2"), cwc.mean = "L3")
# Example 3c: Center L1 predictor 'y1' at the grand-mean (CGM)
# Note that the cluster argument is ignored when type = "CGM",
center(Demo.threelevel, y1, cluster = c("cluster3", "cluster2"), type = "CGM")
# Alternative specification
center(Demo.threelevel, y1)
#.........................................
# Level-2 (L2) Predictor
# Example 3d: Center L2 predictor 'w1' within L3 cluster
# Note that both L3 and L2 cluster IDs are needed
center(Demo.threelevel, w1, cluster = c("cluster3", "cluster2"))
# Example 3e: Center L2 predictor 'w1' at the grand-mean (CGM)
# Note that both L3 and L2 cluster IDs are needed
center(Demo.threelevel, y1, cluster = c("cluster3", "cluster2"), type = "CGM")
#.........................................
# Level-3 (L3) Predictor
# Example 3f: Center L3 predictor 'w1.l3' at the average L3 cluster scores
# Note that L2 cluster ID is ignored
center(Demo.threelevel, w1.l3, cluster = c("cluster3", "cluster2"))
# Alternative specification
center(Demo.threelevel, w1.l3, cluster = "cluster3")
#.........................................
# L1, L2, and L3 Predictors
# Example 3g: Center L1 predictor 'y1' within L2 cluster, L2 predictor 'w1' within
# L3 clusters, and L3 predictor 'w1.l3' at the average L3 cluster scores
center(Demo.threelevel, y1, w1, w1.l3, cluster = c("cluster3", "cluster2"))
#.........................................
# Two-Step Latent Mean Centering
# Load data set "Demo.twolevel" in the lavaan package
data("Demo.twolevel", package = "lavaan")
# Create arbitrary three-level data
Demo.threelevel <- data.frame(Demo.twolevel, cluster2 = Demo.twolevel$cluster,
cluster3 = rep(1:10, each = 250))
# Example 3h: Decompose L1 predictor 'y1' as within-between components
center(Demo.threelevel, y1, cluster = "cluster2", type = "latent")
# Example 3i: Decompose L1 predictor 'y1' as within-between components
# label variables as 'l1.y1' and 'l2.y1'
center(Demo.threelevel, y1, cluster = "cluster2", type = "latent",
name = c("l1.y1", "l2.y2"))
# Example 3j: Decompose L1 predictor 'y1' as within-between components
center(Demo.threelevel, y1, cluster = c("cluster3", "cluster2"), type = "latent")
# Example 3k: Decompose L1 predictor 'y1' as within-between components
# label variables as 'l1.y1', 'l2.y1', and 'l3.y1'
center(Demo.threelevel, y1, cluster = c("cluster3", "cluster2"), type = "latent",
name = c("l1.y1", "l2.y1", "l3.y1"))
}
Run the code above in your browser using DataLab