*! version 1.1.0 30nov2012
help pctrim

Title

pctrim -- Mark or recode observations outside of a percentile range

Syntax

pctrim
varlist
[if] [in]
, [options]

percentiles(lb ub) Lower and upper bounds for percentile trimming. Defaults are 1 and 99.

by(varname) Group variables within which to check for outliers and calculate replacement values.

mark(newvarname) Generate indicator variable marking observations with outliers in any variable.

recode(mean|median|miss|bound) Recode outliers for each variable.

replace Replace existing outliers with recode value, after recode.

generate(stubname) Prefix for new, recoded variables, after recode.

copyrest Copy out-of-sample values from old variables to new variables, after recode generate.

missok Do not exclude observations with missing values for some or all variables.

Description

pctrim trims outlying observations based on percentile bounds. pctrim can operate on
a varlist. The user can create an indicator variable marking outliers or recode them. Recode
options include mean, median, upper/lower bounds, or system missing. Outliers may be
recoded in place, or new variables may be generated with trimmed data. The mark option
operates case-wise, marking observations that have outliers for any variable
in varlist. The recode option operates on each variable indpendently. Only
values that are outliers with respect to the current variable are recoded.
Recode can replace outliers with system missing, mean, median, or the relevant (upper or lower)
percentile bound. Replacement statistics are computed with outliers included.

pctrim works best if the variables in varlist have no missing observations. By default,
pctrim works on a single analysis sample after excluding any observations with missing
data for any variable in varlist. Observations with missing data are not considered when
identifying outliers or when computing replacement statistics. If the user would prefer to
operate on each variable independently, the option missok should be specified. Given
this option, pctrim operates on a single variable at a time, and does not consider
whether other variables in varlist have missing data.

Options

Main

percentiles(lb ub) allows the user to choose lower and upper percentile bounds to
designate outliers. The defaults are 1 and 99. 0 may be used for the lower bound if trimming
is only desired for the top of the distribution. Similarly, 100 may be used for the upper bound.

by(varlist) designates group variables. If specified, percentile bounds and
replacement statistics are calculated within each combination of group
variables. By variables must be numeric. Observations with missing data for the by-variables
are excluded. This is true even with the missok option.

mark(newvarname) creates a new indicator variable marking observations with outliers in
any of the variables in varlist. mark(newvarname) operates case-wise.

recode(mean|median|miss|bound) replaces outlying values for each variable.
recode(mean|median|miss|bound) operates on each variable indpendently. Only
observations that are outliers with respect to that variable are recoded.
Recode options are system missing, mean, median, or the relevant (upper or lower)
percentile bound.

replace replaces outlying values in existing variables.

generate(stubname) creates a new, trimmed variable for each variable in varlist.
Each new variable is prefixed with stubname.

copyrest specifies that out-of-sample values be copied from the original
variables. In line with other data-management commands, pctrim defaults
to setting newvar to missing (.) outside the observations selected by if
exp and in range. copyrest option can only be used with the recode
and generate options.

missok specifies that observations should not be excluded from the analysis
sample if they have missing values for some or all variables in varlist.
Missing values are ignored when computing percentile bounds and marking
outlying observations for each variable.

Examples

. sysuse auto

. pctrim weight mpg length , by(foreign) p(0 99) mark(outlier)

. pctrim weight mpg length , p(0 99) gen(tr_)

Author

Michael Barker

Georgetown University

mdb96@georgetown.edu

Also see

Help:
pctile ,
centile ,
egen pctile()

Net:
winsor ,
winsor2 ,
trimmean ,
trimplot