[R] Greater than 1 or less than 1?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon Jun 21 01:13:07 CEST 2004


"Zhen Chen" <zchen at cceb.upenn.edu> writes:

> I have problem evaluating the expression h = exp(x)/(exp(exp(x))-1)
> for large negative x. This expression is actually the probability
> that y = 1 when y is a Poisson random variable truncated at 0, hence
> must satisfy 0 <= h <= 1. However, when
> x < -18, I may get an h value that is  larger than 1 while the true
> value should be a quantity that is smaller than but very close to 1.
> For example when x = -19, h = 1.00000000031174. I tried to use
> different form of h and none of
> them give me an h value that is less than or equal to 1. I also tried
> to find patterns, but discovered none: for some x < -18, h is below 1;
> for others
> h is greater than 1. Is there any trick that enables me to obtain
> theoretically correct results from this expression?

expm1() should help...

What platform is this? I don't see it until x=-30:

> exp(-30)
[1] 9.357623e-14
> exp(exp(-30))-1
[1] 9.348078e-14

which is of course "wrong" since exp(x) > x + 1 mathematically.
However, notice that in double precision calculations the latter value
is only accurate up to 1e-15 or so. 

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list