alishinski / lavaanPlot

R package for plotting lavaan path models with DiagrammeR
http://alexlishinski.com/lavaanPlot/
39 stars 4 forks source link

covs does not plot observed variables #20

Closed willynilly closed 4 years ago

willynilly commented 4 years ago

The covs parameter does not plot observed variables. Can you add this?

krivit commented 4 years ago

I am running into a similar issue. From http://lavaan.ugent.be/tutorial/cov.html ,

library(lavaan)
library(lavaanPlot)

lower <- '
 11.834
  6.947   9.364
  6.819   5.091  12.532
  4.783   5.028   7.495   9.986
 -3.839  -3.889  -3.841  -3.625  9.610
-21.899 -18.831 -21.748 -18.775 35.522 450.288 '

wheaton.cov <- 
    getCov(lower, names = c("anomia67", "powerless67", 
                            "anomia71", "powerless71",
                            "education", "sei"))

# classic wheaton et al model
wheaton.model <- '
  # latent variables
    ses     =~ education + sei
    alien67 =~ anomia67 + powerless67
    alien71 =~ anomia71 + powerless71
  # regressions
    alien71 ~ alien67 + ses
    alien67 ~ ses
  # correlated residuals
    anomia67 ~~ anomia71
    powerless67 ~~ powerless71
'
fit <- sem(wheaton.model, 
           sample.cov = wheaton.cov, 
           sample.nobs = 932)

lavaanPlot(model=fit, covs=TRUE)

I would expect it to have edges between the anomia67 and the anomia71 nodes and similarly for powerlessness, but instead, I get the following: image

Is this by design? As far as I can tell, the issue is the following line: https://github.com/alishinski/lavaanPlot/blob/1e1066b0b3870e891f646e11b97e09e0c27f64e0/R/lavaanPlot.R#L26 The filter (ParTable$rhs %in% ParTable$lhs[latent | regress]) excludes manifest variables. Removing it makes the covariance edges appear, though I don't know what else it might do: image

By the way, it looks like they have coefficients even if coefs=FALSE---but that may be a separate issue.

alishinski commented 4 years ago

@krivit You're exactly right about what's causing the issue. I've removed it, so now you can plot residual covariances. I don't think that it should cause any other problems, I think that I put that there unnecessarily to exclude residual variances. But we'll see. Test out the new version and see if you can make it malfunction.

krivit commented 4 years ago

Looks like it works now. Thanks!