Dummy example of the 9 types of Quantiles

Viewed 27

We all know that the R software has 9 ways of calculating quantiles through the function quantile(x,probs,type=1,2,3,4...).

I'm looking for a nice simple and stupid special example for my students where each type of quantile calculation method would return a different value with a sample size of minimum 20 values.

The best would be an example where they all return a different value for the quantile 0.5 (ie the median).

I tried to simulate some random vectors but i wasn't able to get a different values for all of them for a given quantile at the same time.

Thanks for your help.

1 Answers

Take a look at the paper referenced in ?quantile.

Based on the definitions, type = 1,3,4 should always give the same result for probs = 0.5, and type = 2,5,6,7,8,9 should always give the same result for probs = 0.5. The differences are most easily seen near the first or last order statistic:

quantile.vec <- Vectorize(function(x, probs, type) quantile(x, probs, type = type), "type")

quantile.vec(1:4, c(0.25, 0.3, 0.5), 1:9)
#>     [,1] [,2] [,3] [,4] [,5] [,6] [,7]     [,8]   [,9]
#> 25%    1  1.5    1  1.0  1.5 1.25 1.75 1.416667 1.4375
#> 30%    2  2.0    1  1.2  1.7 1.50 1.90 1.633333 1.6500
#> 50%    2  2.5    2  2.0  2.5 2.50 2.50 2.500000 2.5000
quantile.vec(1:20, c(0.05, 0.06, 0.5), 1:9)
#>     [,1] [,2] [,3] [,4] [,5]  [,6]  [,7]      [,8]    [,9]
#> 5%     1  1.5    1  1.0  1.5  1.05  1.95  1.350000  1.3875
#> 6%     2  2.0    1  1.2  1.7  1.26  2.14  1.553333  1.5900
#> 50%   10 10.5   10 10.0 10.5 10.50 10.50 10.500000 10.5000

These two plots might be useful.

matplot(quantile.vec(1:4, seq(0, 1, 0.05), 1:3), type = "p", pch = c(1, 3:4), col = 1:3, xlab = "prob", ylab = "quantile")
legend("topleft", legend = 1:3, pch = c(1, 3:4), col = 1:3)

enter image description here

matplot(quantile.vec(1:4, seq(0, 1, 0.05), 4:9), type = "p", pch = 0:6, col = 1:6, xlab = "prob", ylab = "quantile")
legend("topleft", legend = 4:9, pch = 0:6, col = 1:6)

enter image description here

The difference between types 8 and 9 is small.

Related