make predictions from lavaan fit?

121 views
Skip to first unread message

ya

unread,
Jul 2, 2019, 4:17:52 AM7/2/19
to lavaan
Dear list,

Is it possible to use lavaan to do machine learning with training and test dataset? I have a training dataset for a logistic regression model, the reason I want to use lavaan is that I have missing values in the dataset, I would like to use FIML in lavaan to handle missingness. However, I could not feed the model fit to the test dataset and make predictions. Is lavaan compatible with the predict() function in R base? Is there anyway to do this machine learning predictions? Thank you very much.

Below is an replicable code using mtcars dataset in R:

train.index=sample(32,20)
train=mtcars[train.index,]# this model use the validation set approach
test=mtcars[-train.index,] 
mod='am~vs+wt'
fit=sem(mod,data=train,ordered=c("am"))
summary(fit)
lavaan 0.6-3 ended normally after 11 iterations

 Optimization method                           NLMINB
 Number of free parameters                          3

 Number of observations                            20

 Estimator                                       DWLS      Robust
 Model Fit Test Statistic                       0.000       0.000
 Degrees of freedom                                 0           0
 Minimum Function Value               0.0000000000000
 Scaling correction factor                                     NA
 Shift parameter                                     
   for simple second-order correction (Mplus variant)

Parameter Estimates:

 Information                                 Expected
 Information saturated (h1) model        Unstructured
 Standard Errors                           Robust.sem

Regressions:
                  Estimate  Std.Err  z-value  P(>|z|)
 am ~                                                
   vs               -2.560    2.692   -0.951    0.342
   wt               -4.108    3.321   -1.237    0.216

Intercepts:
                  Estimate  Std.Err  z-value  P(>|z|)
  .am                0.000                           

Thresholds:
                  Estimate  Std.Err  z-value  P(>|z|)
   am|t1           -13.412   10.972   -1.222    0.222

Variances:
                  Estimate  Std.Err  z-value  P(>|z|)
  .am                1.000                           

Scales y*:
                  Estimate  Std.Err  z-value  P(>|z|)
   am                1.000                           


predict(fit,test,type="response")
Error in .local(object, ...) : unused argument (type = "response")
predict(fit,data.frame(vs=0,wt=0.5))
Error in lav_data_full(data = data, group = group, cluster = cluster,  : 
 lavaan ERROR: missing observed variables in dataset: am

Terrence Jorgensen

unread,
Jul 2, 2019, 5:09:41 AM7/2/19
to lavaan
Is lavaan compatible with the predict() function in R base?

No, lavaan stands for LAtent VAriable ANalysis.  Its predict() method is used for calculating factor-score estimates, which is meaningless in path models without latent variables.  The usual predict() methods for calculating expected values of observed outcomes requires knowing what the values of your predictors are, which is unavailable by definition if you have latent predictors.

Is there anyway to do this machine learning predictions? 

Jarrett Byrnes wrote a predict() method customized for path models in lavaan.  You can download it here:


But I can't speak as to how it will work.  Your model is quite simple, so I expect it will provide what you need.

Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Terrence Jorgensen

unread,
Jul 2, 2019, 5:12:02 AM7/2/19
to lavaan
I expect it will provide what you need.

Actually, I take that back.  Your model has a categorical outcome, and you are requesting predictions on the "response" scale (i.e., probabilities).  I don't know if Jarrett's function provides that (it might, you can read through it).  But for simple models, if you understand the parameters of your model, you should be able to write a function to calculate the predicted probabilities you need from lavaan's parameters.
Reply all
Reply to author
Forward
0 new messages