Skip to content

Applying a function to a vector

5 messages · Thomas Lumley, Carlos J. Gil Bellosta, Brian Ripley +1 more

#
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.
vector
calculation
{y^(p[1]-1)/(1+y)^(p[1]+p[2])}
value
http://www.R-project.org/posting-guide.html
#
On Mon, 31 Oct 2005, Florent Bresson wrote:

            
You would need mapply().  It won't help, though.  Most of the time is 
presumably being spent in all the calls to integrate, so mapply() or 
sapply() won't be much faster than a loop.


According to Google, the VGAM R package 
http://www.stat.auckland.ac.nz/~yee/
claims to have the beta distributions of the second kind.


 	-thomas
Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle
#
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>:
#
On Mon, 31 Oct 2005, Thomas Lumley wrote:

            
It has ML fitting for such distributions.

I think a reasonable answer is to make a function to approximate the 
indefinite integral via linear or spline interpolation.  Then you can 
replace 22,000 numerical integrations by interpolation from a few hundred 
values (at most).

  
    
#
It works really faster with mapply. I just had to
change my fonction from pbeta2(z,p) with p=c(p1,p2) to
pbeta2(z,p1,p2). Thanks for the tip.

--- Thomas Lumley <tlumley at u.washington.edu> a ??crit :
Seattle