问题
I've been struggling to find a solution to find the area of overlap between two curves. I'm not dealing with probability density functions with known parameters but curves obtained from smoothing of empirical data points.
The only hint I found is to calculate the area that is not over-lapping as in this code (from here):
x <- seq(-6,6,by = 0.01)
y1 <- dnorm(x,0,1)
y2 <- pnorm(x,1,1.1)
f1 <- approxfun(x, y1-y2)
f2 <- function(z) abs(f1(z))
dif <- integrate(f2, min(x), max(x))
plot(x,y1,type="l",ylim=c(0,1))
lines(x,y2,type="l",col="red")
polygon(c(x,rev(x)),c(y1,rev(y2)), col="skyblue")
This is essentially the area between curves, but what I need is not the highlighted blue area but the white area in-between. So the area of overlap.
I was reading that one has to find the intersections of the two curves on a mathematician's blog but I cannot find how to do this in R either.
Hopefully, someone can help me out.
Any suggestions are helpful. I apologise in advance though, I'm not an expert in maths.
回答1:
This is the integral of the minimum:
x <- seq(-6,6,by = 0.01)
y1 <- dnorm(x,0,1)
y2 <- pnorm(x,1,1.1)
f <- approxfun(x, pmin(y1,y2))
integrate(f, min(x), max(x))
回答2:
Just do
z <- c(y2[y2 < y1], y1[y1 < y2])
polygon(x, z, col="skyblue")
.
来源:https://stackoverflow.com/questions/60517255/find-area-of-overlap-between-two-curves