I was doing some integration into a loop using integrate
and I come up with an error I can\'t understand neither get rid of. Here is a MWE I could extract:
I'd use this work-around:
integrate(f = function(v){pnorm(v, mean = m, sd = s, lower.tail = FALSE)},
max(u_min,m-10*s),min(u_max,m+10*s))$value + (u_min-m+10*s)*(u_min<m+10*s)
What I've done:
pnorm
with lower.tail=FALSE
is basically zero when very far on the right from the mean. So there is no point in "stretching" the right limit of the integral. So, when u_max > m+10*s
, you just integrate to m + 10*s
. You can of course change the 10
factor to add precision;pnorm
is basically always 1; so you can enhance the left limit and the missing part is just u_min - m+10*s
. Same logic as above.The default tolerance of .Machine$double.eps^0.25
(= 0.0001220703) needs to be lowered. Try, for example, this:
f <- function(v) pnorm(v, mean = m, sd = s, lower.tail = FALSE)
integrate(f, u_min, u_max, rel.tol = 1e-15)
## 0.0009421867 with absolute error < 1.1e-17