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
#----------------------------------------------------------------------------
# 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
#----------------------------------------------------------------------------
# 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)
#----------------------------------------------------------------------------
Run the code above in your browser using DataLab