*! version 1.0.0 08Nov2023

Title

centile2 -- Enhancement to Stata's official centile command that provides additional definitions for computing sample quantiles

Syntax

centile2 [varlist] [if] [in]
[,
options]

Main
centile(numlist) report specified centiles;
default is centile(50)

Options
type(#) an integer between 4 and 9 selecting one of six continuous quantile algorithms; default is type(6)

cci binomial exact; conservative confidence interval

normal normal, based on observed centiles

meansd normal, based on mean and standard deviation

level(#) set confidence level; default is level(95)

by, collect, and statsby are allowed; see prefix.

Description

centile2 is an enhancement to Stata's official centile command that allows the user to select amongst six algorithms
for defining the quantile (centile), corresponding to the continuous sample quantile types described in Hyndman and Fan (1996),
and likewise implemented in R's quantile function.
The availability of different definitions for computing sample quantiles ensures that the same result can be reproduced using different software
packages.

centile2 estimates specified centiles and calculates confidence intervals. If no varlist is specified,
centile2 calculates centiles for all the variables in the dataset. If no centiles are specified, medians are reported.

By default, centile2 uses a binomial method for obtaining confidence intervals that makes no assumptions about the underlying
distribution of the variable.

Options

Main

centile(numlist) specifies the centiles to be reported.
The default is to display the 50th centile.
Specifying
centile(5) requests that the fifth centile be reported.
Specifying centile(5 50 95) requests that the 5th, 50th, and 95th
centiles be reported.
Specifying centile(10(10)90) requests that the 10th, 20th, ..., 90th
centiles be reported.

Options

type(#) specifies which of the six algorithms should be used to compute
the centile. type(4) is a linear interpolation of the empirical cdf;
type(5) is a piecewise linear function where the knots are the values
midway through the steps of the empirical cdf; type(6) is used by centile
in Stata, as well as by Minitab and SPSS; type(7) is the default used in R's
quantile
function; type(8) produces quantile estimates that are approximately median-unbiased
regardless of the distribution of varlist; and type(9) produces quantile estimates
that are approximately unbiased for the expected order statistics if varlist is normally distributed.
The default is type(6) and will return the same results as centile.
Hyndman and Fan (1996) recommend using type(8).

cci (conservative confidence interval) forces the confidence limits to
fall exactly on sample values. Confidence intervals displayed with the
cci option are slightly wider than those with the default (nocci)
option.

normal causes the confidence interval to be calculated by using a formula
for the standard error of a normal-distribution quantile given by
Kendall and Stuart (1969, 237). The normal option
is useful when you want empirical centiles -- that is, centiles based on sample
order statistics rather than on the mean and standard deviation -- and are
willing to assume normality.

meansd causes the centile and confidence interval to be calculated based
on the sample mean and standard deviation, and it assumes normality.

level(#) specifies the confidence level, as a percentage, for
confidence intervals. The default is level(95) or as set by
set level.

Examples

Setup

. sysuse auto

Calculate the 50th centile for all variables in the dataset using the default definition type(6)

. centile2

Calculate the 25th, 50th, and 75th centiles for price using type(7) -- the default definition used in R's
quantile function

. centile2 price, centile(25 50 75) type(7)

Same as above but showing 99% CI

. centile2 price, centile(25 50 75) type(7) level(99)

Same as above but assuming normality

. centile2 price, centile(25 50 75) type(7) level(99) normal

Stored results

centile2 stores the following in r():

Scalars

r(type) definition # used for computing centile

r(N) number of observations

r(n_cent) number of centiles requested

r(c_#) value of # centile

r(lb_#) #-requested centile lower confidence
bound

r(ub_#) #-requested centile upper confidence
bound

Macros

r(centiles) centiles requested

References

Hyndman, R. J. and Y. Fan. 1996.
Sample quantiles in statistical packages. American Statistician 50: 361-365.

Kendall, M. G., and A. Stuart. 1969.
The Advanced Theory of Statistics, Vol. 1: Distribution Theory. 3rd ed.
London: Griffin.

Citation of centile2

centile2 is not an official Stata command. It is a free contribution
to the research community, like a paper. Please cite it as such:

Linden, Ariel (2023). CENTILE2: Enhancement to Stata's official centile command that provides additional definitions for computing sample quantiles

Author

Ariel Linden

President, Linden Consulting Group, LLC

alinden@lindenconsulting.org

http://www.lindenconsulting.org

Also see

Online: centile, summarize