Chong Xing, Center for Research Methods and Data Analysis, University of Kansas <cxing@ku.edu>
Paul Johnson, Center for Research Methods and Data Analysis, University of Kansas <pauljohn@ku.edu>
Please visit http://crmda.ku.edu/guides
Keywords: Structural Equation Modeling, R, lavaan
2019 January 18
Abstract
This guide outlines how to fit a structural equation model with measurement and structural components. The predictor and outcome variables in this example are latent factors.Load the lavaan package at the beginning of the session
library(lavaan)
This is lavaan 0.6-3
lavaan is BETA software! Please report any bugs.
The data file is read in, columns are named, and missing values are specified.
dat <- read.csv("../../data/insomnia.dat", header = FALSE, sep = "\t")
colnames(dat) <- c("insom1", "insom2", "insom3",
"insom4", "insom5", "insom6",
"insom7",
"cesd1", "cesd2", "cesd3",
"cesd4", "cesd5", "cesd6",
"cesd7", "cesd8", "cesd9",
"cesd10", "cesd11", "cesd12",
"cesd13", "cesd14", "cesd15",
"cesd16", "cesd17", "cesd18",
"cesd19", "cesd20",
"phy", "psy", "soc", "env")
dat[dat == 999] <- NA
This builds the SEM model
SEMModel <- ' ## the measurement model for exogenous factor Impact
Impact =~ insom5 + insom6 + insom7
## the measurement model for exogenous factor Severity
Severity =~ insom1 + insom2 + insom3
## the measurement model for exogenous factor Satisf
Satisf =~ insom1 + insom4 + insom7
## the measurement model for endogenous factor Qol
Qol =~ phy + psy + soc + env
## the structural model for regressing the
## endogenous onto the exogenous factors
Qol ~ Impact + Severity + Satisf '
Here the model is fitted and the summary is requested.
output <- sem(model = SEMModel, data = dat, std.lv = TRUE,
missing = "fiml", mimic = "Mplus")
summary(output, standardized = TRUE, fit.measures = TRUE)
lavaan 0.6-3 ended normally after 55 iterations
Optimization method NLMINB
Number of free parameters 41
Number of observations 103
Number of missing patterns 3
Estimator ML
Model Fit Test Statistic 43.481
Degrees of freedom 36
P-value (Chi-square) 0.183
Model test baseline model:
Minimum Function Test Statistic 602.272
Degrees of freedom 55
P-value 0.000
User model versus baseline model:
Comparative Fit Index (CFI) 0.986
Tucker-Lewis Index (TLI) 0.979
Loglikelihood and Information Criteria:
Loglikelihood user model (H0) -1044.951
Loglikelihood unrestricted model (H1) -1023.210
Number of free parameters 41
Akaike (AIC) 2171.901
Bayesian (BIC) 2279.925
Sample-size adjusted Bayesian (BIC) 2150.413
Root Mean Square Error of Approximation:
RMSEA 0.045
90 Percent Confidence Interval 0.000 0.087
P-value RMSEA <= 0.05 0.539
Standardized Root Mean Square Residual:
SRMR 0.045
Parameter Estimates:
Information Observed
Observed information based on Hessian
Standard Errors Standard
Latent Variables:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
Impact =~
insom5 1.053 0.108 9.715 0.000 1.053 0.930
insom6 0.684 0.099 6.912 0.000 0.684 0.672
insom7 0.334 0.119 2.798 0.005 0.334 0.321
Severity =~
insom1 0.121 0.109 1.112 0.266 0.121 0.146
insom2 0.841 0.088 9.501 0.000 0.841 0.916
insom3 0.671 0.090 7.471 0.000 0.671 0.726
Satisf =~
insom1 0.539 0.112 4.833 0.000 0.539 0.650
insom4 0.714 0.080 8.963 0.000 0.714 0.810
insom7 0.572 0.125 4.581 0.000 0.572 0.549
Qol =~
phy 0.353 0.037 9.608 0.000 0.439 0.860
psy 0.415 0.046 9.021 0.000 0.516 0.849
soc 0.283 0.044 6.460 0.000 0.353 0.641
env 0.332 0.039 8.493 0.000 0.413 0.807
Regressions:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
Qol ~
Impact -0.122 0.193 -0.632 0.527 -0.098 -0.098
Severity -0.065 0.202 -0.321 0.748 -0.052 -0.052
Satisf -0.612 0.301 -2.034 0.042 -0.491 -0.491
Covariances:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
Impact ~~
Severity 0.356 0.108 3.301 0.001 0.356 0.356
Satisf 0.655 0.095 6.869 0.000 0.655 0.655
Severity ~~
Satisf 0.672 0.092 7.306 0.000 0.672 0.672
Intercepts:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
.insom5 1.621 0.112 14.523 0.000 1.621 1.431
.insom6 0.951 0.100 9.485 0.000 0.951 0.935
.insom7 1.184 0.103 11.554 0.000 1.184 1.138
.insom1 0.718 0.082 8.792 0.000 0.718 0.866
.insom2 0.670 0.090 7.407 0.000 0.670 0.730
.insom3 0.738 0.091 8.105 0.000 0.738 0.799
.insom4 1.738 0.087 20.023 0.000 1.738 1.973
.phy 3.659 0.050 72.783 0.000 3.659 7.172
.psy 3.413 0.060 56.813 0.000 3.413 5.609
.soc 3.523 0.054 64.839 0.000 3.523 6.409
.env 3.520 0.050 69.751 0.000 3.520 6.873
Impact 0.000 0.000 0.000
Severity 0.000 0.000 0.000
Satisf 0.000 0.000 0.000
.Qol 0.000 0.000 0.000
Variances:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
.insom5 0.175 0.146 1.195 0.232 0.175 0.136
.insom6 0.568 0.100 5.676 0.000 0.568 0.548
.insom7 0.394 0.071 5.591 0.000 0.394 0.364
.insom1 0.295 0.054 5.485 0.000 0.295 0.429
.insom2 0.136 0.095 1.428 0.153 0.136 0.161
.insom3 0.403 0.082 4.924 0.000 0.403 0.472
.insom4 0.267 0.063 4.228 0.000 0.267 0.344
.phy 0.068 0.015 4.443 0.000 0.068 0.260
.psy 0.104 0.021 4.830 0.000 0.104 0.280
.soc 0.178 0.028 6.469 0.000 0.178 0.589
.env 0.091 0.017 5.370 0.000 0.091 0.348
Impact 1.000 1.000 1.000
Severity 1.000 1.000 1.000
Satisf 1.000 1.000 1.000
.Qol 1.000 0.645 0.645
R version 3.5.1 (2018-07-02)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)
Matrix products: default
BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lavaan_0.6-3 stationery_0.98.5.7
loaded via a namespace (and not attached):
[1] Rcpp_1.0.0 digest_0.6.18 MASS_7.3-51.1 plyr_1.8.4
[5] xtable_1.8-3 magrittr_1.5 stats4_3.5.1 evaluate_0.12
[9] zip_1.0.0 stringi_1.2.4 pbivnorm_0.6.0 openxlsx_4.1.0
[13] rmarkdown_1.11 tools_3.5.1 stringr_1.3.1 foreign_0.8-71
[17] kutils_1.59 yaml_2.2.0 xfun_0.4 compiler_3.5.1
[21] mnormt_1.5-5 htmltools_0.3.6 knitr_1.21
Available under Created Commons license 3.0