Skip to content

Instantly share code, notes, and snippets.

Created May 20, 2015 18:16
What would you like to do?
A first look at structured equation models using the Lavaan package
## By Grace Charles, presented at Davis R Users' Group on May 15, 2015
## adapted from Jim Grace's SEM workshop and Lavaan tutorials
# Set your working directory
setwd("~/Desktop/DAVIS/sem workshop")
###Load Libraries
#Built in dataset
## here is an example model from one of Lavaan's build-in datasets
## the measurement model equations are "latent" and represented by =~
## regressions are indicated by ~
## residual correlations (in this case because they represent different years of the same measurement) are represented by ~~
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
y2~~ y6
y3 ~~ y7
y4 ~~ y8
y6 ~~ y8
#fit your SEM
fit <- sem(model, data = PD)
#summarize results
summary(fit, standardized = TRUE, rsq = T)
##plot results using semPaths function in qgraph
semPaths(fit, "std", edge.label.cex = 0.5, curvePivot = TRUE, layout = "tree")
##check to see if you missed anything. High mi values suggest that there is a path that you missed.
## looks good
##can also look at variance tables
## sometimes you get warnings about the scale of your variables
#Warning message:
# In getDataFull(data = data, group = group, group.label = group.label, :
# lavaan WARNING: some observed variances are (at least) a factor 100 times larger than others; please rescale
# in this case, all you have to do to make this error go away is rescale variables
#model comparison
#you can compare alternative pathway models using AIC, BIC, etc:
#create second alternative model
model2 <- '
# measurement model
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3 + y4
dem65 =~ y5 + y6 + y7 + y8
# regressions
dem60 ~ ind60
dem65 ~ dem60
#took out ind60 from regression
# residual correlations
y1 ~~ y5
y2 ~~ y4 + y6
y3 ~~ y7
y4 ~~ y8
y6 ~~ y8
fit2 <- sem(model2, data = PD)
AIC(fit, fit2)
## what about nonlinear data?
# Set your working directory
("~/Desktop/sem workshop")
#commands in bold
# Load data and name file ?k.dat?
# Examine contents of keeley data file
# Write lavaan code for this single equation model
mod <- '
rich ~ cover
cover ~ firesev
mod2<- '
rich ~ cover + cov2
cover ~ firesev
cover ~~ cov2
cov2 ~~ firesev
# Fit the model (i.e. est. parameters) <- sem(mod, data=k.dat)<- sem(mod2, data=k.dat,fixed.x=FALSE)
#need to rescale data.
# Output a summary of the computed results - summary of mod2 suggests that both cover and cover squared can impact
summary(, rsq=T) # rsq=T means output the r-sqr
summary(, rsq=T)
semPaths(, "std", edge.label.cex = 0.5, curvePivot = TRUE, layout = "tree")
semPaths(, "std", edge.label.cex = 0.5, curvePivot = TRUE, layout = "tree")
Copy link

It happens to me too when I try to load the semPlot package. Is the problem solved?

Copy link

Thanks, but where is the "Keeley_rawdata_select4.csv" file. Could we use this file too please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment