[R] factorials

About this list Date view Thread view Subject view Author view Attachment view

From: Damien Joly (do_joly@yahoo.ca)
Date: Wed 16 Jan 2002 - 13:36:55 EST


Message-id: <20020116023655.53687.qmail@web14308.mail.yahoo.com>

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@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Thu 17 Jan 2002 - 11:10:11 EST