[R] Odp: Simplify the code
Petr PIKAL
petr.pikal at precheza.cz
Thu Feb 11 15:20:51 CET 2010
Hi
I did not see the answer yet, probably nobody is much interested in such
function, so I will try.
r-help-bounces at r-project.org napsal dne 10.02.2010 21:57:42:
> Hi all,
> I wrote the following code for the function that is attached.
> I want to simply the code.
Why. If it works there is no problem until you encounter performance
issues. If it does not work you shall specify what is wrong with it.
Many functions are vectorised (including choose and beta) so you could
probably get rid of nested cycles but I am far from elaborating how to
change your code and make it simpler (shorter, quicker ???)
Regards
Petr
> 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
> [příloha question to R-heip.pdf odstraněna uživatelem Petr PIKAL/CTCAP]
> ______________________________________________
> R-help at 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.
More information about the R-help
mailing list