# Load data set "Demo.twolevel" in the lavaan package
data("Demo.twolevel", package = "lavaan")
#----------------------------------------------------------------------------
# Two-Level Models
#..........
# Cluster variable specification
# Example 1a: Cluster variable 'cluster' in '...'
multilevel.icc(Demo.twolevel[, c("y1", "cluster")], cluster = "cluster")
# Example 1b: Cluster variable 'cluster' not in '...'
multilevel.icc(Demo.twolevel$y1, cluster = Demo.twolevel$cluster)
# Example 1c: Alternative specification using the 'data' argument
multilevel.icc(y1, data = Demo.twolevel, cluster = "cluster")
#..........
# Example 2: ICC(1) for 'y1'
multilevel.icc(Demo.twolevel$y1, cluster = Demo.twolevel$cluster)
# Example 3: ICC(2)
multilevel.icc(Demo.twolevel$y1, cluster = Demo.twolevel$cluster, type = 2)
# Example 4: ICC(1)
# use lme() function in the lme4 package to estimate ICC
multilevel.icc(Demo.twolevel$y1, cluster = Demo.twolevel$cluster, method = "nlme")
# Example 5a: ICC(1) for 'y1', 'y2', and 'y3'
multilevel.icc(Demo.twolevel[, c("y1", "y2", "y3")], cluster = Demo.twolevel$cluster)
# Example 5b: Alternative specification using the 'data' argument
multilevel.icc(y1:y3, data = Demo.twolevel, cluster = "cluster")
#----------------------------------------------------------------------------
# Three-Level Models
# Create arbitrary three-level data
Demo.threelevel <- data.frame(Demo.twolevel, cluster2 = Demo.twolevel$cluster,
cluster3 = rep(1:10, each = 250))
#..........
# Cluster variable specification
# Example 6a: Cluster variables 'cluster' in '...'
multilevel.icc(Demo.threelevel[, c("y1", "cluster3", "cluster2")],
cluster = c("cluster3", "cluster2"))
# Example 6b: Cluster variables 'cluster' not in '...'
multilevel.icc(Demo.threelevel$y1, cluster = Demo.threelevel[, c("cluster3", "cluster2")])
# Example 6c: Alternative specification using the 'data' argument
multilevel.icc(y1, data = Demo.threelevel, cluster = c("cluster3", "cluster2"))
#..........
# Example 7a: ICC(1), proportion of variance at Level 2 and Level 3
multilevel.icc(y1, data = Demo.threelevel, cluster = c("cluster3", "cluster2"))
# Example 7b: ICC(1), expected correlation between two randomly chosen elements
# in the same group
multilevel.icc(y1, data = Demo.threelevel, cluster = c("cluster3", "cluster2"),
type = "1b")
# Example 7c: ICC(2)
multilevel.icc(y1, data = Demo.threelevel, cluster = c("cluster3", "cluster2"),
type = "2")
Run the code above in your browser using DataLab