2
$\begingroup$

I have the following question about calculating percentiles (to illustrate my example, I will use the R programming language).

Suppose I have the following data:

library(stats)
set.seed(123)

a <- rnorm(20, 20, 100)

a  

#  [1]  -36.047565   -3.017749  175.870831   27.050839   32.928774
#  [6]  191.506499   66.091621 -106.506123  -48.685285  -24.566197
# [11]  142.408180   55.981383   60.077145   31.068272  -35.584113
# [16]  198.691314   69.785048 -176.661716   90.135590  -27.279141

Given this data, I can calculate the 80th percentile:

quantile(a, 0.8)

#      80% 

# 100.5901 

My Question: Does anyone know the formula used to calculate this number 100.5901?

I spent some time thinking if the calculated percentile for a set of numbers needs to physically correspond to one of the numbers in that set. I then found out that apparently there are are many different ways that percentiles can be calculated. For example (https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/quantile):

#view reference
 ?quantile

quantAll <- function(x, prob, ...)
    t(vapply(1:9, function(typ) quantile(x, prob=prob, type = typ, ...),
             quantile(x, prob, type=1, ...)))

 quantAll(a, 0.8)

         [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]     [,9]
[1,] 90.13559 116.2719 90.13559 90.13559 116.2719 131.9537 100.5901 121.4991 120.1923

Based on the above calculations, it appears that the 80th percentile I calculated earlier corresponds to the #7 (I think this is the default option for the "quantile()" function in R). I tried to look at the formulas provided in the references for the "quantile()" function, but I do not quite understand the way the formula was written.

Can anyone please explain how these quantile formulas work, which of these formulas are considered "more standard", and in what kinds of situations does it make sense to use some of these formulas compared to the others?

Thanks!

$\endgroup$
2
  • 2
    $\begingroup$ There is no universally agreed-upon formula. Look at R documentation on quantile. The main idea is that the 80th percemtile has "not more than 80% of observations below and not more than 20% above." But that often leaves room for interpretation exactly what number is chosen as the 80th percentile. R allows use of about ten widely used specific methods. Differences among methods may be seem relatively large for small samples, but negligible for large samples. There seems to be no agreement among textbooks or software programs which type to use. $\endgroup$
    – BruceET
    Dec 22, 2021 at 15:06
  • $\begingroup$ I am puzzled by the deletion of the initial answer by @cdalitz. $\endgroup$
    – BruceET
    Dec 22, 2021 at 15:36

2 Answers 2

3
$\begingroup$

Here is a brief R session in which differences among several 'types' of 0.8 quantiles (80th percentiles) are shown for a fictitious normal sample of size 100. The default 'type' in R is type=7.

set.seed(2021); x = rnorm(100, 50, 7)
quantile(x, .8, type=1)
     80% 
54.43302 
quantile(x, .8, type=2)
     80% 
54.63839 
quantile(x, .8, type=6)
    80% 
54.7616 
quantile(x, .8, type=7)
     80% 
54.51517
quantile(x, .8)  # default is type 7
     80% 
54.51517  
quantile(x, .8, type=8)
     80% 
54.67946 

Upon sorting x from smallest to largest, observations 80 and 81 are as follows:

sort(x)[80:81]
[1] 54.43302 54.84375

The 80th percentile of the population is unambiguously $55.89135.$

qnorm(.8, 50, 7)
[1] 55.89135
$\endgroup$
2
  • 3
    $\begingroup$ (+1) Very helpful as usual. Not contradicting anything you say, but there are simple cases in which these calculations are all dubious or more precisely may conflict with intuition or expectation. Given a parent distribution that is 0 and 1 with equal probabilities most methods on most samples yield either 0 or 1 and very few yield 0.5 for the median. It is hard for me to regard that as wrong but people need to watch out for this because something like it will bite you sooner or later with percentile calculations. $\endgroup$
    – Nick Cox
    Dec 22, 2021 at 18:36
  • $\begingroup$ @ Nick Cox. Thanks for the word of caution. $\endgroup$
    – BruceET
    Dec 22, 2021 at 21:43
2
$\begingroup$

To expand on @BruceET, the quantile functions in R and in many statistical software packages were analyzed by Robert Hyndman (@Rob Hyndman here) and Yanan Fan around 25 years ago in their paper Sample Quantiles in Statistical Packages. This will have the definitions and comparisons for each. Some have an unbiased mean under the normal assumption, others an unbiased median. They have a twenty-year followup in which they lament various software package design decisions :)

$\endgroup$
1
  • 1
    $\begingroup$ Thanks to you and @cdalitz (in deleted post) for the reference: R.J. Hyndman, Y. Fan: "Sample Quantiles in Statistical Packages." The American Statistician 50, pp. 361-365 (1996) $\endgroup$
    – BruceET
    Dec 22, 2021 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.