2

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?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
user1357015
  • 11,168
  • 22
  • 66
  • 111
  • Look [here](http://stackoverflow.com/questions/7681756/rounding-numbers-in-r-to-specified-number-of-digits/7682869) and [here](http://stackoverflow.com/questions/3245862/format-numbers-to-significant-figures-nicely-in-r) – nograpes Apr 16 '13 at 15:08
  • Hi, I've tried prettyNum, format and signif but it always comes out 0! – user1357015 Apr 16 '13 at 15:17
  • You can't get a p-value smaller than 2.2e-16. http://stackoverflow.com/questions/6970705/why-cant-i-get-a-p-value-smaller-than-2-2e-16 – Chargaff Apr 16 '13 at 15:31
  • 1
    @Chargaff, have you actually read the answer to the question you linked there? It directly contradicts your statement ... – Ben Bolker Apr 16 '13 at 15:33
  • @BenBolker. Yes your right, I always (wrongly) assumed that 2.2e-16 was the smalest p-val R could give. -1 for not reading entire post... – Chargaff Apr 16 '13 at 15:39
  • http://stackoverflow.com/questions/6704365/pchisq-increase-decimal-accurancy – Ben Bolker Apr 16 '13 at 15:43

1 Answers1

8

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
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • 1
    Hi, perfect, exactly what I was looking for. Thanks also for your comment on p-value. It looks like to calculate a pvalue if my obs is x, I would need dpois(x, lambda) + ppois(x, lambda, lower.tail = FALSE) or equivalent, ppois(x -1 lambda, lower.tail = FALSE). – user1357015 Apr 16 '13 at 19:17