[R] Simplify the code

li li hannah.hlx at gmail.com
Wed Feb 10 21:57:42 CET 2010


Hi all,
   I wrote the following code for the function that is attached.
I want to simply the code. Can some one give me some help?


The function is attached and the following is the code:
####################################################################
## f1 is the function calculate the value outside the sum
f1 <- function(t, a, b) {t/beta(a, b)}
## f2 is the function that calculates the summand
f2 <- function(j, k, t, a, b){choose(j-1,k-1)*(1-t)^(j-1) * beta(a+k, b+j-
k)}
## f3 calculates the sum
f3 <- function(n, t, a, b){
ss <- matrix(0, nrow=n, ncol=n)
for (j in 1:n){
    for (i in 1:j) {
            ss[i,j]<- f2(j,i, t, a, b)
                    }
                }
sum <- sum(ss)
return(sum)
}
## f is the final function
f <- function(n,t, a, b) {f1(t,a ,b)*f3(n,t,a,b)}

##########################################################################

   Many thanks!








                             Hannah
-------------- next part --------------
A non-text attachment was scrubbed...
Name: question to R-heip.pdf
Type: application/pdf
Size: 26927 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100210/a3277bd1/attachment.pdf>


More information about the R-help mailing list