help pctile, help xtile, help _pctile dialogs: pctile xtile
-------------------------------------------------------------------------------
Title
[D] pctile -- Create variable containing percentiles
Syntax
Create variable containing percentiles
pctile [type] newvar = exp [if] [in] [weight] [, pctile_options]
Create variable containing quantile categories
xtile newvar = exp [if] [in] [weight] [, xtile_options]
Compute percentiles and store them in r()
_pctile varname [if] [in] [weight] [, _pctile_options]
pctile_options description
-------------------------------------------------------------------------
Main
nquantiles(#) number of quantiles; default is nquantiles(2)
genp(newvarp) generate newvarp variable containing percentages
altdef use alternative formula for calculating
percentiles
-------------------------------------------------------------------------
xtile_options description
-------------------------------------------------------------------------
Main
nquantiles(#) number of quantiles; default is nquantiles(2)
cutpoints(varname) use values of varname as cutpoints
altdef use alternative formula for calculating
percentiles
-------------------------------------------------------------------------
_pctile_options description
-------------------------------------------------------------------------
nquantiles(#) number of quantiles; default is nquantiles(2)
percentiles(numlist) calculate percentiles corresponding to the
specified percentages
altdef use alternative formula for calculating
percentiles
-------------------------------------------------------------------------
aweights, fweights, and pweights are allowed (see [U] 11.1.6 weight),
except when the altdef option is specified, in which case no weights
are allowed.
Menu
pctile
Statistics > Summaries, tables, and tests > Summary and descriptive
statistics > Create variable of percentiles
xtile
Statistics > Summaries, tables, and tests > Summary and descriptive
statistics > Create variable of quantiles
Description
pctile creates a new variable containing the percentiles of exp, where
the expression exp is typically just another variable.
xtile creates a new variable that categorizes exp by its quantiles. If
the cutpoints(varname) option is specified, it categorizes exp using the
values of varname as category cutpoints. For example, varname might
contain percentiles of another variable, generated by pctile.
_pctile is a programmer's command that computes up to 1,000 percentiles
and places results in r(); see [U] 18.8 Accessing results calculated by
other programs. summarize, detail computes some percentiles (1, 5, 10,
25, 50, 75, 90, 95, and 99th); see [R] summarize.
Options
+------+
----+ Main +-------------------------------------------------------------
nquantiles(#) specifies the number of quantiles. It computes percentiles
corresponding to percentages 100*k/m for k=1, 2, ..., m-1, where m=#.
For example, nquantiles(10) requests that the 10th, 20th, ..., 90th
percentiles be computed. The default is nquantiles(2); i.e., the
median is computed.
genp(newvarp) (pctile only) specifies a new variable to be generated
containing the percentages corresponding to the percentiles.
altdef uses an alternative formula for calculating percentiles. The
default method is to invert the empirical distribution function by
using averages, where the function is flat (the default is the same
method used by summarize; see [R] summarize). The alternative
formula uses an interpolation method. Weights cannot be used when
altdef is specified.
cutpoints(varname) (xtile only) requests that xtile use the values of
varname, rather than quantiles, as cutpoints for the categories. All
values of varname are used, regardless of any if or in restriction.
percentiles(numlist) (_pctile only) requests percentiles corresponding to
the specified percentages. Percentiles are placed in r(r1), r(r2),
..., etc. For example, percentiles(10(20)90) requests that the 10th,
30th, 50th, 70th, and 90th percentiles be computed and placed into
r(r1), r(r2), r(r3), r(r4), and r(r5). Up to 1,000 (inclusive)
percentiles can be requested. See [U] 11.1.8 numlist for details
about specifying a numlist.
Examples
---------------------------------------------------------------------------
Setup
. sysuse auto
Create variable pct containing the deciles of mpg
. pctile pct = mpg, nq(10)
List the resulting data
. list pct in 1/10
Setup
. drop pct
Create variable pct containing the deciles of mpg, and create variable
percent containing the percentages
. pctile pct = mpg, nq(10) genp(percent)
List the resulting data
. list percent pct in 1/10
Setup
. drop pct percent
Create pct containing the deciles of mpg weighted by weight, and create
variable percent containing the percentages
. pctile pct = mpg [w=weight], nq(10) genp(percent)
List the resulting data
. list percent pct in 1/10
---------------------------------------------------------------------------
Setup
. webuse bp1, clear
List variable bp
. list bp, sep(4)
Create variable quart containing the quartiles of bp
. xtile quart = bp, nq(4)
List variables bp and quart
. list bp quart, sep(4)
Setup
. input class
class
1. 100
2. 110
3. 120
4. 130
5. end
Create variable category categorizing bp using the values of class as
category cutpoints
. xtile category = bp, cut(class)
List variables bp, class, and category
. list bp class category, sep(4)
---------------------------------------------------------------------------
Setup
. sysuse auto, clear
Compute the deciles for weight and store then in r(r#)
. _pctile weight, nq(10)
List the saved results
. return list
Compute the specified percentiles for weight and store them in r(r#)
. _pctile weight, p(10, 33.333, 45 50, 55, 66.667, 90)
List the saved results
. return list
---------------------------------------------------------------------------
Saved results
pctile and _pctile saves the following in r():
Scalars
r(r#) value of #-requested percentile
Also see
Manual: [D] pctile
Help: [R] centile, [R] summarize