Skip to contents

This vignette attempts to reproduce several different possible lavaan models. We start with the source itself: The lavaan project at https://lavaan.ugent.be. Let’s start by loading both packages.

Example 1 (Model syntax 1):

Source: https://lavaan.ugent.be/tutorial/syntax1.html

lavaan:

myModel <- ' # regressions
             y1 + y2 ~ f1 + f2 + x1 + x2
                  f1 ~ f2 + f3
                  f2 ~ f3 + x1 + x2

             # latent variable definitions 
               f1 =~ y1 + y2 + y3 
               f2 =~ y4 + y5 + y6 
               f3 =~ y7 + y8 + y9 + y10

             # variances and covariances 
               y1 ~~ y1 
               y1 ~~ y2 
               f1 ~~ f2

             # intercepts 
               y1 ~ 1 
               f1 ~ 1
           '
cat(myModel)

lavaanExtra:

#y <- paste0(rep(c("f", "x"), each = 2), 1:2)
regression <- list(y1 = c("f1", "f2", "x1", "x2"),
                   y2 = c("f1", "f2", "x1", "x2"),
                   f1 = c("f2", "f3"),
                   f2 = c("f3", "x1", "x2"))
latent <- list(f1 = paste0("y", 1:3),
               f2 = paste0("y", 4:6), 
               f3 = paste0("y", 7:10))
covariance <- list(y1 = "y1",
                   y1 = "y2",
                   f1 = "f2")
intercept <- c("y1", "f1")
myModel <- write_lavaan(regression = regression,
                        latent = latent,
                        covariance = covariance,
                        intercept = intercept)
cat(myModel)

Example 2 (A CFA example):

Source: https://lavaan.ugent.be/tutorial/cfa.html

lavaan:

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

lavaanExtra:

#y <- paste0(rep(c("f", "x"), each = 2), 1:2)
latent <- list(visual = paste0("x", 1:3),
               textual = paste0("x", 4:6),
               speed = paste0("x", 7:9))
myModel <- write_lavaan(latent = latent)
cat(myModel)

Example 3 (A SEM example):

Source: https://lavaan.ugent.be/tutorial/sem.html

lavaan:

model <- '
  # measurement model
    ind60 =~ x1 + x2 + x3
    dem60 =~ y1 + y2 + y3 + y4
    dem65 =~ y5 + y6 + y7 + y8
  # regressions
    dem60 ~ ind60
    dem65 ~ ind60 + dem60
  # residual correlations
    y1 ~~ y5
    y2 ~~ y4 + y6
    y3 ~~ y7
    y4 ~~ y8
    y6 ~~ y8
'
cat(model)

lavaanExtra:

#y <- paste0(rep(c("f", "x"), each = 2), 1:2)
latent <- list(ind60 = paste0("x", 1:3),
               dem60 = paste0("y", 1:4),
               dem65 = paste0("y", 5:8))
regression <- list(dem60 = "ind60",
                   dem65 = c("ind60", "dem60"))
covariance <- list(y1 = "y5",
                   y2 = c("y4", "y6"),
                   y3 = "y7",
                   y4 = "y8",
                   y6 = "y8")
model <- write_lavaan(latent = latent, regression = regression, covariance = covariance)
cat(model)

Example 4 (Model syntax 2):

Source: https://lavaan.ugent.be/tutorial/syntax2.html

lavaan:

model <- '
  # measurement model
    ind60 =~ x1 + x2 + x3
    dem60 =~ y1 + y2 + y3 + y4
    dem65 =~ y5 + y6 + y7 + y8
  # regressions
    dem60 ~ ind60
    dem65 ~ ind60 + dem60
  # residual correlations
    y1 ~~ y5
    y2 ~~ y4 + y6
    y3 ~~ y7
    y4 ~~ y8
    y6 ~~ y8
'
cat(model)

lavaanExtra:

#y <- paste0(rep(c("f", "x"), each = 2), 1:2)
latent <- list(ind60 = paste0("x", 1:3),
               dem60 = paste0("y", 1:4),
               dem65 = paste0("y", 5:8))
regression <- list(dem60 = "ind60",
                   dem65 = c("ind60", "dem60"))
covariance <- list(y1 = "y5",
                   y2 = c("y4", "y6"),
                   y3 = "y7",
                   y4 = "y8",
                   y6 = "y8")
model <- write_lavaan(latent = latent, regression = regression, covariance = covariance)
cat(model)