[R] My R code is not efficient

Kathie kathryn.lord2000 at gmail.com
Tue Aug 2 17:37:01 CEST 2011


Dear R users,

I have two n*1 integer vectors, y1 and y2, where n is very very large.

I'd like to compute


elbp = 4^(y1) * 5^(y2) * sum_{i=0}^{max(y1, y2)}  [{ (y1-i)! * (i)! *
(y2-i)! }^(-1)];


that is, I need to compute "elbp" for each (y1, y2) pair.

So I made R code like below, but I don't think it's efficient

Would you plz tell me how to avoid this "for" loop blow??



----------------------------------------------------------------------------------------------
        for (k in 1:n){
                ymax <- max( y1[k], y2[k] )
               
                i <- 0:ymax

                sums<- -lgamma(y1[k]-i+1)-lgamma(i+1)-lgamma(y2[k]-i+1)

                maxsums <- max(sums)

                sums <- sums - maxsums

                lsum <- log( sum(exp(sums)) ) + maxsums

                lbp[k] <- y1[k]*log(4)  + y2[k]*log(5)  + lsum
            	
        }
        elbp <- exp(lbp)

------------------------------------------------------------------------------------

Any suggestion will be greatly appreciated.

Regards,

Kathryn Lord 

--
View this message in context: http://r.789695.n4.nabble.com/My-R-code-is-not-efficient-tp3712762p3712762.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list