[R] R truncating decimal places

William Dunlap wdunlap at tibco.com
Mon May 22 23:15:56 CEST 2017


Do not compute the log likelihood as the log of the product of
probabilities.  Instead compute it as the sum of logs of probabilities.
The latter is less likely to underflow (go below c. 0^-309).

Most (all?) of the built-in probability density functions have a 'log'
argument; when log=TRUE you get the log of the density.

E.g.,

> x <- 1:100
> log(prod(dnorm( (x-50)/2 )))
[1] -Inf
> sum(dnorm( (x-50)/2, log=TRUE ))
[1] -10510.64
>
> log(prod(dnorm( (x-50)/20 )))
[1] -196.0814
> sum(dnorm( (x-50)/20, log=TRUE ))
[1] -196.0814



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, May 22, 2017 at 12:10 PM, Andrea Goijman <andrea.goijman at gmail.com>
wrote:

> Hello list,
>
> I' trying to estimate a log likelihood function from my data.
> I apply the mean to all my simulations, and I get something like this:
>
> apply(likelihood, c(2, 3, 4), mean,na.rm=TRUE)
> , , 1
>
>              [,1]       [,2]       [,3]      [,4]      [,5]        [,6]
>  [,7]
>   [1,] 0.73162327 0.81197093 0.58216435 0.7295733 0.8930731 0.971775402
> 0.8882391
>   [2,] 0.73162327 0.81197093 0.00000000 0.7295733 0.8930731 0.971775402
> 0.8882391
>
> If you note, there is a 0.00000000 value, and it seems that R is truncating
> it as a zero, because when I apply the log
>
> apply(likelihood, c(2, 3, 4), mean,na.rm=TRUE)
>
> I get a "-inf" at that position...
>
>
> How do I avoid R truncating this value?
>
> Thanks!
>
> Andrea
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list