if (FALSE) {
# Load data set "Demo.twolevel" in the lavaan package
data("Demo.twolevel", package = "lavaan")
#-------------------------------------------------------------------------------
# Cluster variable specification
# Example 1a: Cluster variable 'cluster' in 'x'
multilevel.cor(Demo.twolevel[, c("y1", "y2", "y3", "cluster")], cluster = "cluster")
# Example 1b: Cluster variable 'cluster' not in 'x'
multilevel.cor(Demo.twolevel[, c("y1", "y2", "y3")], cluster = Demo.twolevel$cluster)
# Example 1c: Alternative specification using the 'data' argument
multilevel.cor(x1:x3, data = Demo.twolevel, cluster = "cluster")
#-------------------------------------------------------------------------------
# Example 2: All variables modeled on both the within and between level
# Highlight statistically significant result at alpha = 0.05
multilevel.cor(Demo.twolevel[, c("y1", "y2", "y3")], sig = TRUE,
cluster = Demo.twolevel$cluster)
# Example 3: Split output table in within-group and between-group correlation matrix.
multilevel.cor(Demo.twolevel[, c("y1", "y2", "y3")],
cluster = Demo.twolevel$cluster, split = TRUE)
# Example 4: Print correlation coefficients, standard errors, z test statistics,
# and p-values
multilevel.cor(Demo.twolevel[, c("y1", "y2", "y3")],
cluster = Demo.twolevel$cluster, print = "all")
# Example 5: Print correlation coefficients and p-values
# significance values with Bonferroni correction
multilevel.cor(Demo.twolevel[, c("y1", "y2", "y3")],
cluster = Demo.twolevel$cluster, print = c("cor", "p"),
p.adj = "bonferroni")
#-------------------------------------------------------------------------------
# Example 6: Variables "y1", "y2", and "y2" modeled on both the within and between level
# Variables "w1" and "w2" modeled on the cluster level
multilevel.cor(Demo.twolevel[, c("y1", "y2", "y3", "w1", "w2")],
cluster = Demo.twolevel$cluster,
between = c("w1", "w2"))
# Example 7: Show variables specified in the argument 'between' first
multilevel.cor(Demo.twolevel[, c("y1", "y2", "y3", "w1", "w2")],
cluster = Demo.twolevel$cluster,
between = c("w1", "w2"), order = TRUE)
#-------------------------------------------------------------------------------
# Example 8: Variables "y1", "y2", and "y2" modeled only on the within level
# Variables "w1" and "w2" modeled on the cluster level
multilevel.cor(Demo.twolevel[, c("y1", "y2", "y3", "w1", "w2")],
cluster = Demo.twolevel$cluster,
within = c("y1", "y2", "y3"), between = c("w1", "w2"))
#-------------------------------------------------------------------------------
# Example 9: lavaan model and summary of the multilevel model used to compute the
# within-group and between-group correlation matrix
mod <- multilevel.cor(Demo.twolevel[, c("y1", "y2", "y3")],
cluster = Demo.twolevel$cluster, output = FALSE)
# lavaan model syntax
mod$model
# Fitted lavaan object
lavaan::summary(mod$model.fit, standardized = TRUE)
#----------------------------------------------------------------------------
# Write Results
# Example 10a: Write results into a text file
multilevel.cor(Demo.twolevel[, c("y1", "y2", "y3")],
cluster = Demo.twolevel$cluster,
write = "Multilevel_Correlation.txt")
# Example 10b: Write results into an Excel file
multilevel.cor(Demo.twolevel[, c("y1", "y2", "y3")],
cluster = Demo.twolevel$cluster,
write = "Multilevel_Correlation.xlsx")
result <- multilevel.cor(Demo.twolevel[, c("y1", "y2", "y3")],
cluster = Demo.twolevel$cluster, output = FALSE)
write.result(result, "Multilevel_Correlation.xlsx")
}
Run the code above in your browser using DataLab