Tutorials › Exam PA - Predictive Analytics › Capping Extreme Values with Winsorization

Exam PA · Missingness, Outliers, and Factor Levels · Tutorial 182 of 966

Capping Extreme Values with Winsorization

Use specified quantiles to cap extreme values, verify the changes in R, and explain the trade-offs without confusing capping with deleting observations.

Intermediate 11 min read

What You'll Learn

  • Define winsorization using lower and upper quantile cutoffs
  • Calculate interpolated cutoffs using R’s default type-7 quantiles
  • Cap values in R while preserving missing entries and row count
  • Compare ranges, means, medians, and other summaries before and after capping
  • Explain which features of the data are altered and which are preserved
  • Avoid common errors in quantile selection and cutoff application

What Winsorization Does

An extreme value may be a valid observation rather than a recording error. Deleting it is not the only way to limit its influence on summaries or a model. Winsorization replaces values beyond specified lower and upper cutoffs with the cutoffs themselves. It retains the observations, but changes the recorded values at the tails.

The cutoffs are often chosen as quantiles. For example, winsorizing at the 5th and 95th percentiles caps values below the 5th-percentile cutoff at that lower cutoff, and values above the 95th-percentile cutoff at that upper cutoff. This is an editing rule, not a diagnosis: a value being capped does not establish that it was erroneous or unimportant.

Definition: For a numeric value \(x_i\), lower cutoff \(L\), and upper cutoff \(U\), the winsorized value is \(w_i=\min\{\max(x_i,L),U\}\), provided \(L\le U\). Thus, \(w_i=L\) if \(x_i<L\), \(w_i=x_i\) if \(L\le x_i\le U\), and \(w_i=U\) if \(x_i>U\). For quantile-based winsorization, \(L\) and \(U\) are calculated from specified lower and upper quantiles of the observed values.

A quantile convention must be stated because different conventions can produce different cutoffs, especially in small samples. R’s default for quantile() is type 7. For \(n\) sorted, non-missing observations \(x_{(1)}\le \cdots \le x_{(n)}\), type 7 calculates the quantile at probability \(p\) using \(h=1+(n-1)p\). If \(h\) is not an integer, it linearly interpolates between the observations at positions \(\lfloor h\rfloor\) and \(\lceil h\rceil\).

For example, if \(h=2.5\), the quantile is halfway between the second and third sorted observations. As a result, a cutoff need not equal an observed value, and the number of observations changed need not equal a simple percentage of the sample. Ties and interpolation both matter.

Calculating and Applying the Cutoffs in R

For a numeric vector x, the general R procedure is to calculate both cutoffs and clamp every observed value to that interval. For example, with probabilities 0.05 and 0.95, use quantile(x, probs = c(0.05, 0.95), na.rm = TRUE, type = 7) to obtain the cutoffs, then pmax(lower, pmin(x, upper)) to create the winsorized vector. The inner operation caps high values at the upper cutoff; the outer operation raises values below the lower cutoff.

With na.rm = TRUE, missing values are excluded when calculating the cutoffs. The clamping expression leaves missing entries missing. Check that the input is numeric, that the requested probabilities are in increasing order and between zero and one, and that the resulting lower cutoff is no greater than the upper cutoff. Inspect the number of non-missing values and the cutoffs before interpreting the result.

Worked Example: Capping Both Tails of a Small Data Set

A hypothetical analyst has recorded eight numeric processing times, in minutes: 2, 4, 5, 7, 9, 12, 40, and 100. Winsorize at the 12.5th and 87.5th percentiles using R’s type-7 quantiles. Find the cutoffs, the resulting values, and the changes to the range and mean. State one quantity that is unchanged.

Method and cutoffs. Sort the values; they are already in increasing order. With \(n=8\), the lower probability is \(p=0.125\), so \(h=1+7(0.125)=1.875\). Interpolate 0.875 of the way from the first value to the second: \(L=2+0.875(4-2)=3.75\).

For the upper probability \(p=0.875\), \(h=1+7(0.875)=7.125\). Interpolate 0.125 of the way from the seventh value to the eighth: \(U=40+0.125(100-40)=47.5\).

Apply the caps. The values below 3.75 become 3.75, and the value above 47.5 becomes 47.5. The winsorized vector is 3.75, 4, 5, 7, 9, 12, 40, 47.5. In R, the calculation is x <- c(2, 4, 5, 7, 9, 12, 40, 100), followed by q <- quantile(x, c(0.125, 0.875), type = 7) and w <- pmax(q[1], pmin(x, q[2])).

Check the summaries. The original minimum and maximum are 2 and 100, so the original range is 98. The new minimum and maximum are 3.75 and 47.5, so the new range is 43.75. The original sum is 179 and the original mean is \(179/8=22.375\). The winsorized sum is \(3.75+4+5+7+9+12+40+47.5=128.25\), giving a mean of \(128.25/8=16.03125\). The median remains \((7+9)/2=8\), because the two middle observations did not change.

Check: The output still contains eight values, and every output value lies between 3.75 and 47.5. The mistake to avoid is treating the interpolated upper cutoff as 40, the seventh observation; type 7 gives 47.5 here.

Why the Mean Changes and What Is Preserved

Winsorization changes the mean by changing the values it caps, not by changing the number of observations. The following identity makes that distinction explicit.

$$ \bar{w}-\bar{x} = \frac{1}{n}\left[ \sum_{x_i<L}(L-x_i) + \sum_{x_i>U}(U-x_i) \right] $$

Here, the sums run over the non-missing observations, \(n\) is their number, \(\bar{x}\) is their original mean, and \(\bar{w}\) is their winsorized mean. To justify the identity, write the winsorized mean as \(n^{-1}\sum_i w_i\) and subtract the original mean \(n^{-1}\sum_i x_i\). For each observation below \(L\), the difference \(w_i-x_i\) is \(L-x_i\); for one above \(U\), it is \(U-x_i\); and for one between the cutoffs, it is zero. Adding these differences and dividing by \(n\) gives the formula.

If \(L\) is above every value it replaces and \(U\) is below every value it replaces, the terms in the first sum are positive and the terms in the second are negative. Thus lower-tail capping raises the mean, while upper-tail capping lowers it. The net direction depends on their combined changes; it is not guaranteed that the mean moves up or down overall.

Winsorization keeps the same rows and, when missing values are left untouched, preserves which rows are missing for that variable. Because the transformation is nondecreasing, it does not reverse the ordering of two values. It can, however, make different values equal by replacing them with the same cutoff. It therefore changes tail magnitudes, the range, and often the mean and variance. Relationships with other variables can change as well, so retaining rows does not mean retaining all the information in their original values.

A median or another central summary may remain the same when the values determining it are not capped. It is not safe to assume that it always remains unchanged: the result depends on the sample, quantile convention, and cutoffs. The correct practice is to compute and compare the summaries rather than infer the effect from the name of the operation.

Worked Example: Unequal Tail Cutoffs

A hypothetical portfolio review records six positive claim amounts, in thousands of dollars: 10, 12, 13, 14, 15, and 90. Winsorize at the 10th and 90th percentiles using type 7. Report the cutoffs, transformed amounts, range, mean, and median.

Method and cutoffs. For the 10th percentile, \(h=1+5(0.10)=1.5\), halfway between 10 and 12. Therefore \(L=10+0.5(12-10)=11\). For the 90th percentile, \(h=1+5(0.90)=5.5\), halfway between 15 and 90. Therefore \(U=15+0.5(90-15)=52.5\).

Apply the caps. The value 10 becomes 11, and 90 becomes 52.5. The transformed data are 11, 12, 13, 14, 15, and 52.5. The original range is \(90-10=80\) thousand dollars; the new range is \(52.5-11=41.5\) thousand dollars.

Recalculate summaries. The original sum is \(10+12+13+14+15+90=154\), so its mean is \(154/6=25.6667\) thousand dollars. The transformed sum is \(11+12+13+14+15+52.5=117.5\), so its mean is \(117.5/6=19.5833\) thousand dollars. Both medians are \((13+14)/2=13.5\) thousand dollars. The transformed values remain in the stated units; rounding only now, the means are 25.67 and 19.58 thousand dollars to two decimal places.

Check: The lower-tail increase is \(11-10=1\) and the upper-tail decrease is \(52.5-90=-37.5\); their sum, \(-36.5\), divided by six is \(-6.0833\), the change in the mean. A common mistake is to use 15 as the upper cutoff because it is the fifth observation. Type-7 interpolation at probability 0.90 gives 52.5, not 15.

Keep the Rule Reproducible

The cutoff probabilities and quantile convention are part of the data-preparation rule and should be recorded along with the resulting cutoffs. This makes the edit auditable: another analyst can distinguish a value that was observed from one that was capped. It also helps prevent a subtle inconsistency when the rule is used beyond the data on which it was developed.

For a predictive model, if cutoffs are estimated from a training sample, preserve those cutoff values and apply them to later data. Recalculating quantiles separately on new data creates a different transformation, so a given raw value can be mapped to a different capped value depending on which data set it appears in. Missing values should also be handled according to a stated rule rather than silently converted to a cutoff.

Winsorization is useful when the analysis calls for limiting extreme magnitudes while retaining the rows, but it is not a universal remedy for influential observations. Capping a valid large claim, for example, changes its amount and may affect a model’s interpretation. Before using the transformed variable, report the cutoff probabilities, the quantile convention, how many values were changed, and relevant before-and-after summaries. Keep the original variable available when appropriate so the edit can be reviewed.

Key takeaway: Winsorization replaces values outside specified cutoffs; it does not remove their rows. State how the cutoffs were calculated, verify the resulting values and summaries, and explain that tail magnitudes—and potentially analyses based on them—have been altered.

Check Your Understanding

Use R’s type-7 quantiles unless a question states otherwise. For each item, calculate the cutoffs before applying them.

  1. For values 3, 5, 7, 9, and 50, winsorize at the 20th and 80th percentiles. Give the cutoffs and transformed values. (Answer: Cutoffs 4.6 and 17.2; transformed values 4.6, 5, 7, 9, 17.2.)
  2. For values 4, 6, 8, 10, 12, and 40, winsorize at the 20th and 80th percentiles. Give the transformed mean and median. (Answer: Transformed values 6, 6, 8, 10, 12, 12; mean 9 and median 9.)
  3. For values 2, 4, 6, 8, and 20, winsorize at the 10th and 90th percentiles. Give the transformed mean. (Answer: Cutoffs 2.8 and 15.2; transformed mean 7.2.)
  4. A vector contains 1, 3, 5, 7, a missing value, and 30. Using the 25th and 75th percentiles with missing values excluded from cutoff calculation, give the cutoffs, transformed non-missing values, and transformed mean. (Answer: Cutoffs 3 and 7; transformed non-missing values 3, 3, 5, 7, 7; mean 5; the missing value remains missing.)
  5. A training vector contains 10, 20, 30, 40, and 100. Calculate 20th- and 80th-percentile cutoffs from training data, then apply them without recalculating to new values 5, 35, 80, and a missing value. Give the cutoffs and transformed new values. (Answer: Training cutoffs 18 and 52; transformed new values 18, 35, 52, and missing.)
Clinical Biostats