Search code examples
rr-lavaanstructural-equation-model

Create a path diagram of a SEM model with a categorical response variable using semPaths()


I would like to create a path diagram of a SEM model with a categorical response variable using semPaths(). However I am running into an error:

library(lavaan)
library(semPlot)

table.7.5 <-read.table("http://www.da.ugent.be/datasets/Agresti2002.Table.7.5.dat",header=TRUE)

table.7.5$mental <- ordered(table.7.5$mental,levels = c("well","mild","moderate","impaired"))

model <- "mental ~ ses + life"

fit <- sem(model, data=table.7.5)

semPaths(fit,"std",edge.label.cex = 0.5, curvePivot=TRUE,layout = "tree")

Error is:

Error in colnames<-(*tmp*, value = "mental") : attempt to set 'colnames' on an object with less than two dimensions

Thanks


Solution

  • Someone helped me to solve the problem by replacing the content of cov by res.cov. I don't know why but when it's ordered data, lavaan puts the implied covariance matrix in res.cov instead of cov.

    All you have to do is this:

    fit@SampleStats@cov<-fit@[email protected]
    

    Before calling:

    semPaths(fit,"std",edge.label.cex = 0.5, curvePivot=TRUE,layout = "tree")