Title

winsor4 - trims or winsorizes a variable based on outliers defined by either percentiles or interquartile range.

Syntax

winsor4 varname [if] [in], method(string) outlier(string) level(string) [newvar(string)] [positive] [group(string)]

Description

winsor4 provides methods for handling outliers in your data by either trimming (i.,e., replacing the outliers with missing values) or winsorizing (i.e., replacing outliers with the maximum and minimum specified).
Outliers can either be identified by specifying a pre-determined level in the distribution (e.g., bottom 5% and top 95%), or by using the interquartile range (IQR) method.
The distribution can be computed unconditionally over the full distribution of varname, or within specified groups (e.g., for each year). Only one variable at a time is supported.

Options

method(string): Specifies the method to be applied. This is a required option. Must be one of the following:

trim : Replace the outliers with missing values.

winsor : Replace the outliers with the maximum and minimum specified.

outlier(string): Specifies outliers' definition. This is a required option. Must be one of the following:

tail : Outliers are defined based on percentiles.

iqr : Outliers are defined based on the IQR.

level(string): Specifies the threshold level for determining outliers. For outlier(tail) this represents the percentile cut-off. For outlier(iqr), this represents the multiplier of the interquartile range. For example, level(5) combined with outlier(tail) would correspond to the 5th and 95th percentiles. Similarly, level(5) combined with outlier(iqr) would correspond to 5 times the interquartile range. level is a required option.

newvar(string): Specifies the name of a new variable where the results will be stored. If not provided, the original variable is replaced. newvar is not a required option.

positive: Applies the operation only to strictly positive values of the variable. Values less than or equal to zero are set to zero. positive is not a required option.

group(string): Specifies a grouping variable. The operations are applied within each group separately. group is not a required option.

Example

*- Winsor based on (p1 p99) percentiles and replace the variable.

. sysuse nlsw88, clear

. winsor4 wage, method(winsor) outlier(tail) level(1)

*- Same but create a new variable and preserve the old one without changes.

. winsor4 wage, method(winsor) outlier(tail) level(1) newvar(wage_winsor)

*- Same but winsorize only the strictly positive values

. winsor4 wage, method(winsor) outlier(tail) level(1) newvar(tenure_winsor) positive

*- Trim based on (p1 p99) percentiles and replace the variable.

. winsor4 wage, method(trim) outlier(tail) level(1)

*- Winsor based on one time the IQR and replace the variable.

. winsor4 wage, method(winsor) outlier(iqr) level(1)

*- Winsor based on (p1 p99) percentiles by industry and replace the variable

. winsor4 wage, method(winsor) outlier(tail) level(1) group(industry)

Authors

Adrien Matray (adrien.matray@gmail.com), Pablo E. Rodriguez (pablo6@mit.edu)

Comments

Send any suggestions or feedback to Adrien Matray (adrien.matray@gmail.com).