Demo.twolevel.RdA toy dataset containing measures on 6 items (y1-y6), 3 within-level covariates (x1-x3) and 2 between-level covariates (w1-w2). The data is clustered (200 clusters of size 5, 10, 15 and 20), and the cluster variable is “cluster”.
data(Demo.twolevel)A data frame of 2500 observations of 12 variables. clusters.
y1item 1
y2item 2
y3item 3
y4item 4
y5item 5
y6item 6
x1within-level covariate 1
x2within-level covariate 2
x3within-level covariate 3
w1between-level covariate 1
w2between-level covariate 2
clustercluster variable
head(Demo.twolevel)
#> y1 y2 y3 y4 y5 y6 x1
#> 1 0.2293216 1.3555232 -0.6911702 0.8028079 -0.3011085 -1.7260671 1.1739003
#> 2 0.3085801 -1.8624397 -2.4179783 0.7659289 1.6750617 1.1764210 -1.0039958
#> 3 0.2004934 -1.3400514 0.4376087 1.1974194 1.1951594 1.4988962 -0.4402545
#> 4 1.0447982 -0.9624490 -0.4464898 -0.2027252 -0.4590574 1.1734061 -0.6253657
#> 5 0.6881792 -0.4565633 -0.6422296 0.9900408 1.7662535 0.7944601 -0.8450025
#> 6 -2.0687644 -0.5997856 0.3148418 0.6764432 -0.6519928 1.8405605 -0.7831784
#> x2 x3 w1 w2 cluster
#> 1 -0.62315173 0.6470414 -0.2479975 -0.4989800 1
#> 2 -0.56689380 0.0201264 -0.2479975 -0.4989800 1
#> 3 -2.13432572 -0.4591246 -0.2479975 -0.4989800 1
#> 4 -0.33688869 1.2852093 -0.2479975 -0.4989800 1
#> 5 -0.04229954 1.5598970 -0.2479975 -0.4989800 1
#> 6 -0.22441996 -0.3814231 -2.3219338 -0.6910567 2
model <- '
level: 1
fw =~ y1 + y2 + y3
fw ~ x1 + x2 + x3
level: 2
fb =~ y1 + y2 + y3
fb ~ w1 + w2
'
fit <- sem(model, data = Demo.twolevel, cluster = "cluster")
summary(fit)
#> lavaan 0.6-20 ended normally after 36 iterations
#>
#> Estimator ML
#> Optimization method NLMINB
#> Number of model parameters 20
#>
#> Number of observations 2500
#> Number of clusters [cluster] 200
#>
#> Model Test User Model:
#>
#> Test statistic 8.092
#> Degrees of freedom 10
#> P-value (Chi-square) 0.620
#>
#> Parameter Estimates:
#>
#> Standard errors Standard
#> Information Observed
#> Observed information based on Hessian
#>
#>
#> Level 1 [within]:
#>
#> Latent Variables:
#> Estimate Std.Err z-value P(>|z|)
#> fw =~
#> y1 1.000
#> y2 0.774 0.032 24.349 0.000
#> y3 0.734 0.030 24.052 0.000
#>
#> Regressions:
#> Estimate Std.Err z-value P(>|z|)
#> fw ~
#> x1 0.510 0.023 22.387 0.000
#> x2 0.407 0.022 18.489 0.000
#> x3 0.205 0.021 9.776 0.000
#>
#> Variances:
#> Estimate Std.Err z-value P(>|z|)
#> .y1 0.986 0.043 22.802 0.000
#> .y2 1.066 0.039 27.616 0.000
#> .y3 1.011 0.035 29.287 0.000
#> .fw 0.546 0.032 16.848 0.000
#>
#>
#> Level 2 [cluster]:
#>
#> Latent Variables:
#> Estimate Std.Err z-value P(>|z|)
#> fb =~
#> y1 1.000
#> y2 0.717 0.047 15.182 0.000
#> y3 0.587 0.043 13.503 0.000
#>
#> Regressions:
#> Estimate Std.Err z-value P(>|z|)
#> fb ~
#> w1 0.165 0.078 2.099 0.036
#> w2 0.131 0.076 1.721 0.085
#>
#> Intercepts:
#> Estimate Std.Err z-value P(>|z|)
#> .y1 0.024 0.075 0.327 0.744
#> .y2 -0.016 0.060 -0.267 0.789
#> .y3 -0.042 0.054 -0.775 0.439
#>
#> Variances:
#> Estimate Std.Err z-value P(>|z|)
#> .y1 0.058 0.043 1.346 0.178
#> .y2 0.120 0.031 3.922 0.000
#> .y3 0.149 0.028 5.376 0.000
#> .fb 0.899 0.110 8.194 0.000
#>