[R] How to integrate a function with additional argument being a vector or matrix?

Marius Hofert m_hofert at web.de
Wed Dec 22 13:43:55 CET 2010


Dear expeRts,

I somehow don't see why the following does not work:

integrand <- function(x, vec, mat, val) 1 # dummy return value
A <- matrix(runif(16), ncol = 4)
u <- c(0.4, 0.1, 0.2, 0.3)
integrand(0.3, u, A, 4)
integrate(integrand, lower = 0, upper = 1, vec = u, mat = A, val = 4)

I would like to integrate a function ("integrand") which gets an "x" value (the running variable), a vector ("vec"), a matrix ("mat"), and a value ("val"). This function returns a number (set to 1 here). Of course, pointwise evaluation works without flaws. But why does integration not work? I obtain:

Error in integrate(integrand, lower = 0, upper = 1, vec = u, mat = A,  : 
  evaluation of function gave a result of wrong length

Cheers,

Marius



More information about the R-help mailing list