水平图上百分位数的轮廓

时间:2013-04-25 17:11:00

标签: r

我们如何估计与轮廓内点的百分比相对应的水平轮廓?

这篇文章描述了如何在R中的散点图中添加轮廓:
https://stats.stackexchange.com/questions/31726/scatterplot-with-contour-heat-overlay

例如:

    x <- rnorm(1000, 0, 1.3)
    y <- rnorm(1000, 0, 1);
    dens <- kde2d(x, y, n=200); ## estimate the z counts
    plot(x, y);
    contour(dens, nlevels = 5, add=T)


因此,不是上面例子中绘制的5个等级,我希望对应于99%,95%,50%等点的等级位于等高线内。



感谢...

1 个答案:

答案 0 :(得分:2)

这样的事情(来自@benbolker的HPDregionplot的片段)?:

library(MASS)
x <- rnorm(1000, 0, 1.3)
y <- rnorm(1000, 0, 1);
dens <- kde2d(x, y, n=200); ## estimate the z counts

prob <- c(.98, .95, .90, .8, .5, .1)
dx <- diff(dens$x[1:2])
dy <- diff(dens$y[1:2])
sz <- sort(dens$z)
c1 <- cumsum(sz) * dx * dy
levels <- sapply(prob, function(x) {
    approx(c1, sz, xout = 1 - x)$y
})

plot(x,y)
contour(dens, levels=levels, labels=prob, add=T)