alishinski / lavaanPlot

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

stand=TRUE does not work in lavaanPlot2 #44

Closed SilviaMorgenstern closed 1 year ago

SilviaMorgenstern commented 1 year ago

The package is great and the plots are so much better than semplot, but I have an issue in getting standard coefficients if I use lavaanPlot2. I am trying to plot a quite complex SEM with nine latent variables and more than 30 observed variables.

If I ask a plot with lavaanPlot everything works fine. lavaanPlot(model = model, coefs = TRUE, stand = TRUE, sig = 0.05, stars = c("regress"))

The following with lavaanPlot2 works fine too

n_opts <- formatting(list(shape = "polygon", sides = "10", color = "red", penwidth = 5), list(shape = "polygon", sides = "4",color = "grey"), type = "node")
e_opts <- formatting(list(color = "red", penwidth = 5),list(color = "grey", penwidth = 2), list(color = "blue"), type = "edge")
lavaanPlot2(
  model, 
  graph_options = list(label = "model plot", rankdir = "LR"),
  node_options = n_opts,
  edge_options = e_opts,
  stars = c("regress"),
  coef_labels = TRUE,
  stand = FALSE
)

But if I replace "stand = FALSE" with "stand = TRUE", it does not work and gives this error

`Error in `dplyr::mutate()`:
ℹ In argument: `stars = map(.data$p_val, sig_stars)`.
Caused by error in `map()`:
ℹ In index: 91.
Caused by error in `if (pvals <= 0.001) ...`:
! missing value where TRUE/FALSE needed
Run `rlang::last_error()` to see where the error occurred.
rlang::last_error()
<error/dplyr:::mutate_error>
Error in `dplyr::mutate()`:
ℹ In argument: `stars = map(.data$p_val, sig_stars)`.
Caused by error in `map()`:
ℹ In index: 91.
Caused by error in `if (pvals <= 0.001) ...`:
! missing value where TRUE/FALSE needed
---
Backtrace:
  1. lavaanPlot::lavaanPlot2(...)
 14. purrr::map(.data$p_val, sig_stars)
 15. purrr:::map_("list", .x, .f, ..., .progress = .progress)
 19. lavaanPlot (local) .f(.x[[i]], ...)`

Any idea why? Has anyone had this issue before?

alishinski commented 1 year ago

Thanks for trying out lavaanPlot2 and sharing the error.

Does it give you the same error if you remove the significance stars?

SilviaMorgenstern commented 1 year ago

I have tried removing the significance stars, but unfortunately it does not help. I have tried also in a different dataset, testing a slightly different model and I still have the same issue. All works fine, until I ask lavaanPlot2 to use stand = TRUE

alishinski commented 1 year ago

Thanks for giving that a try. That's what I expected to happen, I just wanted to be sure.

Could you show me what it looks like when you do standardizedSolution() with you model?

SilviaMorgenstern commented 1 year ago

The output looks normal, here is a screenshot of the first lines image

alishinski commented 1 year ago

I'm looking for whether you have latent variable variances with a NA in the z column

SilviaMorgenstern commented 1 year ago

Interesting! I do. I have no NA until I check the z of a covariate with itself, e.g., ATTITUDE ~~ ATTITUDE. In that case, for some variables I have a value of 0.000, sometimes it says NA. I never noticed before. Any idea why that would happen? I should not have any missing value in the dataset.

alishinski commented 1 year ago

I think that is because those latent variable variances are fixed to 1 for the model estimation (you can change them to something else too I assume) so they're not estimated, and so they don't have a standard error or a z score.

SilviaMorgenstern commented 1 year ago

I understand, any idea why that does not happen with lavaanPlot? Meanwhile, I am trying to understand how can I set lavaan to estimate them instead.

alishinski commented 1 year ago

It's just to do with picayune details of the differences between lavaanPlot and lavaanPlot2.

I don't know if you can estimate them, I think it could be a necessary model constraint. Or you'd have to constrain the latent variable coefficients maybe.

alishinski commented 1 year ago

I just pushed https://github.com/alishinski/lavaanPlot/commit/2292161266eb64cf261417124471f3f84f2ce77c which should fix the problem, give it a shot and let me know.

SilviaMorgenstern commented 1 year ago

Wow, thank you so much! I will try and let you know!

SilviaMorgenstern commented 1 year ago

It works! Thank you so much again! Any idea when this will be updated in CRAN? No rush, I ask just to avoid messing up between the dev version on GitHub and the stable on CRAN.

alishinski commented 1 year ago

Glad to hear it!

I was planning to wait till I could iron out difficulties with the new stuff from 0.7.0 and 0.8.0 before I went to CRAN again. This helped towards that, I'll probably do it sometime in the next month or two.

But you can keep using the dev version, I haven't broken backwards compatibility so everything that worked in the newest CRAN version should still work the same with the dev version.

SilviaMorgenstern commented 1 year ago

Great, thank you!