Skip to content

Posts tagged ‘sem’

R packages for Structural Equation Model: SEM with R

Structural Equation Model (SEM) was first examined by a software called LISREL. Then, SEM has been mainly run by several proprietary software i.e., Mplus, AMOS, EQS, SAS and a new version of Stata (v.12).

However, you may also run SEM with a great but free software like R.

To the best of my knowledge, there are now four active packages that you can use to fit SEM. Here they are:

Main Packages (for fitting SEM models) 

  1. sem (John Fox, 2006):The first R package for SEM ” fit by maximum likelihood assuming multinormality, and single-equation estimation for observed-variable models by two-stage least.squares.” It was also the first package I tried to run SEM in R. Thanks to a very quick response from Prof.Fox on my question I emailed him.
    See Example of ‘sem’ package here.
  2. OpenMx (Boker et al, 2011)
    A very active package that “is free and open source software for use with R that allows estimation of a wide variety of advanced multivariate statistical models.” contributed by experts in R and SEM.
    See Example of ‘OpenMx’ package here.
  3. lavaan (Yves Rosseel, 2012)
    A promising package for SEM. Its command language is similar to those of Mplus. Hence it is perhaps the most user-friendly package for SEM to date.
    See Example of ‘lavaan’ package here.
    Link to JSS paper
  4. semPLS (Armin Monecke, 2012)
    Fitting Structural Equation Model Using Partial Least Squares
    See: CRAN link, JSS paper
  5. plspm (Gaston Sanchez, 2012)
    R package dedicated to Partial Least Squares (PLS) methods (CRAN,
    by Gaston Sanchez and Laura Trinchera
    A corresponding book titled “PLS Path Modeling with R” can be downloaded here.
My paper in useR! 2011 has evaluated R packages vs. Proprietary software i.e., AMOS & Lisrel.

Today (30 May 2012), I gladly found that there are also complementary packages for SEM in R as follows.

Complementary packages

  • SEMplusR: Functions, examples and datasets to learn, use and teach Structural Equation Modeling (SEM)  [GitHub]
    by Pairach Piboonrungroj 
  • SEMModComp: Model Comparisons for SEM [CRAN link, Additional Documents]
    by  Roy Levy
  • semGOF: an add-on package which provides fourteen goodness-of-fit indeces for structural equation models using ‘sem’ package.[CRAN]
    by Elena Bertossi 
  • stremo: Functions to help the process of learning structural equation modelling [CRAN link]
    by  Gustavo Carvalho, Marco Batalha, and Owen Petchey
  • FIAR: Functional Integration Analysis in R [CRAN link]
    by  Bjorn Roelstraete
  • semTools: Useful tools for structural equation modeling [CRAN link]
    by  Sunthud Pornprasertmanit, Patrick Miller, Alex Schoemann, Yves Rosseel
  • simsem: SIMulated Structural Equation Modeling [CRAN link]
    by  Sunthud Pornprasertmanit, Patrick Miller, Alexander Schoemann
  • pathmox R package dedicated to segmentation trees in PLS Path Modeling [CRAN,]

Packages for SEM plotting and graphics

  • qgraph: Network representations of relationships in data [CRAN link]
    by  Sacha Epskamp, Angelique O. J. Cramer, Lourens J. Waldorp, Verena D. Schmittmann and Denny Borsboom
  • psych: Procedures for Psychological, Psychometric, and Personality Research [CRAN link]
    by William Revelle

Packages that link R with other software to fit SEM

  • Mplus
    Automating Mplus Model Estimation and Interpretation [CRAN link]
    by  Michael Hallquist
  • EQS
    R/EQS Interface [CRAN link]
    by  Patrick Mair and Eric Wu

More external resources on SEM in R

  • CRAN Task view on ‘Structural Equation Models, Factor Analysis, PCA’ in Psychometrics [url]
    by Patrick Mair
  • A tutorial on the use of sem package  [url]
    by William Revelle
  • A post on ‘Structural Equation Modeling in R‘  [url]
    by Jeromy Anglim

sem package in R: a sample of transaction cost measurement

A sneak peek of the result of a Structural Equation Model using sem package in R.

I will present in the R User Conference in Warwick next week, is reveal first here.

The following are the codes of ‘sem’ package I used in the paper.

You may also view the code of other packages to run SEM models as well.

##    Measuring Transaction Cost in Supply Chains  ##
##               Pairach Piboonrungroj             ##
##         R useR conference August 2011           ##

# 1. Load data
hoteldata <- read.csv("")

#Input covariance matrix <- cor(hoteldata)

# Have a look at the top of the data
# to check it we import the right one

#Create an object contains correlation matrix of the data for fitting the model in the next step
hotel.cor <- cor(hoteldata)
#  path parameter  start-value
model.TC.1 <- specify.model()
	TC   -> TC1,    gamma1,  NA # measurement item
	TC   -> TC2,    gamma2,  NA
 	TC   -> TC3,    gamma3,  NA
 	TC   -> TC6,    gamma6,  NA
 	TC   -> TC7,    gamma7,  NA
 	TC   -> TC11,   gamma11, NA
 	TC   -> TC13,   gamma13, NA
 	TC1  <-> TC1,	e1,      NA # measurement error
 	TC2  <-> TC2,	e2,      NA
 	TC3  <-> TC3,	e3,      NA
 	TC6  <-> TC6,	e6,      NA
 	TC7  <-> TC7,	e7,      NA
 	TC11 <-> TC11,	e11,     NA
 	TC13 <-> TC13,	e13,     NA
 	TC   <-> TC,    NA,      1


sem.TC.1 <- sem(model.TC.1,, 53)
# print result (fit indices, parameters, hypothesis tests)
# standardised coefficients (loadings)

useR Conference 2011 – Timetable

Next month I will have a chance to present how I use R to conduct a structural equation model in my thesis to the expeRts. And now the tentative timetable was just released.
Similar to EurOMA, I will be the last on the first day, just before the dinner.

Here is the link for the timetable.

Mahalanobis Test in AMOS

Analysis Properties -> Output ->

Tick on the box of “Tests for normality and outliers”

Then you will find the Mahalanobis d-squared with p1 and p2 in the “View Text”
by go to -> Observations farthest from the centroid (Mahalanobis distance).

Steps to run SEM

There are several books discussing about how to perform SEM.

Here, I will illustrate those steps from my experience in papers and thesis.

1. Conceptualise the model or set the hypotheses
2. Operationalise the measures in the model
3. Get the data (primary or secondary)
4. Check the data i.e., normality, reliability
5. Fitting measurement model (latent variable model)
6. Fitting the full model (structural model = measurement + path analysis)
7. Access model’s fitness i.e., fit indices,

Optional steps

8. Modify model e.g., modification indices
9. Check common method bias
10. Bootstrapping

More details in each step will follow soon.

1 June 2011
D46, Aberconway, Cardiff, UK

%d bloggers like this: