[R] How to avoid the NaN errors in dnbinom?

francogrex francogrex at mail.com
Wed Oct 24 12:33:25 CEST 2007


Thanks Jim, I corrected the error by avoiding negative K values: 
k1=abs(mu1/((var1/mu1)-1)) 
k2=abs(mu2/((var2/mu2)-1)) 

I am not sure about the general code to determine the parameters though,
sometimes it gives the expected values (determined in advance by rnbinom)
and sometimes it's way off.


jholtman wrote:
> 
> Your problems is that your code is generating NaN and 'err' is set to
> NaN and therefore the 'while (err > eps)' fails since NaN is not a
> logical variable.
> You should learn to use debug or the browser.  I put the following
> statement in your code:
> 
>         new.parms=c(k1,mu1,k2,mu2,prob)
>         err=max(abs((old.parms-new.parms)/new.parms))
>         if (is.nan(err)) browser()  # added statement
>         iter=iter+1
>     }
> 
> that caught the error and the printed out the values of some of your
> objects:
> Called from: mixnbinom(y, 10, 7, 20, 12, 0.8)
> Browse[1]> old.parms
> [1]  8.233552e+00  5.225557e+00 -2.487524e+05  1.512247e+01  9.668136e-01
> Warning message:
> In dnbinom(x, size, prob, log) : NaNs produced
> Browse[1]> new.parms
> [1] NaN NaN NaN NaN NaN
> Browse[1]>
> This shows that 'new.parms' at that point was all NaN.  You need to
> check your logic since is does say that "In dnbinom(x, size, prob,
> log) : NaNs produced".
> 

-- 
View this message in context: http://www.nabble.com/How-to-avoid-the-NaN-errors-in-dnbinom--tf4680211.html#a13383142
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list