3

I'm trying to plot a sem-path with R.

Im using an OUT file provinent from Mplus with semPaths {semPLot}.

Apparently it seems to work, but i want to remove some latent variables and i don't know how.

I am using the following syntax :

Out from Mplus : https://www.dropbox.com/s/vo3oa5fqp7wydlg/questedMOD2.out?dl=0

outfile1 <- "questedMOD.out"
```

semPaths(outfile1,what="est", intercepts=FALSE, rotation=4,  edge.color="black", sizeMan=5, esize=TRUE, structural="TRUE",  layout="tree2", nCharNodes=0, intStyle="multi" )   
John Conde
  • 217,595
  • 99
  • 455
  • 496

2 Answers2

3

There may be an easier way to do this (and ignoring if it is sensible to do it) - one way you can do this is by removing nodes from the object prior to plotting.

Using the Mplus example from your question Rotate Edges in semPaths/qgraph

library(qgraph)
library(semPlot)
library(MplusAutomation)

# This downloads 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)

enter image description here

In the above call to semPaths, outfile is of class character, so the line (near the start of code for semPaths)

 if (!"semPlotModel" %in% class(object)) 
                    object <- do.call(semPlotModel, c(list(object), modelOpts))  

returns the object from semPlot:::semPlotModel.mplus.model(outfile). This is of class "semPlotModel".

So the idea is to create this object first, amend it and then pass this object to semPaths.

# Call semPlotModel on your Mplus file
obj <- semPlot:::semPlotModel.mplus.model(outfile)
# obj <- do.call(semPlotModel, list(outfile)) # this is more general / not just for Mplus

# 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, ]

class(obj)

obj is now of class "semPlotModel" and can be passed directly to semPaths

s <- semPaths(obj, intercepts = FALSE)

enter image description here

You can use str(s) to see the structure of this returned object.

Community
  • 1
  • 1
user20650
  • 24,654
  • 5
  • 56
  • 91
0

Assuming that you use the following sempath code to print your SEM

semPaths(obj, intercepts = FALSE)%>%
   plot()

you can use the following function to remove any node by its label:

remove_nodes_and_edges <- function(semPaths_obj,node_tbrm_vec){
   
   relevent_nodes_index <- semPaths_obj$graphAttributes$Nodes$names %in% node_tbrm_vec

   semPaths_obj$graphAttributes$Nodes$width[relevent_nodes_index]=0

   semPaths_obj$graphAttributes$Nodes$height[relevent_nodes_index]=0
   
   semPaths_obj$graphAttributes$Nodes$labels[relevent_nodes_index]=""

   return(semPaths_obj)

}

and use this function in the plotting pipe in the following way

semPaths(obj, intercepts = FALSE) %>%
   remove_nodes_and_edges(c("Y1","Y2","Y3")) %>%
   plot()
amann
  • 369
  • 1
  • 8