This function generates the lavaan syntax for a measurement model. The factor names and items are taken from a named list. This can be used by itself (e.g. for a CFA) or in conjunction with a structural model (e.g. with a mediation model).
Examples
library(lavaan)
#> This is lavaan 0.6-12
#> lavaan is FREE software! Please report any bugs.
factors <- list(x="x"%p%1:3, y="y"%p%1:3)
factors |>
get_measurement_model() |>
sem(data=df) |>
summary()
#> Warning: lavaan WARNING: covariance matrix of latent variables
#> is not positive definite;
#> use lavInspect(fit, "cov.lv") to investigate.
#> lavaan 0.6-12 ended normally after 32 iterations
#>
#> Estimator ML
#> Optimization method NLMINB
#> Number of model parameters 13
#>
#> Number of observations 500
#>
#> Model Test User Model:
#>
#> Test statistic 262.382
#> Degrees of freedom 8
#> P-value (Chi-square) 0.000
#>
#> Parameter Estimates:
#>
#> Standard errors Standard
#> Information Expected
#> Information saturated (h1) model Structured
#>
#> Latent Variables:
#> Estimate Std.Err z-value P(>|z|)
#> x =~
#> x1 1.000
#> x2 1.787 0.092 19.459 0.000
#> x3 2.310 0.118 19.642 0.000
#> y =~
#> y1 1.000
#> y2 1.833 0.117 15.695 0.000
#> y3 2.499 0.154 16.230 0.000
#>
#> Covariances:
#> Estimate Std.Err z-value P(>|z|)
#> x ~~
#> y 0.155 0.015 10.427 0.000
#>
#> Variances:
#> Estimate Std.Err z-value P(>|z|)
#> .x1 0.162 0.010 15.496 0.000
#> .x2 0.129 0.010 13.457 0.000
#> .x3 0.195 0.015 13.092 0.000
#> .y1 0.230 0.015 15.763 0.000
#> .y2 0.173 0.012 14.318 0.000
#> .y3 0.207 0.016 12.763 0.000
#> x 0.156 0.017 8.939 0.000
#> y 0.138 0.018 7.577 0.000
#>