I have defined a function to compute the value of a beta distribution of the second kind (the existing beta distribution of th stats package is the beta distribution of the first kind). It works perfectly for a single value, but I want to apply it to a vector of 22 000 values. I can use a loop for the calculation of each value but it runs very very slowly. So, what can I change ? Hers's the function : p <- c(1,1) y <- 1 z <- 1 truc <- function(y) {y^(p[1]-1)/(1+y)^(p[1]+p[2])} pbeta2 <- function(z,p) 1/beta(p[1],p[2])*integrate(truc,0,z)$value machin <- pbeta2(vector,p) just return a single value Thanks for your help
Florent have a look at:> help(sapply)On Mon, 31 Oct 2005, Florent Bresson wrote:> I have defined a function to compute the value of a > beta distribution of the second kind (the existing > beta distribution of th stats package is the beta > distribution of the first kind). It works perfectly > for a single value, but I want to apply it to a vector > of 22 000 values. I can use a loop for the calculation > of each value but it runs very very slowly. > So, what can I change ? > > Hers's the function : > p <- c(1,1) > y <- 1 > z <- 1 > truc <- function(y) {y^(p[1]-1)/(1+y)^(p[1]+p[2])} > pbeta2 <- function(z,p) > 1/beta(p[1],p[2])*integrate(truc,0,z)$value > > machin <- pbeta2(vector,p) just return a single value > > Thanks for your help > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >Paulo Justiniano Ribeiro Jr LEG (Laborat?rio de Estat?stica e Geoinforma??o) Departamento de Estat?stica Universidade Federal do Paran? Caixa Postal 19.081 CEP 81.531-990 Curitiba, PR - Brasil Tel: (+55) 41 3361 3573 Fax: (+55) 41 3361 3141 e-mail: paulojus at est.ufpr.br http://www.est.ufpr.br/~paulojus
I just have a try with sapply. The problem is that my function pbeta2 has two parameters z and p (wich is a vector of two parameters). If I use sapply , R returns a message incicating that parameter p is missing. It is a problem since both z and p are varying along my data.frame.>Florent >have a look at: > help(sapply)>On Mon, 31 Oct 2005, Florent Bresson wrote:>> beta distribution of the second kind (the existing >> beta distribution of th stats package is the beta >> distribution of the first kind). It works perfectly >> for a single value, but I want to apply it to avector>> of 22 000 values. I can use a loop for thecalculation>> of each value but it runs very very slowly. >> So, what can I change ? >> >> Here's the function : >> p <- c(1,1) >> y <- 1 >> z <- 1 >> truc <- function(y){y^(p[1]-1)/(1+y)^(p[1]+p[2])}>> pbeta2 <- function(z,p) >> 1/beta(p[1],p[2])*integrate(truc,0,z)$value >> >> machin <- pbeta2(vector,p) just return a singlevalue>> >> Thanks for your help >> >> ______________________________________________ >> R-help at stat.math.ethz.ch mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html>> >>>Paulo Justiniano Ribeiro Jr >LEG (Laborat??rio de Estat??stica e Geoinforma????o) >Departamento de Estat??stica >Universidade Federal do Paran?? >Caixa Postal 19.081 >CEP 81.531-990 >Curitiba, PR - Brasil >Tel: (+55) 41 3361 3573 >Fax: (+55) 41 3361 3141 >e-mail: paulojus at est.ufpr.br >http://www.est.ufpr.br/~paulojus
You should then try apply. See ?apply. For instance: a <- matrix(c(1:10), 5, 2) apply(a, 1, function(x) x[1] + 100 * x[2]) This way you can disaggregate the components of your input (that is not a singleton) and use them inside your function separately. Carlos J. Gil Bellosta http://www.datanalytics.com Quoting Florent Bresson <f_bresson at yahoo.fr>:> I just have a try with sapply. The problem is that my > function pbeta2 has two parameters z and p (wich is a > vector of two parameters). If I use sapply , R returns > a message incicating that parameter p is missing. It > is a problem since both z and p are varying along my > data.frame.