【问题标题】:How to obtain the density estimation of a specific value in stats::density?如何获得 stats::density 中特定值的密度估计?
【发布时间】:2015-01-29 11:53:50
【问题描述】:

假设我有如下数据:

val <- .65
set.seed(1)
distr <- replicate(1000, jitter(.5, amount = .2))
d <- density(distr)

由于stats::density 使用特定的bw,它不包括区间中的所有可能值(因为它们是无限的):

d$x[ d$x > .64 & d$x < .66 ]

[1] 0.6400439 0.6411318 0.6422197 0.6433076 0.6443955 0.6454834 0.6465713 0.6476592 0.6487471
[10] 0.6498350 0.6509229 0.6520108 0.6530987 0.6541866 0.6552745 0.6563624 0.6574503 0.6585382
[19] 0.6596261

我想找到一种方法将val 提供给密度函数,以便它返回其d$y 估计值(然后我将使用它为密度图的区域着色)。

我猜不出这个问题有多傻,但我找不到快速的解决方案。

我想通过d$y 的线性插值来获得它,这两个值对应于更接近vald$x。有更快的方法吗?

【问题讨论】:

    标签: r density-plot


    【解决方案1】:

    这说明了approxfun的用法:

    > Af <- approxfun(d$x, d$y)
    > Af(val)
    [1] 2.348879
    > plot(d(
    + 
    > plot(d)
    > points(val,Af(val) )
    > png();plot(d); points(val,Af(val) ); dev.off()
    

    【讨论】:

    • 通过使用线性插值,我得到了很好的结果,但是approxfun 做得很好;)谢谢!
    • approxfun 使用线性插值。
    猜你喜欢
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 2013-01-11
    • 2013-04-21
    • 2017-02-03
    • 2017-05-09
    相关资源
    最近更新 更多