Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate quantiles in R without interpolation - round up or down to actual value

It's my understanding that when calculating quantiles in R, the entire dataset is scanned and the value for each quantile is determined.

If you ask for .8, for example it will give you a value that would occur at that quantile. Even if no such value exists, R will nonetheless give you the value that would have occurred at that quantile. It does this through linear interpolation.

However, what if one wishes to calculate quantiles and then proceed to round up/down to the nearest actual value?

For example, if the quantile at .80 gives a value of 53, when the real dataset only has a 50 and a 54, then how could one get R to list either of these values?

like image 367
jsuprr Avatar asked Apr 09 '15 19:04

jsuprr


2 Answers

Try this:

#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
like image 139
zx8754 Avatar answered Nov 15 '22 18:11

zx8754


There are many estimation methods implemented in R's quantile function. You can choose which type to use with the type argument as documented in https://stat.ethz.ch/R-manual/R-devel/library/stats/html/quantile.html.

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
like image 22
Tomás Senart Avatar answered Nov 15 '22 19:11

Tomás Senart