SachaEpskamp / semPlot

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

Error in colMeans when trying to plot model with ordered variables using a self defined matrix layout #27

Open adriaanvl opened 4 years ago

adriaanvl commented 4 years ago

Dear Sacha,

Whilst trying to plot a model, I get the following error

Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric

I get this error only in the following combination (both conditions must be present): Condition 1: When the sem(fit, df, ordered=c("X1", "X2"...)) is instructed to treat specific variables as ordered. Condition 2: When in the semPaths command I want to use my own matrix than the standard one provided by the software.

With my data, when I remove either ordered=, or when use one of the predefined layouts (tree, circle, etc.), I do not get the above problem.

I do not wish to burden you with my data or code, so below is an example which reproduces the result (The example, not my data, creates a problem when I don't define specific variables as ordered, but hopefully the below example is helpful).

set.seed(1234)
X1 <- sample(0:2, 100, replace=TRUE)
X2 <- sample(0:2, 100, replace=TRUE)
X3 <- sample(0:2, 100, replace=TRUE)
X4 <- sample(0:2, 100, replace=TRUE)

df <- data.frame(X1, X2, X3, X4)

example.model <- '
latent =~ X1 + X2 + X3 + X4
'

fit <- cfa(example.model,
           df,
           ordered = c("X1","X2","X3","X4")
           )
summary(fit, fit.measures=TRUE, standardized=TRUE)

#create plot
semPaths(fit)

# Create plot using self defined matrix
L <- matrix(
  c(
    NA,       "latent",     NA,     NA, 
    "X1",   "X2",   "X3", "X4") 
  ,4 )

Graph <- semPaths(fit, layout=L)
Graph$graphAttributes$Edges$curve <-
  ifelse(Graph$Edgelist$bidir, 1, 0)
plot(Graph)