[R] factorials

Damien Joly do_joly at yahoo.ca
Wed Jan 16 03:36:55 CET 2002


I'm a total newbie at using R, and so there probably
is a better way to do this.  However, I couldn't find
one, and so maybe this will help someone.

I was calculating log-likelihoods using a multinomial
model, and found that for large n, prod(n:1) wouldn't
work to calculate factorials (e.g., prod(200:1) =
Inf).  The below function calculates the natural log
of a factorial (e.g. factorial(100) returns ln(100!)
or in other words 100! = exp(factorial(100)).  

factorial<-function(p) {P<-array(c(p:1),dim=c(p,1))
P<-log(P)
return(sum(P))}

Damien 

__________________________________________________


http://promo.yahoo.com/videomail/
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list