SachaEpskamp / semPlot

Path diagrams and visual analysis of various SEM packages' output
GNU General Public License v2.0
61 stars 20 forks source link

Models with "ordered = TRUE" cannot be plotted #40

Open FabianTomaschek opened 1 year ago

FabianTomaschek commented 1 year ago

Dear Dr. Epskamp,

I started to use the lavaan package to analyze linguistic data in which we are investigating the presence and absence of a variable. Since this is what is traditionally called binomial data, I used the "ordered =TRUE" parameter when estimating the SEM. Unfortunately, the semPaths function of your amazing semPlot package is not capable to plot the model.

Here, you can download our data: https://osf.io/shuxk/

In the SEM_online_question_SE.R you can see my code and how the code performs. It also contains the version information of the packages I am using.

Thanks FT

thepoorv commented 1 year ago

I am encountering a similar issue.

Here's the error for me:

Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent

Thank you Poorvi

mvanaman commented 7 months ago

Hi, I came here to report the same issue actually. Not sure what is going on with Jasp since I am an R user, but here is a reproducible example in case anyone finds it helpful:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(lavaan)
#> This is lavaan 0.6-16
#> lavaan is FREE software! Please report any bugs.
library(semPlot)

# am -> carb -> cyl with hp as covariate

mod <- '
# direct Effects
cyl ~ c * am + hp

# Indirect Effects
indirect := a*b

# Total Effects
total := c + (a*b)

# Mediating Effects
carb ~ a * am + hp
cyl ~ b * carb
'
fit <- sem(
  mod,
  data = mtcars,
  estimator = "ML"
)
semPaths(fit)


mtcars_2 <- mutate(
  mtcars,
  carb = factor(carb, ordered = TRUE),
  cyl = factor(cyl, ordered = TRUE)
)

fit_2 <- sem(
  mod,
  data = mtcars_2,
  estimator = "WLSMV"
)
semPaths(fit_2)
#> Error in dimnames(x) <- dn: length of 'dimnames' [2] not equal to array extent

Created on 2023-11-28 with reprex v2.0.2

jadenecke commented 3 months ago

running in to the same issue, but it seems like its not easily resolvable. The issues stems from a completely different output structure. The error occurs in the function semPlotModel_S4() finally in this line rownames(semModel@ObsCovs[[i]]) <- colnames(semModel@ObsCovs[[i]]) <- lavaanNames(object, type="ov"), however this is really only the point where it finally breaks. The returned covariance matrices and res.cov matrices are different in size from what is expected (i.e. one row/column for each variable in the model), however if an ordered variable is included, they are one row/column per level in the ordered variable. Furthermore the implied covariance matrix is also different, i.e. lapply(lavTech(object, "implied"), "[[", "cov") does not exists, however a cov.x implied covariance matrix exist. Sorry for the unstructured info, i don't really understand what these different matrices really are, but it seems to me no easy fix or bug, but an unsupported model structure. Maybe a small hint to that instead of the none-descriptive error message would be helpful in the meantime.