Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rpois generates NA with large means (lambda) in R

Tags:

r

lambda

poisson

I am debugging a larger set of nested models that only run into problems during optimization. During the process of zeroing on what I believe is causing the errors I've come across unusual behavior in the rpois() function.

It seems that with very large mean values, rpois() returns NA instead of a value. This problem does not generate a warning. See below for a reproducible set of code.

> rpois(1,3000000000)
[1] NA

My question is two fold:
1 - why is it showing this behavior (is there a max limit on the size of an integer for the rpois function?) and
2 - is there a work around to prevent the generation of NA (even if that's to limit the size of the mean input to some smaller value)?

I am running 32x R version 3.0.2 in 64x Windows 7.

like image 541
user3658540 Avatar asked Mar 20 '23 13:03

user3658540


1 Answers

The problem is that rpois returns an integer, and it converts the value to NA if the value is greater than the maximum possible integer value (.Machine$integer.max).

rpois(1,.Machine$integer.max/1.00001)
## [1] 2147428954
rpois(1,.Machine$integer.max/1)
## [1] NA

The Normal approximation should be insanely precise in this case (it's generally extremely good if the mean is greater than 100!): if your mean is greater than (say) 0.999*.Machine$integer.max, you can use round(rnorm(1,mean=lambda,sd=sqrt(lambda)))

like image 104
Ben Bolker Avatar answered Mar 23 '23 07:03

Ben Bolker