Skip to content

lavaan package for Structural Equation Model in R


lavaan is a package for running a “Structural Equation Models” or SEM in R. The script of lavaan is similar to Mplus, which is very comprehensive and compact. The lavaan package is developed by Yves Rosseel.

The following is my lavaan script I used in my presentation at R useR! 2011.

##-------------------------------------------------##
##    Measuring Transaction Cost in Supply Chains  ##
##               Pairach Piboonrungroj             ##
##         R useR conference August 2011           ##
##-------------------------------------------------##
# 1. Load data
hoteldata <- read.csv("https://www.dropbox.com/s/fzh040xkhofozjy/cleandata.csv")
# 2. Install Package
install.packages("lavaan")
# 3. Load Package
library(lavaan)
#5. Structural Model
TC.Model <- '
# latent variable definitions
cost =~ TC1 + TC2 + TC3 +TC6 + TC7   +TC11  + TC13
asset =~ AS1 + AS2
uncertainty  =~ UN1 + UN2
# regression
cost ~ uncertainty + asset
# resident (co)variance
TC1 ~~ TC3
TC6 ~~ TC13
TC11 ~~ TC13
TC13 ~~ AS1
'
fitTC <- sem(TC.Model, data = hoteldata)
summary(fitTC, standardized = TRUE, fit.measures=TRUE)
# Coefficients
# coef(fitTC)
#More indices
# Fit indices e.g. CFI
fit.indices <- inspect(fitTC, what = "fit")
fit.indices["cfi"]
# Calling Modification Indices
# inspect(fitTC, what = "mi")

See more details about the model and the comparison with other R packages and software

8 Comments Post a comment
  1. vincentfeltkamp #

    ‘t would be good to have some data to try this script out, can you post either the csv or the structure of the csv file?

    April 19, 2012
    • Hi, thanks a lot for your interest.

      I have put a public of the data file in the script already. Then one can run the script straight away. Please let me know any bug or problem you find.

      April 19, 2012
  2. dong #

    Hi I tried to use that script comparing with the amos results. The regression and correlation are almost similar however the model fit index especially the chi p-value cannot provide result if the df is 1. The amos recognize the df as 1 while this r-code gives 0 df. Any way to improve the model fit script ?

    June 19, 2012
    • Degrees of freedom I’ve got Degrees of freedom of 37 using the R code above.
      Have you included the whole code?

      June 19, 2012
      • dong #

        Thanks for the reply. Yes sir i included the codes above using my own data. By comparing the results using my data, the Amos gives DF =1 while this R-script gives DF=0. With that, the chi square p-value don’t provide results for model fit. Another thing is that the CFI, TLI values tends to round-off. Meanwhile the RMSEA values seems to have problem.

        Additionally, can we use this codes to compute sem model without latent variable just purely the exogenous and endogeonus something like causal modelling?

        June 20, 2012

Trackbacks & Pingbacks

  1. My presentation at The R useR! Conference 2011 | Pairach Piboonrungroj
  2. R packages for Structural Equation Model | Pairach Piboonrungroj
  3. sem package in R: a sample of transaction cost measurement | Pairach Piboonrungroj

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 2,799 other followers

%d bloggers like this: