# Load data set "Demo.twolevel" in the lavaan package
data("Demo.twolevel", package = "lavaan")
# Example 1a: Compute cluster means for 'y1' and expand to match the input 'y1'
cluster.scores(Demo.twolevel$y1, cluster = Demo.twolevel$cluster)
# Example 1b: Alternative specification using the 'data' argument
cluster.scores(y1, data = Demo.twolevel, cluster = "cluster")
# Example 2: Compute standard deviation for each cluster
# and expand to match the input x
cluster.scores(Demo.twolevel$y1, cluster = Demo.twolevel$cluster, fun = "sd")
# Example 3: Compute cluster means without expanding the vector
cluster.scores(Demo.twolevel$y1, cluster = Demo.twolevel$cluster, expand = FALSE)
# Example 4a: Compute cluster means for 'y1' and 'y2' and append to 'Demo.twolevel'
cbind(Demo.twolevel,
cluster.scores(Demo.twolevel[, c("y1", "y2")], cluster = Demo.twolevel$cluster))
# Example 4b: Alternative specification using the 'data' argument
cluster.scores(y1, y2, data = Demo.twolevel, cluster = "cluster")
Run the code above in your browser using DataLab