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.
Method
Step 1: Load the data
Typically, data sets are loaded into Displayr from raw data files. In this case, we will load data from 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 Code
panel and click OK.
library(sem) Bollen
Step 2: Fit the model
The difficult step is fitting the model, which requires specifying the measurement model, the relationships to be tested (i.e., the regressions), and the model's correlation structure. For more information, please check out the lavaan website.
To do this:
- From the toolbar or from + in the Report tree, select Calculation
> Custom Code.
-
Paste the code below into the Code
panel and click 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 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 or from the + menu in the Report tree, select Calculation
> Custom Code.
-
Paste in the code below and click Calculate.
library(semPlot) semPaths(fit)
The output will appear as:
Step 4: Extract the summary statistics
- From the toolbar or from the + menu in the Report tree, select Calculation
> Custom Code.
-
Paste in the code below into the Code
panel and click Calculate.
summary(fit)
- To align the text neatly, go to Properties
> Appearance > Appearance > Font and set the font to Courier New.
The output will appear as:
Next