[R] how to keep very small or large number?
Spencer Graves
spencer.graves at pdf.com
Fri Sep 23 03:59:03 CEST 2005
Your code would not run for me, because "m" was not defined.
When I let m=n=1000 and start your script with set.seed(1), set.seed(2),
set.seed(3), and set.seed(4), I got 1.911056e-169, 1.264587e-162,
2.225186e-176, and 9.289588e-175, respectively. However, I'm quibbling:
With m=n=10000 and set.seed(4), I got 0.
This problem is easily solved in R using the "log.p" and
"lower.tail" arguments of "plogis". The vector arithmetic also helps
make things much easier to code if you can think vectors, in my opinion.
Consider the following:
z <- pta0+pta1*x1+pta2*x2
ln.p <- plogis(z, log.p=TRUE, lower.tail=as.logical(y))
ln.p. <- sum(ln.p)
log10.p. <- ln.p./log(10)
printSmall <- function(ln.x){
l10x <- ln.x/log(10)
paste(10^(l10x%%1), "e", l10x%/%1, sep="")
}
printSmall(ln.p.)
"6.43113656279925e-5355"
How's this?
spencer graves
Cunningham Kerry wrote:
> Suppose I have n=1000 cases. For each case, the
> probability can go like 1e-10, then I have to multiply
> these probabilities together to get joint probability.
>
> I tried to do the following:
>
> p=1
>
> x1=runif(n,0,1)
> x2=runif(n,0,1)
> y=ifelse(runif(m) < plogis(-6+6*x1+6*x2), 1, 0);
>
> pta0=-6
> pta1=6
> pta2=6
>
> for(i in 1:n)
> {
> p <-
> p*exp((y[i]*log(plogis(pta0+pta1*x1[i]+pta2*x2[i]))+(1-y[i])*log(1-plogis(pta0+pta1*x1[i]+pta2*x2[i]))))
> }
>
> but the result is shown to be zero.
>
> --- Cunningham Kerry <kerryrekky at yahoo.com> wrote:
>
>
>>When I was computing some joint probabilities, I
>>found
>>that R reported most of the results to to -Inf and
>>thus didn't record the value. I guess it is b/c the
>>joint log(probability) can be extremely small. Is
>>there a way in R to keep the values even if they are
>>small?
>>
>>
>>
>>
>>__________________________________
>
>
>>http://mail.yahoo.com
>>
>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
--
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA
spencer.graves at pdf.com
www.pdf.com <http://www.pdf.com>
Tel: 408-938-4420
Fax: 408-280-7915
More information about the R-help
mailing list