SEM Example in R

Load the lavaan package at the beginning of the session

library(lavaan)
## Warning: package 'lavaan' was built under R version 3.1.3
## This is lavaan 0.5-18
## 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/job_placement.csv", header = FALSE)
colnames(dat) <- c("id", "wjcalc", "wjspl", "wratspl", "wratcalc", "waiscalc", "waisspl", "edlevel", "newschl", "suspend", "expelled", "haveld", "female", "age")
dat[dat == 99999] <- NA

If we want to have the results match what is provided by Mplus, we have to remove cases with missing values on predictors. The Mplus default is to remove these cases, whereas R will attempt to take the missing values on the predictors into account.

missings <- rowSums(dat[,c("edlevel", "newschl", "suspend", "expelled", "haveld", "female", "age")])
dat <- dat[-which(is.na(missings)),]

This builds the SEM model

SEMModel <- "MATH =~ wratcalc + wjcalc + waiscalc
             SPELL =~ wratspl + wjspl + waisspl
             MATH ~ edlevel + newschl + suspend + expelled + haveld + female + age
             SPELL ~ edlevel + newschl + suspend + expelled + haveld + female + age
             MATH ~~ SPELL
             "

Here the model is fitted and the summary is requested.

output <- sem(model = SEMModel, data = dat, missing = "fiml")
summary(output)
## lavaan (0.5-18) converged normally after 198 iterations
## 
##   Number of observations                           313
## 
##   Number of missing patterns                         4
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic               49.780
##   Degrees of freedom                                36
##   P-value (Chi-square)                           0.063
## 
## Parameter estimates:
## 
##   Information                                 Observed
##   Standard Errors                             Standard
## 
##                    Estimate  Std.err  Z-value  P(>|z|)
## Latent variables:
##   MATH =~
##     wratcalc          1.000
##     wjcalc            0.684    0.027   25.116    0.000
##     waiscalc          0.403    0.024   16.552    0.000
##   SPELL =~
##     wratspl           1.000
##     wjspl             1.038    0.029   35.933    0.000
##     waisspl           0.967    0.028   34.549    0.000
## 
## Regressions:
##   MATH ~
##     edlevel           1.720    0.282    6.097    0.000
##     newschl           0.535    0.649    0.824    0.410
##     suspend          -1.727    0.677   -2.550    0.011
##     expelled         -0.524    0.954   -0.549    0.583
##     haveld           -1.495    0.858   -1.743    0.081
##     female           -0.757    0.689   -1.098    0.272
##     age               0.617    0.179    3.441    0.001
##   SPELL ~
##     edlevel           1.169    0.302    3.874    0.000
##     newschl          -0.212    0.694   -0.305    0.760
##     suspend          -0.111    0.718   -0.155    0.877
##     expelled         -2.540    1.021   -2.488    0.013
##     haveld           -6.631    0.923   -7.182    0.000
##     female            0.843    0.736    1.145    0.252
##     age               0.349    0.192    1.819    0.069
## 
## Covariances:
##   MATH ~~
##     SPELL            15.401    2.048    7.520    0.000
## 
## Intercepts:
##     wratcalc          8.718    3.847    2.266    0.023
##     wjcalc            3.169    2.710    1.169    0.242
##     waiscalc         -1.141    1.686   -0.677    0.498
##     wratspl          17.949    4.113    4.364    0.000
##     wjspl            22.456    4.267    5.263    0.000
##     waisspl          19.289    3.978    4.849    0.000
##     MATH              0.000
##     SPELL             0.000
## 
## Variances:
##     wratcalc          4.035    0.966
##     wjcalc            3.876    0.528
##     waiscalc          5.221    0.457
##     wratspl           4.985    0.612
##     wjspl             4.751    0.626
##     waisspl           4.960    0.591
##     MATH             27.561    2.600
##     SPELL            32.786    2.961