2
$\begingroup$

One solution for calculating the overlap coefficient for two density plots was given by @mmk here: How to calculate overlap between empirical probability densities?

Question: However, does anyone know how one would calculate confidence intervals in R for the overlap coefficient, or a measure of error?

I have tried the overlap package - essentially for use with time series data - but I get very different results to @mmk's code when using this package's functions. If one plots the curves, then @mmk's code seems to give a far more accurate estimate of the area of overlap.

Therefore, I would like to carry on using @mmk's code, but I do not know how to take it forward to calculate confidence intervals. How can this be done?

Code: Here is the code from the above link written by @mmk:

# simulate two samples
a <- rnorm(100)
b <- rnorm(100, 2)

# define limits of a common grid, adding a buffer so that tails aren't cut off
lower <- min(c(a, b)) - 1 
upper <- max(c(a, b)) + 1

# generate kernel densities
da <- density(a, from=lower, to=upper)
db <- density(b, from=lower, to=upper)
d <- data.frame(x=da$x, a=da$y, b=db$y)

# calculate intersection densities
d$w <- pmin(d$a, d$b)

# integrate areas under curves
library(sfsmisc)
total <- integrate.xy(d$x, d$a) + integrate.xy(d$x, d$b)
intersection <- integrate.xy(d$x, d$w)

# compute overlap coefficient
overlap <- 2 * intersection / total
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.