考虑在Gamma分布后我有一百万个观察参数(3,5)。我能够使用summary()
找到分位数,但我试图找出每条红线之间有多少观察值被分成10个?
a = rgamma(1e6, shape = 3, rate = 5)
summary(a)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0053 0.3455 0.5351 0.6002 0.7845 4.4458
我们可以使用cut
和table
:
table(cut(a, quantile(a, 0:10 / 10)))
# (0.00202,0.22] (0.22,0.307] (0.307,0.382] (0.382,0.457] (0.457,0.535] (0.535,0.622]
# 99999 100000 100000 100000 100000 100000
# (0.622,0.724] (0.724,0.856] (0.856,1.07] (1.07,3.81]
# 100000 100000 100000 100000
但鉴于分位数是多少,这可能不是很有趣。也许您可能也想尝试理论分位数:
table(cut(a, qgamma(0:10 / 10, 3, 5)))
# (0,0.22] (0.22,0.307] (0.307,0.383] (0.383,0.457] (0.457,0.535] (0.535,0.621] (0.621,0.723]
# 99978 100114 100545 99843 99273 99644 100104
# (0.723,0.856] (0.856,1.06] (1.06,Inf]
# 100208 99883 100408
更有意思的是,如果您的数据确实遵循了伽玛分布并且您有大量的观察结果,那么您可以确定在q和(q + x)之间将有接近x%的数据 - 理论分位数。在较小的样本中,第二种方法可能很有趣。
编辑:鉴于您更新的问题,很明显,10%,20%,你不是指分位数。假设最小值为0且最大值为2,如果为10%,则考虑为0.2,那么您需要
table(cut(a, seq(min(a), max(a), length = 10 + 1)))
# (0.00418,0.428] (0.428,0.853] (0.853,1.28] (1.28,1.7] (1.7,2.13] (2.13,2.55]
# 361734 436176 155332 37489 7651 1335
# (2.55,2.97] (2.97,3.4] (3.4,3.82] (3.82,4.25]
# 231 38 11 2