R: Safe Integration of One-Dimensional Functions

safeIntegrate {DistributionUtils} | R Documentation

## Safe Integration of One-Dimensional Functions

### Description

Adaptive quadrature of functions of one variable over a finite or infinite interval.

### Usage

    safeIntegrate(f, lower, upper, subdivisions=100,
                  rel.tol = .Machine$double.eps^0.25, abs.tol = rel.tol,
                  stop.on.error = TRUE, keep.xy = FALSE, aux = NULL, ...)

### Arguments

`f` | An R function taking a numeric first argument and returning a numeric vector of the same length. Returning a non-finite element will generate an error.
`lower, upper` | The limits of integration. Can be infinite.
`subdivisions` | The maximum number of subintervals.
`rel.tol` | Relative accuracy requested.
`abs.tol` | Absolute accuracy requested.
`stop.on.error` | Logical. If true (the default) an error stops the function. If false some errors will give a result with a warning in the `message` component.
`keep.xy` | Unused. For compatibility with S.
`aux` | Unused. For compatibility with S.
`...` | Additional arguments to be passed to `f`. Remember to use argument names not matching those of `safeIntegrate(.)`!

### Details

This function is just a wrapper around `integrate` to check for equality of `upper` and `lower`. A check is made using `all.equal`. When numerical equality is detected, if `lower` (and hence `upper`) is infinite, the value of the integral and the absolute error are both set to 0. When `lower` is finite, the value of the integral is set to 0, and the absolute error to the average of the function values at `upper` and `lower` times the difference between `upper` and `lower`.

When `upper` and `lower` are determined to be different, the result is exactly as given by `integrate`.

### Value

A list of class `"integrate"` with components:

`value` | The final estimate of the integral.
`abs.error` | Estimate of the modulus of the absolute error.
`subdivisions` | The number of subintervals produced in the subdivision process.
`message` | `"OK"` or a character string giving the error message.
`call` | The matched call.

### See Also

The function `integrate` and `all.equal`.

### Examples

    integrate(dnorm, -1.96, 1.96)
    safeIntegrate(dnorm, -1.96, 1.96)  # Same as for integrate()
    integrate(dnorm, -Inf, Inf)
    safeIntegrate(dnorm, -Inf, Inf)    # Same as for integrate()
    integrate(dnorm, 1.96, 1.96)       # OK here but can give an error
    safeIntegrate(dnorm, 1.96, 1.96)
    integrate(dnorm, -Inf, -Inf)
    safeIntegrate(dnorm, -Inf, -Inf)   # Avoids nonsense answer
    integrate(dnorm, Inf, Inf)
    safeIntegrate(dnorm, Inf, Inf)     # Avoids nonsense answer

[Package DistributionUtils version 0.6-0 Index]