Calculate definite integral in R bunch of error

269 Views Asked by At

I am trying to calculate the following integral, i.e there is an integral inside the integral.

integrate(function(v){
  integrate(function(s){
    pnorm(qnorm(v)-qnorm(s))
  },0,1)$value
},0,1)

Unfortunately I all the time get either:

Error in integrate(function(v) { : 
  evaluation of function gave a result of wrong length

or:

 Error in integrate(function(s) { : maximum number of subdivisions reached 
1

There are 1 best solutions below

0
On BEST ANSWER

The error because integrate needs a vectorized function. A workaround is to use Vectorize around the upper function.

integrate(Vectorize(function(v){
  integrate(function(s){
    pnorm(qnorm(v)-qnorm(s))
  },0,1)$value
}),0,1)

## 0.5 with absolute error < 5.6e-15