intro to lavaan

Njål Foldnes
6. May, 2014

Sem software

Commercial packages

  • Lisrel
  • EQS
  • Mplus
  • Amos

Non-commercial R packages

R also has packages to interface with commercial packages:

  • REQS, MplusAutomation

lavaan

  • confirmatory factor analysis: function cfa()
  • sem: function sem()
  • growth modeling: function growth()

More information about lavaan at http://lavaan.org

Rosseel (2012). lavaan: an R package for structural equation modeling. Journal of Statistical Software, 48(2), 1–36.

lavaan

Future releases:

-Bayesian estimation, multilevel SEM (gllaamm)

  • support for discrete latent variables (latent class analysis)
  • C++ engine for core computations

Formula types

Formula type Operator Mnemonic
latent variable definition =~ is measured by
regression ~ is regressed on
(residual) covariance ~~ is correlated with
intercept ~ 1

Holzinger & Swineford data

library(lavaan)# comes with dataset Holzinger & Swineford
head(HolzingerSwineford1939,2)
  id sex ageyr agemo  school grade    x1   x2    x3    x4   x5    x6    x7
1  1   1    13     1 Pasteur     7 3.333 7.75 0.375 2.333 5.75 1.286 3.391
2  2   2    13     7 Pasteur     7 5.333 5.25 2.125 1.667 3.00 1.286 3.783
    x8    x9
1 5.75 6.361
2 6.25 7.917

Confirmatory factor analysis with cfa()

alt text

First define the model:

HS.model <- " visual  =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6
              speed   =~ x7 + x8 + x9
"

Fit model and inspect results

fit <- cfa(model = HS.model, data  = HolzingerSwineford1939, estimator = "MLM")

summary(fit)
#look at parameter estimates
parameterestimates(fit)
#fit measures
fitmeasures(fit)

alt text

model <- "
   # latent variables
     ind60 =~ x1 + x2 + x3
     dem60 =~ y1 + y2 + y3 + y4
     dem65 =~ y5 + y6 + y7 + y8
   # regressions
     dem60 ~ ind60
     dem65 ~ ind60 + dem60
   # residual covariances
     y1 ~~ y5
     y2 ~~ y4 + y6
     y3 ~~ y7
     y4 ~~ y8
     y6 ~~ y8"

Fit model and inspect results

fit <- sem(model, data = PoliticalDemocracy)
summary(fit)

Equality constraints on factor loadings:

model.equal <- "
# measurement model
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + d1*y2 + d2*y3 + d3*y4
dem65 =~ y5 + d1*y6 + d2*y7 + d3*y8
# regressions
dem60 ~ ind60
dem65 ~ ind60 + dem60
#etc
"

Arguments to cfa() and sem() functions

sem(model = NULL, meanstructure = “default”, fixed.x = “default”, orthogonal = FALSE, std.lv = FALSE, data = NULL, std.ov = FALSE, missing = “default”, sample.cov = NULL, sample.mean = NULL, sample.nobs = NULL, group = NULL, group.equal = “”, group.partial = “”, constraints = ’’, estimator = “default”, likelihood = “default”, information = “default”, se = “default”, test = “default”, bootstrap = 1000L, mimic = “default”, representation = “default”, do.fit = TRUE, control = list(), start = “default”, verbose = FALSE, warn = TRUE, debug = FALSE)

Start values, fixed values and constraints

HS.model <- " visual  =~ x1 + start(0.5)*x2 + 0.7*x3
              textual =~ x4 + x5 + x6
              speed   =~ x7 + x8 + x9
              #unique variance constraints
              x1 ~~ a*x1
              x2 ~~ a*x2"

Here we fix third loading to 0.7 and use labeling to impose equality of unique variances for x1 and x2

Multiple Groups

fit <- cfa(HS.model, 
           data = HolzingerSwineford1939, 
           group = "school")
summary(fit)

HS.model <- " visual  =~ x1 + c(a,a)*x2 + x3
               textual =~ x4 + x5 + x6
               speed   =~ x7 + x8 + x9"

All factor loadings equal across groups:

fit <- cfa(HS.model, data = HolzingerSwineford1939, group = "school", group.equal=c("loadings"))

Measurement Invariance

library(semTools)
measurementInvariance(HS.model, data = HolzingerSwineford1939, group = "school")

Inspect fit object

fit <- cfa(HS.model, data = HolzingerSwineford1939, group = "school", group.equal=c("loadings"))
inspect(fit, "converged")
[1] TRUE
inspect(fit, "start")
$lambda
   visual textul speed
x1    1.0      0     0
x2    0.5      0     0
x3    0.7      0     0
x4    0.0      1     0
x5    0.0      1     0
x6    0.0      1     0
x7    0.0      0     1
x8    0.0      0     1
x9    0.0      0     1

$theta
   x1    x2    x3    x4    x5    x6    x7    x8    x9   
x1 0.698                                                
x2 0.000 0.752                                          
x3 0.000 0.000 0.673                                    
x4 0.000 0.000 0.000 0.660                              
x5 0.000 0.000 0.000 0.000 0.854                        
x6 0.000 0.000 0.000 0.000 0.000 0.487                  
x7 0.000 0.000 0.000 0.000 0.000 0.000 0.585            
x8 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.476      
x9 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.489

$psi
        visual textul speed
visual  0.05               
textual 0.00   0.05        
speed   0.00   0.00   0.05 

$nu
   intrcp
x1  4.941
x2  5.984
x3  2.487
x4  2.823
x5  3.995
x6  1.922
x7  4.432
x8  5.563
x9  5.418

$alpha
        intrcp
visual       0
textual      0
speed        0

$lambda
   visual textul speed
x1    1.0      0     0
x2    0.5      0     0
x3    0.7      0     0
x4    0.0      1     0
x5    0.0      1     0
x6    0.0      1     0
x7    0.0      0     1
x8    0.0      0     1
x9    0.0      0     1

$theta
   x1    x2    x3    x4    x5    x6    x7    x8    x9   
x1 0.659                                                
x2 0.000 0.613                                          
x3 0.000 0.000 0.537                                    
x4 0.000 0.000 0.000 0.629                              
x5 0.000 0.000 0.000 0.000 0.671                        
x6 0.000 0.000 0.000 0.000 0.000 0.640                  
x7 0.000 0.000 0.000 0.000 0.000 0.000 0.531            
x8 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.547      
x9 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.526

$psi
        visual textul speed
visual  0.05               
textual 0.00   0.05        
speed   0.00   0.00   0.05 

$nu
   intrcp
x1  4.930
x2  6.200
x3  1.996
x4  3.317
x5  4.712
x6  2.469
x7  3.921
x8  5.488
x9  5.327

$alpha
        intrcp
visual       0
textual      0
speed        0

lavaan ecosystem

  • lavaan.survey (Daniel Oberski) for clustering, strata
  • Onyx for graphical interface
  • semTools package
  • simsem for simulation
  • semPlot