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!
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:
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:
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:
Then specify that matrix in the
layout
argument ofsemPaths()
:Before manual layout, default "tree" layout.
After manual layout, mediators moved to their own layer.