Reputation: 11686
How can I get more significant digits in R? Specifically, I have the following example:
> dpois(50, lambda= 5)
[1] 1.967673e-32
However when I get the p-value:
> 1-ppois(50, lambda= 5)
[1] 0
Obviously, the p-value is not 0. In fact it should greater than 1.967673e-32 since I'm summing a bunch of probabilities. How do I get the extra precision?
Upvotes: 2
Views: 1159
Reputation: 226162
Use lower.tail=FALSE
:
ppois(50, lambda= 5, lower.tail=FALSE)
## [1] 2.133862e-33
Asking R to compute the upper tail is much more accurate than computing the lower tail and subtracting it from 1: given the inherent limitations of floating point precision, R can't distinguish (1-eps
) from 1 for values of eps
less than .Machine$double.neg.eps
, typically around 10^{-16} (see ?.Machine
).
This issue is discussed in ?ppois
:
Setting ‘lower.tail = FALSE’ allows to get much more precise results when the default, ‘lower.tail = TRUE’ would return 1, see the example below.
Note also that your comment about the value needing to be greater than dpois(50, lambda=5)
is not quite right; ppois(x,...,lower.tail=FALSE)
gives the probability that the random variable is greater than x
, as you can see (for example) by seeing that ppois(0,...,lower.tail=FALSE)
is not exactly 1, or:
dpois(50,lambda=5) + ppois(50,lambda=5,lower.tail=FALSE)
## [1] 2.181059e-32
ppois(49,lambda=5,lower.tail=FALSE)
## [1] 2.181059e-32
Upvotes: 8