Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate code execution in lavResiduals #291

Open
mronkko opened this issue Aug 18, 2023 · 1 comment
Open

Duplicate code execution in lavResiduals #291

mronkko opened this issue Aug 18, 2023 · 1 comment

Comments

@mronkko
Copy link

mronkko commented Aug 18, 2023

I am troublehshooting a fitted lavaan object, which is available here fit.Rdata.zip. Printing out the standardized residuals takes about 12 minutes on an M1 Pro mac. To understand why calculating residuals takes so long, I profiled the code

library(profvis) profvis({ a <- lavResiduals(fit) })

This produces the attached profile lavResiduals.Rprofvis.zip. The flame graph shows two identical computationally expensive call traces:

Screenshot 2023-08-18 at 18 09 52

The first set of expensive calls stata in a call to lav_residuals_se. The law_residual_se function contains a call to lav_residuals_acov

# get ACOV per group ACOV.res <- lav_residuals_acov(object = object, type = type, z.type = z.type, h1.acov = h1.acov)

Later on in the lav_residuals, there is a call to lav_residuals_summary. The lav_residuals_summary function contains a call to lav_residuals_acov

srmrList.se <- lav_residuals_acov(object = object, type = "cor.bentler", z.type = "standardized", h1.acov = "unstructured")

Both calls lead to calling lav_model_h1_acov using the same parameter values. Link to code

ACOV.obs <- lav_model_h1_acov(lavobject = object, h1.information = h1.acov)

This means that the computer is calculating the same ACOV.obs twice, nearly doubling the computational time. This is a non-trivial problem with models that have many variables.

@yrosseel
Copy link
Owner

True indeed. Not ideal at all. This is one of the (many) things I need to address when rewriting lav_residuals.R. On my TODO list (see also open issue 238) but not high priority.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants