我的理解是,在计算R中的分位数时,扫描整个数据集并确定每个分位数的值。
如果你要求.8,例如它会给你一个在该分位数处出现的值。即使不存在这样的值,R仍然会给出在该分位数处发生的值。它通过线性插值来实现。
但是,如果有人希望计算分位数,然后继续向上/向下舍入到最接近的实际值,该怎么办?
例如,如果.80处的分位数值为53,那么当真实数据集只有50和54时,那么如何让R列出这些值中的任何一个?
答案 0 :(得分:5)
试试这个:
#dummy data
x <- c(1,1,1,1,10,20,30,30,40,50,55,70,80)
#get quantile at 0.8
q <- quantile(x, 0.8)
q
# 80%
# 53
#closest match - "round up"
min(x[ x >= q ])
#[1] 55
#closest match - "round down"
max(x[ x <= q ])
#[1] 50
答案 1 :(得分:3)
在R quantile
函数中实现了许多估算方法。您可以根据https://stat.ethz.ch/R-manual/R-devel/library/stats/html/quantile.html中记录的type
参数选择要使用的类型。
x <- c(1, 1, 1, 1, 10, 20, 30, 30, 40, 50, 55, 70, 80)
quantile(x, c(.8)) # default, type = 7
# 80%
# 53
quantile(x, c(.8), FALSE, TRUE, 7) # equivalent to the previous invocation
# 80%
# 53
quantile(x, c(.8), FALSE, TRUE, 3) # type = 3, nearest sample
# 80%
# 50