yrosseel/lavaan

Duplicate code execution in lavResiduals

mronkko opened this issue · 1 comments

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.

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.