Adding more horizontal levels and standard errors in SemPaths diagram

1.5k Views Asked by At

I have a lavaan SEM model of observed variables only which I would like to plot to three horizontal levels with command semPaths in semPlot-package.

However, the default in semPaths is: "The default "tree" layout under default rotation places the nodes in one of four horizontal levels. At the top the exogenous manifest variables, under that the exogenous latent variables, under that the endogenous latent variables and at the bottom the endogenous manifest variables." Therefore, semPath plots my model in two horizontal levels only, and I don't know how to add more levels.

Here is my code:

mod1<-'
y ~ t1 + t2 + t3
t1 ~   t2 + t4 + t5 +  t3
t3 ~  t2 
' 
mod1.fit<-sem(mod1, data=data)
summary(mod1.fit, standardized=TRUE, rsq=TRUE,fit.measures=TRUE)
sem<-semPaths(mod1.fit, what="std",  residuals=FALSE, layout="tree", rotation=2,  nCharNodes=0, sizeMan=10, color=c("white"), edge.color=c("black"),  title=FALSE, exoVar=FALSE, exoCov=FALSE, curvePivot=TRUE, "std", cut=0.1, edge.lable.cex=1.5)

Additionally, I haven't found out how to add standard errors to the path diagram.

Thank you for your help in advance!

1

There are 1 best solutions below

0
On

You will need to manually specify a matrix of (x, y) coordinates to be used as a layout.

You can see which nodes will get which rows of the matrix by plotting the model with numeric node labels:

semPaths(mod1.fit, intercepts = F, nodeLabels = 1:7)

Note that having intercepts (as plotted by default) means you will need to specify coordinates for all intercepts of all endogenous variables, which seems to me a tremendous chore:

semPaths(mod1.fit, intercepts = T, nodeLabels = 1:10)

It seems that nodes are numbered in the order that they are specified in the model, with exogenous variables coming last and in the order they were called by the regression formulae. So in your matrix, you will give coordinates for y, t1, and t3, then t2, t4, and t5.

I find it easier to make x and y vectors, then combine them in a matrix:

x = c(0, -1, 1, 0, -1, 1)
y = c(-1, 0, 0, 0, 1, 1)
ly = matrix(c(x, y), ncol=2)

Then specify that matrix in the layout argument of semPaths():

semPaths(mod1.fit, layout=ly)

Here's what I had before manual layout.

Before manual layout, default "tree" layout. Here's what I had after manual layout.

After manual layout, mediators moved to their own layer.