In log(pi.i) : NaNs produced

298 views
Skip to first unread message

Para Jansuwan

unread,
Nov 28, 2020, 3:47:36 AM11/28/20
to lavaan
Hi everyone,

What is the "In log(pi.i) : NaNs produced"?
And how can I solve it?

Thank you in advance for your reply,
Para

balal izanloo

unread,
Nov 28, 2020, 6:44:46 AM11/28/20
to lav...@googlegroups.com
Hi
What is your command that result to this error?
First look to this page may be help to resolve this issue.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/629fd90c-fa6e-4781-a162-a7cac45744c0n%40googlegroups.com.

Patrick (Malone Quantitative)

unread,
Nov 28, 2020, 10:21:01 AM11/28/20
to lav...@googlegroups.com
Most likely you have values in pi.i that are not positive numbers.
> --
> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/629fd90c-fa6e-4781-a162-a7cac45744c0n%40googlegroups.com.



--
Patrick S. Malone, Ph.D., Malone Quantitative
NEW Service Models: http://malonequantitative.com

He/Him/His

Michael P. Grosz

unread,
Nov 28, 2020, 4:29:49 PM11/28/20
to lavaan
Hi everybody,

I just got more than 50 times the same warning: In log(pi.i) : NaNs produced
Plus, when I look at the summary, there are no standard errors and the fit stats are very odd.

Here is my R code with the model that I fit:
m1 <- 'RIV =~ narq13 + narq14 + narq17 + narq6 + narq9 + narq10 + narq4 + narq11 + narq12
       Status =~ status1 + status2 + status3
       Status ~ RIV + sex + age'

fit1 <- sem(model=m1, data=lav.dat)
summary(fit1,fit.measures=TRUE)


The narq items are ordered so that lavaan uses the DWLS / WLSMV estimator.
I do not get any warnings when I treat the narq items as numeric (i.e, when lavaan uses the ML estimator).

Because you were talking about negative values. I double checked the item scores of the narq items. All of them are positive.
What is pi.i?
What might be the problem and a solution?

best,
Michael

Terrence Jorgensen

unread,
Nov 30, 2020, 7:25:30 AM11/30/20
to lavaan
pi.i is a casewise probability.  Not sure how it could occur that one would be negative, which is the only thing that would trigger log() to return NaN, except the arbitrary case log(NaN).  Perhaps there is a case with pi.i == 0, and machine imprecision just yielded something like -1e-16 in this case.  
  1. Check your univariate tables for each ordinal indicator to see if any categories are zero (although lavaan should already be catching cases like that, and returning more informative messages).  
  2. Also, check whether this happens when you remove age + sex from your model, or if you leave them in but set conditional.x=FALSE.  There may be a situation where the zero counts only occur for subsets of the data (e.g., contingency tables of sex by each indicator), so conditioning on the exogenous covariate is leading to pi.i being zero (but machine imprecision yields a super small negative value). 
  3. You do not declare any variables ordered=, so I assume they are already of class c("ordered","factor") in your data.frame.  Try checking their classes: lapply(lavNames(fit1), function(x) class(lav.dat[,x])).  The exogenous predictors (age + sex) should be "numeric" class, using dummy/contrast codes to represent grouping variables: https://lavaan.ugent.be/tutorial/cat.html

I do not get any warnings when I treat the narq items as numeric (i.e, when lavaan uses the ML estimator).

How many categories do these indicators have? 

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

Patrick (Malone Quantitative)

unread,
Nov 30, 2020, 9:52:47 AM11/30/20
to lav...@googlegroups.com
Terrence,

Trying to take the log of zero would be sufficient to produce an
NaN/undefined--it wouldn't need to be a negative value.
> --
> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/f427f757-2906-4e65-adc2-27053ab8592cn%40googlegroups.com.

Michael P. Grosz

unread,
Nov 30, 2020, 11:18:48 AM11/30/20
to lavaan
ok, thanks a lot for the advice!

Terrence Jorgensen

unread,
Dec 3, 2020, 4:33:33 AM12/3/20
to lavaan
Trying to take the log of zero would be sufficient to produce an
NaN/undefined--it wouldn't need to be a negative value.

When I run log(0) on R 4.0.3, it returns -Inf, not NaN.

Patrick (Malone Quantitative)

unread,
Dec 3, 2020, 7:51:53 AM12/3/20
to lav...@googlegroups.com
I stand corrected. 

--
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.

Yves Rosseel

unread,
Dec 9, 2020, 3:05:54 AM12/9/20
to lav...@googlegroups.com
In any case, this is not supposed to happen. I would like to investigate
this further. Would you be able to email me your dataset (or a snippet
of the datset) and a small R script, so I can reproduce this?

Please send it to Yves dot Rosseel at UGent dot be

Yves.

On 11/30/20 5:18 PM, Michael P. Grosz wrote:
> ok, thanks a lot for the advice!
>
> On Monday, November 30, 2020 at 1:25:30 PM UTC+1 Terrence Jorgensen wrote:
>
> pi.i is a casewise probability.  Not sure how it could occur that
> one would be negative, which is the only thing that would
> triggerlog() to return NaN, except the arbitrary case log(NaN).
> Perhaps there is a case withpi.i == 0, and machine imprecision just
> yielded something like-1e-16 in this case.
>
> 1. Check your univariate tables for each ordinal indicator to see
> if any categories are zero (although lavaan should already be
> catching cases like that, and returning more informative messages).
> 2. Also, check whether this happens when you remove age + sex from
> your model, or if you leave them in but set
> conditional.x=FALSE.  There may be a situation where the zero
> counts only occur for subsets of the data (e.g., contingency
> tables of sex by each indicator), so conditioning on the
> exogenous covariate is leading topi.i being zero (but machine
> imprecision yields a super small negative value).
> 3. You do not declare any variablesordered=, so I assume they are
> already of classc("ordered","factor") in your data.frame.  Try
> checking their classes:lapply(lavNames(fit1), function(x)
> class(lav.dat[,x])).  The exogenous predictors (age + sex)
> should be"numeric" class, using dummy/contrast codes to
> represent grouping variables:
> https://lavaan.ugent.be/tutorial/cat.html
> <https://lavaan.ugent.be/tutorial/cat.html>
>
>
> I do not get any warnings when I treat the narq items as numeric
> (i.e, when lavaan uses the ML estimator).
>
>
> How many categories do these indicators have?
> http://psychmodels.ucdavis.edu/uploads/8/6/3/9/86398102/rhemtullabrosseauliardsavalei_pm.pdf
> <http://psychmodels.ucdavis.edu/uploads/8/6/3/9/86398102/rhemtullabrosseauliardsavalei_pm.pdf>
>
> Terrence D. Jorgensen
> Assistant Professor, Methods and Statistics
> Research Institute for Child Development and Education, the
> University of Amsterdam
> 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 view this discussion on the web visit
> https://groups.google.com/d/msgid/lavaan/2a0ace8f-c30f-4874-b85d-a02ad9a25185n%40googlegroups.com
> <https://groups.google.com/d/msgid/lavaan/2a0ace8f-c30f-4874-b85d-a02ad9a25185n%40googlegroups.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages