我需要帮助计算R中两个密度图中的Youden指数,基本上,我需要计算两个密度图重叠的值,变量是数字的。
有没有任何方法来计算这个值(Youden指数)?
发布于 2019-06-15 10:29:15
我们可以展开this solution,得到交点的坐标(也就是y
)。
因此,首先要将这两个发行版合并和编制索引,并计算出最小和最大值,以使它们具有可比性。
其次,使用density
计算每个分布的核密度估计值。
我们可以通过which(diff((d2$y - d1$y) > 0) != 0) + 1
获得x
和y
的坐标,如下所示。
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(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")
https://stackoverflow.com/questions/56612670
复制