在没有插值的情况下计算R中的分位数 - 向上或向下舍入到实际值

jsu*_*prr 4 r quantile linear-interpolation

我的理解是,当计算R中的分位数时,扫描整个数据集并确定每个分位数的值.

如果你要求.8,例如它会给你一个在该分位数处出现的值.即使不存在这样的值,R仍将为您提供在该分位数处发生的值.它通过线性插值来实现.

但是,如果想要计算分位数然后向上/向下舍入到最接近的实际值,该怎么办?

例如,如果.80处的分位数值为53,那么当真实数据集只有50和54时,那么如何让R列出这些值中的任何一个?

zx8*_*754 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
Run Code Online (Sandbox Code Playgroud)


Tom*_*art 5

R函数中实现了多种估计方法quantile。您可以选择与参数一起使用的类型,如https://stat.ethz.ch/R-manual/R-devel/library/stats/html/quantile.htmltype中所述。

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
Run Code Online (Sandbox Code Playgroud)