[R] problem with function involving ifelse and integrate

Jan.Schelling@hydro.com Jan.Schelling at hydro.com
Sun Apr 16 16:39:19 CEST 2000



The error is in the use of 'ifelse'


Rhelp: 'ifelse' returns a value with the same shape as `test' (...)

whereas 'integrate' returns a list of five elements.

If all you're interested in is the value returned by 'integrate', you can use:

inbeta <- function(x) ifelse(x==0, 0, integrate(fm, 0, x,maxpts = NULL,
eps=0.01)$value)

Be careful, you can only use this for vectors of length 1. If you want the
integral for more than one value you can use 'sapply' as in:

plot(x, sapply(x, inbeta))

Jan


-------------------------
Jan Schelling
Norsk Hydro
Research Centre
N - 3907 Porsgrunn
NORWAY



From: faheem at email.unc.edu on 2000-04-15 20:03 GMT



To:   r-help at stat.math.ethz.ch
cc:    (bcc: Jan Schelling/HRE/Hydro)

Subject:  [R] problem with function involving ifelse and integrate



Dear R people,

There is probably a simple explanation for the following, but I have been
unable to come up with one.

I want to integrate x(1-x)^{-1/3} over intervals of the form [0,a] where a
is between 0 and 1.

Hence, consider:

fm <- function(x) ifelse(x==0 | x==1 ,0,(x*(1-x))^(-1/3))

inbeta <- function(x)
{
 ifelse(x==0,0,integrate(fm,0,x,maxpts = NULL, eps=0.01))

}

Comments:

integrate(fm,0,x,maxpts = NULL, eps=0.01) works ok for everything I tried
it for except x = 0 for some reason, hence the ifelse above.

The problem is with inbeta. Trying inbeta(x) for any value of x other than
0 gives the error "Error in rep(no, length = length(ans)) : Unimplemented
feature in rep" For 0, of course, it returns the value 0.

I can't see anything obviously wrong with this. Can someone help to figure
it out?

Also, I was wondering what method of numerical quadrature is used for
integrate. The documentation is silent about this. Also, the error
messages for

integrate(fm,0,0,maxpts = NULL, eps=0.01)

refers to something called lenwrk. What is this?

                                      Sincerely, Faheem Mitha.


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._






-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list