Hello! I realize this thread is a bit old but have run into a similar situation and am interested to see if anyone has follow-ups to report or any feedback on the solution proposed below.
I am in a situation where I want to fit many models (> 200) to a large (80 X 80) polychoric correlation matrix. To reduce computational time, I would like to compute the polychorics only once and input them into lavaan.
This led me to essentially the same code posted above by Rico and Terrence above (reproduced here for clarity):
fitcov <- cfa(model=mod,
sample.cov=lavCor(data, ordered=names(data)),
sample.nobs=20000,
parameterization='theta',
WLS.V=lavInspect(fit, "wls.v"),
NACOV=lavInspect(fit, "gamma"),
std.lv=T,
estimator="WLSMV")
where fit is a model run on the categorical raw data.
As Yves noted previously, this won't work because when sample.cov is used, lavaan assumes the data are continuous. I guess no update on that yet?
Anyway, when the data are continuous, this means that WLS.V and NACOV should be ordered as follows (taken from the help documentation for lavaan):
"The elements of the weight matrix should be in the following order (if all data is continuous): first the means (if a meanstructure is involved), then the lower triangular elements of the covariance matrix including the diagonal, ordered column by column." (quote_1)
On the other hand, the "wls.v" and "gamma" output from the categorical raw data fit will be organized for the categorical case:
"First the thresholds (including the means for continuous variables), then the slopes (if any), the variances of continuous variables (if any), and finally the lower triangular elements of the correlation/covariance matrix excluding the diagonal, ordered column by column." (quote_2)
The error that lavaan throws when running the cfa above is that the dimensions of WLS.V (and NACOV) are not conformable with the sample.cov. This is because they are obtained from the categorical raw data and therefore include the thresholds but do not include the diagonal elements of the covariance (polychoric correlation) matrix.
I am stubbornly still trying to make this work and I think I have found a solution. But I am not sure why (or if) it works in theory and am looking for any feedback. The solution is as follows.
We can get the elements of "wls.v" and "gamma" for the off-diagonal elements of the polychoric matrix using lavInspect with the raw data fit, as above. We can also order these as required in quote_1.
However, I am not sure what we could use for the elements of "wls.v" and "gamma", in place of the variances from quote_1? If anyone knows of relevant theory here, please send a reference!
Anyway, on a hunch I just used an identity (sub) matrix for the elements of "wls.v" and "gamma" for the variances. The output below compares the fit of this approach with the categorical raw data using a toy example (a 2-factor cfa with 6 ordinal indicators). The proposed approach gave the same GOF and results for the factor loadings, and even reproduced the point estimates for the residual variances (not shown). More surprising, the results of fit_poly did not change when I rescaled elements of "wls.v" and "gamma" for the variances by any constant.
I am tentatively making the following conclusions:
1. You can pass the polychorics to lavaan so long as the WLS.V and NACOV are organized as described in quote_1.
2. It doesn't matter what you use for the variances in quote_1, but an identity (sub) matrix works fine for them both. I have no theory to back this up.
Any input would be welcome! I would like to know if I am making a mistake before I try this with 200+ models!
Thanks, Peter