?expand.grid
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
Kathie <kathryn.lord2000@gmail.com> wrote:
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.
_____________________________________________
R-help@r-project.org mailing list
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]]