如附图所示,我有两个重叠的密度。我想知道两条线的交汇点。我该怎么做呢?
这是生成图像的代码:
... #reading in files etc.
pdf("test-plot.pdf")
d1 <- density(somedata)
d2 <- density(someotherdata)
plot(d1)
par(col="red")
lines(d2)
dev.off()
原始数据只是两个一维向量,所以我感兴趣的是它们密度的交点。
我尝试使用 here 中显示的解决方案,但不幸的是,它既没有给我一个数字,也没有正确地画线:
最佳答案
# create and plot example data
set.seed(1)
plotrange <- c(-1,8)
d1 <- density(rchisq(1000, df=2), from=plotrange[1], to=plotrange[2])
d2 <- density(rchisq(1000, df=3)-1, from=plotrange[1], to=plotrange[2])
plot(d1)
lines(d2)
# look for points of intersection
poi <- which(diff(d1$y > d2$y) != 0)
# Mark those points with a circle:
points(x=d1$x[poi], y=d1$y[poi], col="red")
# or with lines:
abline(v=d1$x[poi], col="orange", lty=2)
abline(h=d1$y[poi], col="orange", lty=2)
关于r - 在R中找到两个密度的交点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21212352/