yrosseel / lavaan

an R package for structural equation modeling and more
http://lavaan.org
431 stars 100 forks source link

Predicting latent factors #380

Open AroobIqbal opened 2 months ago

AroobIqbal commented 2 months ago

I am using lavpredict however I am getting this error: "Error: lavaan->lav_predict_fy_eta.i():
unknown type: `character' for variable: as1" I see there is an earlier thread on this. And lionel posted this response. I follow the advice but still get the error. Would really appreciate advice on what to do.

The labelled class is considered to be a temporary class, and the onus is on the user to convert labelled classes to factors before getting into data analysis. In any case, lavaan should not do anything. I am not sure how converting to data frame fixes the issue with inputs containing haven-labelled columns.

Regarding: https://github.com/yrosseel/lavaan/blob/290cc70ee05ccf7643cdc6d40e4c4fcad3ec8f53/R/xxx_lavaan.R#L64-L67

I recommend doing this for any classes that inherit from data frame. For instance data.table does data-masking among other special interpretation of subsetting. This could introduce scoping issues in your code. Another example, sf data frames maintain a sticky geometry column, which means that the usual invariant length(sf[1:n]) == n is not applicable.

If you convert inputs to a data frame, there is no need to hope for the best because you control exactly the interface you're using.

Originally posted by @lionel- in https://github.com/yrosseel/lavaan/issues/163#issuecomment-619882512

yrosseel commented 2 months ago

In 0.6-18, we do convert all objects that inherit from data.frame to a regular data.frame. See https://github.com/yrosseel/lavaan/blob/6e8866831497b0bf810b02e0e4987b58b9819a0f/R/lav_lavaan_step00_init.R#L106C2-L108C34

Are you using 0.6-18? If so, could you please provide a minimal reprex?