Double integrate in R

Viewed 83

I would like to ask if any of you guys can help me find how can I compute a double integral of a given joint distribution e.g. dnorm(x,m1,s1)*dnorm(y,m2,s2)*CopulaDensity with Ymin=-inf, Ymax=inf, and Xmin= x and Xmax = inf? I want to do this in R.

Thanks a lot .

Note! CopulaDensity is a numeric vector .

1 Answers

Here is a solution inspired in this post.
But with no CopulaDensity.

f <- function(x, m1, s1, m2, s2) {
  dnorm(x, m1, s1) * integrate(function(y, m2, s2) {
    dnorm(y, m2, s2)
  }, x, Inf, m2 = m2, s2 = s2)$value
}

(res <- integrate(Vectorize(f), -Inf, Inf, m1 = 0, s1 = 1, m2 = 0, s2 = 1)$value)
#[1] 0.5
Related