I am using the R programming language. I am trying to evaluate the (multivariable) integral (through numerical integration) of the following function ("fitness") I defined :
fitness <- function(mu1, mu2, sigma1, sigma2, sigma12) {
rho = sigma12/(sigma1*sigma2)
a = -1/(2 * (1 - ((sigma12 / (rho))^2))) * ((0.4395244 - mu1)/ sigma1)^2 - 2*(sigma12 / (sigma1 * sigma2)) *((0.4395244 - mu1)/sigma1) * ((3.7150650 - mu2)/sigma2) + ((3.7150650 - mu2)/sigma2)^2
b = -1/(2 * (1 - ((sigma12 / (rho))^2))) * ((0.7698225 - mu1)/ sigma1)^2 - 2*(sigma12 / (sigma1 * sigma2)) *((0.7698225 - mu1)/sigma1) * ((2.4609162 - mu2)/sigma2) + ((2.4609162 - mu2)/sigma2)^2
c = -1/(2 * (1 - ((sigma12 / (rho))^2))) * ((2.5587083 - mu1)/ sigma1)^2 - 2*(sigma12 / (sigma1 * sigma2)) *((2.5587083 - mu1)/sigma1) * ((0.7349388 - mu2)/sigma2) + ((0.7349388 - mu2)/sigma2)^2
d = -1/(2 * (1 - ((sigma12 / (rho))^2))) * ((1.0705084 - mu1)/ sigma1)^2 - 2*(sigma12 / (sigma1 * sigma2)) *((1.0705084 - mu1)/sigma1) * ((1.3131471 - mu2)/sigma2) + ((1.3131471 - mu2)/sigma2)^2
e = -1/(2 * (1 - ((sigma12 / (rho))^2))) * ((1.1292877 - mu1)/ sigma1)^2 - 2*(sigma12 / (sigma1 * sigma2)) *((1.1292877 - mu1)/sigma1) * ((1.5543380 - mu2)/sigma2) + ((1.5543380 - mu2)/sigma2)^2
answer = answer = 1/ ((2*pi*sigma1*sigma2*sqrt((1 - rho^2)) / ((sigma1 * sigma2) * exp( a * b * c * d * e))))
}
I tried to integrate this function using the "integrate" function in R:
integrate(fitness, lower = c(-5, -5, -5, -5, -5), upper = c(5, 5, 5, 5, 5))
But I got the following error:
Error in integrate(fitness, lower = c(-5, -5, -5, -5, -5), upper = c(5, :
length(lower) == 1 is not TRUE
Looking at the documentation for the "integrate" function (integrate function - RDocumentation), it seems like the "integrate" function is only designed to evaluate the integral single variable function.
What I tried so far: Looking at similar questions (e.g. numerical integration of multivariable functions), I saw that sometimes evaluating multivariable integrals requires you to "vectorize" your function:
Nd_vectorized_for <- function(s) {
result <- numeric(length(s))
for (i in 1:length(s)) {
result[i] <- fitness(s[i])
}
result ## or `return(result)`
}
I tried again to evaluate the integral of this function:
Nd_vectorized_for( c(-5, -5, -5, -5, -5) :c(5, 5, 5, 5, 5) )
But it still returns an error:
Error in fitness(s[i]) : argument "sigma12" is missing, with no default
Can someone please show me how to fix this error?
Thanks!