[R] help with adapt function

Thomas Lumley tlumley at u.washington.edu
Sun Jul 14 04:21:22 CEST 2002


On Sat, 13 Jul 2002, Faheem Mitha wrote:

>
> Dear People,
>
> I'm trying to use the function adapt, from the adapt library package,
> which does multidimensional numerical integration. I think I must be using
> the wrong syntax or something, because even a simple example does not
> work. Consider
>
>  foo <- function(x){x[1]*x[2]}
>
> and
>
> adapt(2, lo = c(-1,-1), up = c(1,1), functn = foo)
>
> This simply hangs. A more complicated example crashes R, but never
> mind that for the moment. Perhaps some kind person could explain what
> I am doing wrong here?


You're trying to compute an integral that's exactly zero and not
specifying a maximum number of evaluations. The Fortran code
in adapt() uses an estimate of the relative error in deciding if the
answer is accurate, and if you don't specify the maxpts argument it will
keep increasing the number of evaluations until the relative error
criterion is satisfied.

If the true integral is zero the relative error will never be less than
the specified level and the function will never stop.  With your example I
get
>  adapt(2, lo = c(-1,-1), up = c(1,1), functn = foo,maxpts=10000)
                                                    value
                                             8.673617e-19
                                                   relerr
                                                        1
                                                   minpts
                                                     9999
                                                   lenwrk
                                                     1403
                                                    ifail
                                                        1
                                                     warn
Ifail=1, maxpts was too small. Check the returned relerr!
Warning message:
Ifail=1, maxpts was too small. Check the returned relerr! in: adapt(2, lo
= c(-1, -1), up = c(1, 1), functn = foo, maxpts = 10000)

or integrating over just one quadrant so the answer isn't zero

>  adapt(2, lo = c(-1,-1), up = c(0,0), functn = foo)
      value      relerr      minpts      lenwrk       ifail
       0.25 7.38275e-08         165          73           0

or changing the function slightly
> bar<-function(x) {foo(x)+1e-4}
>  adapt(2, lo = c(-1,-1), up = c(1,1), functn = bar)
      value      relerr      minpts      lenwrk       ifail
      4e-04 9.23213e-05         165          73           0


	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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