This is a wrapper routine for integration in maxlogL
framework. It is
used in integration for compute detectability density functions and in
computation of mean values, but it is also a general purpose integrator.
Arguments
- fun
an R function which should take a numeric argument x and possibly some parameters. The function returns a numerical vector value for the given argument
x
.- lower
a numeric value for the lower limit of the integral.
- upper
a numeric value for the upper limit of the integral.
- routine
a character specifying the integration routine.
integrate
andgauss_quad
are available, but the custom routines can be defined.- ...
additional arguments to be passed to
fun
and to the integration routine specified inroutine
argument.
Details
The user can create custom integration routines through implementation of a wrapper function using three arguments
fun
: a function which should take a numeric argument x and possibly some parameters.lower
: a numeric value for the lower limit of the integral.upper
: a numeric value for the upper limit of the integral....
> furthermore, the user must define additional arguments to be passed tofun
function.
The output must be a numeric atomic value with the result of the integral.
Author
Jaime Mosquera Gutiérrez, jmosquerag@unal.edu.co
Examples
library(EstimationTools)
#----------------------------------------------------------------------------
# Example 1: Mean of X ~ N(2,1) using 'integrate'.
mynorm <- function(x, mu, sigma) x*dnorm(x, mean = mu, sd = sigma)
i1 <- integration(mynorm, lower = -Inf, upper = Inf, mu = 2, sigma = 1)
i1
#> [1] 2
#----------------------------------------------------------------------------
# Example 2: Mean of X ~ N(2,1) using 'gauss_quad' (Gauss-Hermitie
# quadrature).
g <- function(x, mu, sigma) sqrt(2)*sigma*x + mu
i2 <- integration(g, lower = -Inf, upper = Inf, routine = 'gauss_quad',
kind = 'hermite.h', normalized = FALSE,
mu = 2, sigma = 1)
i2 <- i2/sqrt(pi)
i2
#> [1] 2
#----------------------------------------------------------------------------
# Example 3: replicating integrate
i3 <- integrate(dnorm, lower=-1.96, upper=1.96)
i4 <- integration(dnorm, lower=-1.96, upper=1.96)
identical(i3$value, i4)
#> [1] TRUE
#----------------------------------------------------------------------------