Removing Nodes from Structural Equation Models Using Mplus and SemPlot in R

Using Mplus and SemPlot in R: Removing Nodes from a Structural Equation Model (SEM)

Introduction

Structural equation models (SEMs) are a powerful tool for analyzing complex relationships between variables. Mplus, a popular software package for SEM analysis, provides an interface to plot the paths of these models using semPaths and qgraph packages in R. However, sometimes we need to remove nodes from these plots to better understand the relationships between other nodes.

In this article, we will explore how to remove nodes from a structural equation model (SEM) using Mplus and the semPlot package in R. We will walk through an example where we need to exclude one factor (F1) from the SEM and demonstrate how to achieve this using the semPlot and MplusAutomation packages.

Requirements

  • R
  • Mplus
  • qgraph
  • semPlot
  • MplusAutomation

Creating a Structural Equation Model (SEM)

To begin, let’s create an example structural equation model. This model will include two latent variables, F1 and F2, with two observed variables, X1 and Y1.

# Load required libraries
library(qgraph)
library(semPlot)
library(MplusAutomation)

# Create a sample dataset for the SEM
set.seed(123)
df <- data.frame(
  X1 = rnorm(100),
  Y1 = rnorm(100),
  group = rep(c("Group1", "Group2"), each = 50)
)

# Fit the SEM model using MplusAutomation
fit <- fitSem(df, 
               "X1 ~ F1 + e(X1)", 
               "Y1 ~ F1 + e(Y1)")

Examining the SEM Model Using semPaths

After fitting the SEM model, we can use the semPaths function to create a plot of the paths in the model.

# Download an output file from Mplus examples
download.file("http://www.statmodel.com/usersguide/chap5/ex5.8.out", 
             outfile <- tempfile(fileext = ".out"))

# Unadjusted plot
s <- semPaths(outfile, intercepts = FALSE)

Removing a Node from the SEM Model

To remove one factor (F1) from the SEM model, we first need to create the object of type semPlotModel using the MplusAutomation package.

# Call semPlotModel on your Mplus file
obj <- semPlot:::semPlotModel.mplus.model(outfile)

We then need to remove one factor (F1) from the object@Pars by identifying which variables contain the string “F1”.

# Remove one factor (F1) from object@Pars - need to check lhs and rhs columns
idx <- apply(obj@Pars[c("lhs", "rhs")], 1, function(i) any(grepl("F1", i)))
obj@Pars <- obj@Pars[!idx, ]

After removing the node, we can create a new plot of the paths in the updated SEM model.

# Remove one factor (F1) from object and pass to semPaths
s <- semPaths(obj, intercepts = FALSE)

Visualizing the Updated Plot

We can use the str() function to view the structure of the returned object.

# View the structure of the returned object
str(s)

The resulting plot will show the paths in the updated SEM model after removing one factor (F1).

Conclusion

In this article, we explored how to remove nodes from a structural equation model (SEM) using Mplus and the semPlot package in R. We demonstrated an example where we need to exclude one factor (F1) from the SEM and showed how to achieve this by creating the semPlotModel object first, removing the node from the object@Pars, and then passing it to the semPaths function. The resulting plot will provide a clearer understanding of the relationships between other nodes in the updated SEM model.

References

Example Use Cases

  • To remove multiple nodes from an SEM model, modify the idx variable to include additional strings that match other variables to be removed. For example: idx <- apply(obj@Pars[c("lhs", "rhs")], 1, function(i) any(grepl("F2|Y2", i)))
  • To create a different plot type (e.g., circular layout), modify the layout argument when calling semPaths. For example: s <- semPaths(obj, intercepts = FALSE, layout = "circular")

Additional Tips and Variations

  • Color schemes: Change the color scheme of the plot using the color argument in the semPaths function. For example: s <- semPaths(obj, intercepts = FALSE, color = "blue")
  • node size and shape: Adjust node sizes and shapes by modifying the sizeMan or intStyle arguments when calling semPaths. For example: s <- semPaths(obj, intercepts = FALSE, sizeMan = 3)

Last modified on 2024-05-05