lavPredict error from a model obtained by sem() in Lavaan

302 views
Skip to first unread message

sc21...@uw.edu

unread,
Apr 19, 2018, 1:09:55 PM4/19/18
to lavaan
Dear All,

Good morning. 
May I ask a question about 'how to compute factor scores using Lavaan package'?
I tried lavPredict() which supports a model with numeric and ordinal items, but I got an error message.
When I dropped "ordered", with treating all items numeric, I did not get any error message (from lavPredict) but this is not a correct model...


# data and code
# There are 12 items, x1-x3, x10-x12 are count items, x4-x9 are ordinal.
# This is a part of my code:

        model.choi <- '     # freely estimate loading of the first item 
                # measurement model
                F =~ NA*x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12
                F ~~ 1*F
 
        # fit the model
        # THEMATRIX is a data.frame which is of a dimension 1600 x 12.
        mod.free.seme  = sem(model.choi , data=THEMATRIX, ordered=colnames(THEMATRIX)[4:9], missing = "pairwise")   

        # compuate factor scores
        lavPredict(mod.free.sem)

# end of code

I got the warnings as I run sem():  (I put warnings and errors in color for convenience of reading)

Warning messages:
1: In lav_data_full(data = data, group = group, cluster = cluster,  :
  lavaan WARNING: some cases are empty and will be ignored:
  1494
2: In lav_samplestats_from_data(lavdata = lavdata, missing = lavoptions$missing,  :
  lavaan WARNING: 5 bivariate tables have empty cells; to see them, use:
                  lavInspect(fit, "zero.cell.tables")

and I ended up with an error message when I tried lavPredict.

        Error in EETAx[[g]][i, , drop = FALSE] : subscript out of bounds


I used traceback() to follow the error lines, and found that the error(s) was(were) caused from internal function(s).

> traceback()
3: lav_predict_eta_ebm(lavobject = lavobject, lavmodel = lavmodel, 
       lavdata = lavdata, lavsamplestats = lavsamplestats, data.obs = data.obs, 
       eXo = eXo, optim.method = optim.method)
2: lav_predict_eta(lavobject = NULL, lavmodel = lavmodel, lavdata = lavdata, 
       lavsamplestats = lavsamplestats, lavimplied = lavimplied, 
       data.obs = data.obs, eXo = eXo, method = method, fsm = fsm, 
       optim.method = optim.method)
1: lavPredict(mod.free.sem)


May these errors be coming from the situation of sparseness and/or skewness of ordinal items?
I appreciate your comment/help/advice!!

Sincerely,
Seo-Eun

Terrence Jorgensen

unread,
Apr 20, 2018, 9:54:02 AM4/20/18
to lavaan
May these errors be coming from the situation of sparseness and/or skewness of ordinal items?

Does the error occur in the latest development version?

install.packages("lavaan", repos="http://www.da.ugent.be", type="source")

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

sc21...@uw.edu

unread,
Apr 20, 2018, 12:11:03 PM4/20/18
to lavaan
Thank you for your comment!!
Yes, the problem still remains..

               
                   > lavPredict(mod.free.sem)

                     
Error in EETAx[[g]][i, , drop = FALSE] : subscript out
of bounds


                   
> traceback()
                         
3: lav_predict_eta_ebm_ml(lavobject = lavobject, lavmodel = lavmodel,
                             lavdata
= lavdata, lavsamplestats = lavsamplestats, se = se,
                             level
= level, data.obs = data.obs, eXo = eXo, ML = FALSE,
                            optim
.method = optim.method)

                         
2: lav_predict_eta(lavobject = NULL, lavmodel = lavmodel, lavdata = lavdata,
                             lavsamplestats
= lavsamplestats, lavimplied = lavimplied,

                             se
= se, level = level, data.obs = data.obs, eXo = eXo, method = method,
                            fsm
= fsm, optim.method = optim.method)
                         
1: lavPredict(mod.free.sem)




Previously I used version 0.5-23.1097 at R 3.4.4. This time, I installed the developer's version (0.6-1.1215) as you suggested.

Sincerely,
Seo-Eun

sc21...@uw.edu

unread,
Apr 20, 2018, 1:57:02 PM4/20/18
to lavaan
Update:
I followed the trace (of error), and got stuck at

                                         
                         mu
.i <- EETAx[[g]][i,,drop=FALSE]



in the very last for-loop for FS in lav_predict_eta_ebm( ) .
I am afraid this is the place error occurs for my model.


On Friday, April 20, 2018 at 6:54:02 AM UTC-7, Terrence Jorgensen wrote:

sc21...@uw.edu

unread,
Apr 20, 2018, 2:49:55 PM4/20/18
to lavaan
Update2:

There is one participant, excluded due to missingness when I ran sem().  
EETAx has a length of 1599, instead of 1600.

The for-loop in lav_predict_eta_ebm is running i in 1:N, where N is 1600 from   
                     
                         
                           N
<- nrow(data.obs[[g]])


I am afraid if the error was due to this mismatch, because this loop runs to compute factor scores for all participants, even including a participant excluded for modeling.

Yves Rosseel

unread,
Apr 24, 2018, 8:37:31 AM4/24/18
to lav...@googlegroups.com
You are right. Could you send me your dataset and R script. That will
help me to fix this.

Yves.

On 04/20/2018 08:49 PM, sc21...@uw.edu wrote:
> Update2:
>
> There is one participant, excluded due to missingness when I ran */sem()/*.
> EETAx has a length of 1599, instead of 1600.
>
> The for-loop in lav_predict_eta_ebm is running i in 1:N, where N is 1600
> from
> |
>
>                            N <-nrow(data.obs[[g]])
>
> |
>
> I am afraid if the error was due to this mismatch, because this loop
> runs to compute factor scores for all participants, even including a
> participant excluded for modeling.
>
>
>
> On Friday, April 20, 2018 at 10:57:02 AM UTC-7, sc21...@uw.edu wrote:
>
> Update:
> I followed the trace (of error), and got stuck at
>
> |
>
>                          mu.i <-EETAx[[g]][i,,drop=FALSE]
>
>
> |
>
> in the very last for-loop for */FS/* in */lav_predict_eta_ebm( ) /*.
> I am afraid this is the place error occurs for my model.
>
>
> On Friday, April 20, 2018 at 6:54:02 AM UTC-7, Terrence Jorgensen wrote:
>
> May these errors be coming from the situation of sparseness
> and/or skewness of ordinal items?
>
>
> Does the error occur in the latest development version?
>
> |install.packages("lavaan", repos="http://www.da.ugent.be",
> type="source")|
>
>
> Terrence D. Jorgensen
> Postdoctoral Researcher, Methods and Statistics
> Research Institute for Child Development and Education, the
> University of Amsterdam
> UvA web page: http://www.uva.nl/profile/t.d.jorgensen
> <http://www.uva.nl/profile/t.d.jorgensen>
>
> --
> You received this message because you are subscribed to the Google
> Groups "lavaan" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to lavaan+un...@googlegroups.com
> <mailto:lavaan+un...@googlegroups.com>.
> To post to this group, send email to lav...@googlegroups.com
> <mailto:lav...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/lavaan.
> For more options, visit https://groups.google.com/d/optout.

sc21...@uw.edu

unread,
Apr 25, 2018, 2:27:28 PM4/25/18
to lavaan
Thank you!
I sent you .RData and code via email.

Yves Rosseel

unread,
Apr 27, 2018, 5:36:57 AM4/27/18
to lav...@googlegroups.com
On 04/25/2018 08:27 PM, sc21...@uw.edu wrote:
> Thank you!
> I sent you .RData and code via email.

I have not received it yet. Could you email it to Yves.Rosseel (at) UGent.be

Thanks in advance,

Yves.

Christopher Hübel

unread,
May 13, 2020, 11:50:45 AM5/13/20
to lavaan
Hi Yves,

I've got exactly the same problem. lavPredict() works if the variables are numeric, but it does not work if they are coded as factors. Did you find a solution for the problem?

Thanks
Topher

Yves Rosseel

unread,
May 13, 2020, 2:16:36 PM5/13/20
to lav...@googlegroups.com
On 5/13/20 5:50 PM, 'Christopher Hübel' via lavaan wrote:
> Hi Yves,
>
> I've got exactly the same problem.

Could you email me a reproducible example? An R script and a snippet of
the data, just enough to illustrate the problem.

Yves.

Yves Rosseel

unread,
May 13, 2020, 2:19:39 PM5/13/20
to lav...@googlegroups.com
Actually, even better would be to open an issue on github about this, so
it does not get 'forgotten'.

https://github.com/yrosseel/lavaan/issues

Yves.

On 5/13/20 5:50 PM, 'Christopher Hübel' via lavaan wrote:
> Hi Yves,
>
> I've got exactly the same problem. lavPredict() works if the variables
> are numeric, but it does not work if they are coded as factors. Did you
> find a solution for the problem?
>
> Thanks
> Topher
>
> On Friday, April 27, 2018 at 10:36:57 AM UTC+1, Yves Rosseel wrote:
>
> On 04/25/2018 08:27 PM, sc21...@uw.edu <javascript:> wrote:
> > Thank you!
> > I sent you .RData and code via email.
>
> I have not received it yet. Could you email it to Yves.Rosseel (at)
> UGent.be
>
> Thanks in advance,
>
> Yves.
>
> --
> You received this message because you are subscribed to the Google
> Groups "lavaan" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to lavaan+un...@googlegroups.com
> <mailto:lavaan+un...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/lavaan/84f0fb27-7a60-4c01-9b3e-2ba2e61cba43%40googlegroups.com
> <https://groups.google.com/d/msgid/lavaan/84f0fb27-7a60-4c01-9b3e-2ba2e61cba43%40googlegroups.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages