Here, we will walk through the steps of fitting a structural equation model (SEM) in Q. The post assumes that you already know what a SEM is and how to interpret it.
Case study
In this example, we will analyze Bollen's famous Political Democracy data set (Kenneth Bollen (1989), Structural Equations with Latent Variables, Wiley.)
Requirements
- A dataset you would like to use to create a structural equation model.
- A hypothesis you would like to test in a structural equation model.
Method
Step 1: Load the data
Typically data sets are loaded into Q from raw data files. But, in this case, we will load some data that is stored in an R package.
- File > Data Sets > Add to Project > From R
- Name: BollenPoliticalDemocracy
- Paste in the code below into the R CODE box
- Press the play button (the triangle at the top of the code)
- Press Add Dataset
- Choose the Automatically detect data file structure option and click OK.
library(sem)
Bollen
Step 2: Fit the model
The hard step is fitting the model, as this requires you to specify the measurement model, the relationships to be tested (i.e., the regressions), and the correlation structure of the model. For more information about this, please check out the lavaan website.
To do this:
- Create > R Output
- Paste in the code below
- Press Calculate
# Load the package for computing the SEM
library(lavaan)
# Describe the model
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'
# Assemble the data into a data frame, using the variable names
# from the raw data
df = data.frame(y1, y2, y3, y4, y5, y6, y7, y8, x1, x2, x3)
# Fit the model
fit <- sem(model, data = df)
Step 3: Review the path diagram
In order to check that the model has been correctly specified, it’s a good idea to review the path diagram.
- Create > R Output
- Paste in the code below
- Press Calculate
library(semPlot)
semPaths(fit)
Step 4: Extract the summary statistics
- Create > R Output
- Paste in the code below
- Press Calculate
- Under OUTPUT, tick Show raw R output
summary(fit)
Next
How to Do a Principal Components Analysis in Q