3

I need help with calculating the Youden index in a two density plot in R. Basically, I need to calculate the value where two density plots are overlapping The variables are numeric.

Is there any method for calculating this value (Youden index) ???

J.R.
  • 33
  • 4

1 Answers1

1

We could expand this solution to get the coordinates (i.e. the y, too) for the intersection point.

Accordingly, first the two distributions are to be combined and indexed and the minimum and maximum calculated to make them comparable.

Second, use density to compute the kernel density estimates for each distribution.

The coordinates we can get with which(diff((d2$y - d1$y) > 0) != 0) + 1 for x and y as shown below.

set.seed(42)
n <- 1e3
dat <- data.frame(v=c(rnorm(n, 1, 3), rnorm(n, 5, 3)),
                  grp=rep(1:2, each=n))

lo <- min(dat$v)
up <- max(dat$v)

d1 <- density(dat$v[dat$grp == 1], from=lo, to=up, n=2^10)
d2 <- density(dat$v[dat$grp == 2], from=lo, to=up, n=2^10)

intersection.point <- cbind(x=d1$x[which(diff((d2$y - d1$y) > 0) != 0) + 1], 
                            y=d1$y[which(diff((d2$y - d1$y) > 0) != 0) + 1])

Plot

plot(d1, ylim=c(0, .14), main="Intersection point")
lines(d2)
points(intersection.point, col="red", pch=19)
legend("topright", pch=19, col="red", legend="intersection point")

enter image description here

jay.sf
  • 60,139
  • 8
  • 53
  • 110