In this article, we will walk through the steps of fitting a structural equation model (SEM) in Displayr. The post assumes you already know what an SEM is and how to interpret it.
Case study
I am going to analyze Bollen's famous Political Democracy data set (Kenneth Bollen (1989), Structural Equations with Latent Variables, Wiley.)
Requirements
A Displayr Document with data suitable for structural equation modeling.
Please note these steps require a Displayr license.
Method
Step 1: Load the data
Typically data sets are loaded into Displayr from raw data files. In this case, we will load some data stored in an R package.
- Go to Data Sources > Plus (+) > R.
- Under Name, enter the name BollenPoliticalDemocracy for the data set.
- Paste the code below into the R Code box 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:
- From the toolbar, select Calculation > Custom Code.
- Click on the page to place the calculation object.
- Paste in the code below in the R Code window and click Calculate in the object inspector.
# 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 corrections
y1 ~~ y5
y2 ~~ y4 + y6
y3 ~~ y7
y4 ~~ y8
y6 ~~ y8'
# Assemble the raw 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)
The output will appear as:
Step 3: Review the path diagram
To check that the model has been correctly specified it's a good idea to review the path diagram.
- From the toolbar, select Calculation > Custom Code.
- Click on the page to place the calculation object.
- Paste in the code below and click Calculate from the object inspector.
library(semPlot)
semPaths(fit)
The output will appear as:
Step 4: Extract the summary statistics
- From the toolbar, select Calculation > Custom Code.
- Click on the page to place the calculation object.
- Paste in the code below and click Calculate from the object inspector.
- To align the text neatly, go to Appearance > Appearance > Font and set the font to Courier New.
summary(fit)
The output will appear as:
Next