

integrate(integrate)                         R Documentation

_A_d_a_p_t_i_v_e _I_n_t_e_g_r_a_t_i_o_n _i_n _1_-_-_2_0 _D_i_m_e_n_s_i_o_n_s_.

_D_e_s_c_r_i_p_t_i_o_n_:

     `integrate()' integrates a function of 1 variable over
     a specified interval, i.e. it computes

                  integral[l .. u] functn(t) dt

     where l =`lower', u =`upper'.

     `adapt()' allows 1 to 20 variables and integrates over
     a rectangular box.

_U_s_a_g_e_:

     adapt(ndim, lower, upper, minpts, maxpts, functn, eps,...)
     integrate(functn, lower,upper,minpts=100,maxpts=500,eps=0.01,...)

_A_r_g_u_m_e_n_t_s_:

    ndim: the number of dimensions of the function/integral

   lower: vector of at least length ndim of the lower bounds
          on the integral

   upper: vector of at least length ndim of the upper bounds
          on the integral

  minpts: the minimum number of function evaluations

  maxpts: the maximum number of function evaluations or NULL

  functn: the name of an R function.  `functn' should take a
          single vector argument and possibly some parame-
          ters and return the function value at that point.
          `functn' must return a single numeric value.

     eps: The desired accuracy for the relative error.

     ...: Other parameters to be passed to `functn'

_V_a_l_u_e_:

     A list of `class "integrate"' with components

   value: the estimated integral

  relerr: the estimated relative error; `< eps' argument if

  minpts: the actual number of function evaluations

   ifail: an error indicator.  If ifail is not equal to 0,
          the function warns the user of the error condi-
          tion.

_N_o_t_e_:

     This is modified from Mike Meyer's S code. The func-
     tions just call A.C. Genz's fortran ADAPT subroutine to
     do all of the calculations.  A work array is allocated
     within the C/Fortran code.

     The Fortran function has been modified to use double
     precision, for compatibility with R. It only works in
     two or more dimensions; for one-dimensional integrals
     we integrate over a strip of unit width.

     Setting maxpts to NULL asks the function to keep dou-
     bling maxpts until the desired precision is achieved or
     R runs out of room.

_E_x_a_m_p_l_e_s_:

     integrate(dnorm,-1.96,1.96)
     ##-        value       relerr       minpts       lenwrk        ifail
     ##-    0.9500042 0.0003456134          125           73            0

     normloc <- function(x,mu) dnorm(x-mu)
     integrate(normloc,-1.96,1.96, mu=0) # passing 'mu' arg. to  normloc()
     integrate(normloc,-2.96,0.96, mu=-1)

     ## Example of a three dimensional spherical normal distribution:
     ir2pi <- 1/sqrt(2*pi)
     fred <- function(z) { ir2pi^length(z) * exp(-0.5 * sum(z * z))}
     adapt(3, c(-2,-2,-2),c(2,2,2),100,500,fred,.01)
     ##-        value       relerr       minpts       lenwrk        ifail
     ##-    0.8691984 2.579652e-05          345           54            0
     ##
     ## i.e., it took 345 function evaluations to find the integral.
           adapt(3, c(-2,-2,-2),c(2,2,2),100,NULL,fred,.00001)
     ##       value       relerr       minpts       lenwrk        ifail
     ##   0.8696159 1.902895e-06         5073          804            0
     ##  ie 5073 points needed (4 doublings).

