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

2

6

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?

Dualism answered 9/4, 2015 at 19:39 Comment(0)
Y
5

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
Yeryerevan answered 9/4, 2015 at 20:3 Comment(1)
Not sure I understand your comment, please add reproducible dummy data to your post, and expected output.Yeryerevan
O
5

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
Olgaolguin answered 8/8, 2015 at 11:15 Comment(1)
I tried different values of the "type" argument until I got what I wanted. In my most recent case, "type = 1" did the trick; in other projects, other ways of calculating quantile might be more appropriate.Significs

© 2022 - 2024 — McMap. All rights reserved.