[R] Different values for double integral

Andreas Wittmann andreas_wittmann at gmx.de
Sat Jan 24 11:23:29 CET 2009


Dear R useRs,

i have the function f1(x, y, z) which i want to integrate for x and y. 
On the one hand i do this by first integrating for x and then for y, on 
the other hand i do this the other way round and i wondering why i 
doesn't get the same result each way?

z <- c(80, 20, 40, 30)

"f1" <- function(x, y, z) {dgamma(cumsum(z)[-length(z)], shape=x, rate=y)}

"g1" <- function(y, z) {integrate(function(x) {f1(x=x, y=y, z=z)}, 0.1, 
0.5)$value}
"g2" <- function(x, z) {integrate(function(y) {f1(x=x, y=y, z=z)}, 0.1, 
0.5)$value}

integrate(Vectorize(function(y) {g1(y=y, z=z)}), 0.1, 0.5)$value
[1] 5.909943e-09
integrate(Vectorize(function(x) {g2(x=x, z=z)}), 0.1, 0.5)$value
[1] 5.978334e-09

If you have any advice what is wrong here, i would be very thankful.


best regards
Andreas




More information about the R-help mailing list