从 geom_density() 获取计数

Getting counts from geom_density()

我有一系列数字:

tmp<- c(round(seq(0, 12000, ((12000 - 0) / round(1500 * .05)))), 
                 round(seq(12000, 18900, ((18900 - 12000) / round(1500 * .1)))),
                 round(seq(18900, 23300, ((23300 - 18900) / round(1500 * .1)))),
                 round(seq(23300, 28100, ((28100 - 23300) / round(1500 * .1)))),
                 round(seq(28100, 33500, ((33500 - 28100) / round(1500 * .1)))),
                 round(seq(33500, 40000, ((40000 - 33500) / round(1500 * .1)))),
                 round(seq(40000, 47700, ((47700 - 40000) / round(1500 * .1)))),
                 round(seq(47700, 56500, ((56500 - 47700) / round(1500 * .1)))),
                 round(seq(56500, 68300, ((68300 - 56500) / round(1500 * .1)))),
                 round(seq(68300, 94200, ((94200 - 68300) / round(1500 * .1)))),
                 round(seq(94200, 200000, ((200000 - 94200) / round(1500 * .05)))))

现在我可以使用 geom_density 来获取分布的形状。如何根据该密度形状获得两个特定 tmp 值之间的 tmp 数量的近似计数?

例如,我可以根据实际系列计算 tmp 中 10050 到 10100 之间的值的数量。但我想根据平滑直方图(密度)计算值的数量,它不像实际系列那样线性。

不知道我翻译的好不好。以下代码将根据密度估计而不是实际分布计算 'tmp' 中的行数。估计是概率密度估计,所以要乘以:

  • 通过估计每个bin的宽度,得到每个估计点周围的概率值

  • 通过总行数来估计给定范围内的行数(这里的例子,10000到20000不包括)。

'density' 是 'geom_density' 调用的函数,用于获取要绘制的点。

> k <- density(tmp); sum(k$y[which(k$x>10000 & k$x<20000)])*(k$x[2]-k$x[1])*length(tmp)
[1] 199.3722

> length(which(tmp>10000 & tmp<20000))
[1] 202