Tutorials › Exam PA - Predictive Analytics › Robust Scaling With Medians and Interquartile Ranges

Exam PA · Transformations, Scaling, and Binning · Tutorial 211 of 966

Robust Scaling With Medians and Interquartile Ranges

Use the median and interquartile range to scale predictors in R, and assess what this robust scaling does—and does not—protect against.

Intermediate 11 min read

What You'll Learn

  • Define robust scaling using a predictor’s sample median and interquartile range
  • Calculate scaled values using R quantiles with the type 7 convention
  • Verify that the transformed median is zero and the transformed IQR is one
  • Explain why transformed quartiles are not necessarily minus 0.5 and plus 0.5
  • Compare the response of robust scaling and z-score scaling to an extreme observation
  • Identify limitations of robust scaling when working with unusual observations

Why Scale With the Median and Interquartile Range?

Predictors measured in different units can have very different numerical ranges. Scaling can make their magnitudes more comparable, which is useful for methods that use distances or are sensitive to predictor scale. Earlier material on z-score and min-max scaling introduced alternatives, but both can be affected by extreme observations: a large value can pull the mean and standard deviation, or set the endpoint of a min-max range.

Robust scaling uses the median as its center and the interquartile range as its scale. These statistics are less sensitive to a small number of extreme observations than the mean and standard deviation or the observed minimum and maximum. The word “robust” describes this relative resistance; it does not mean that the transformation removes outliers or is unaffected by every unusual data pattern.

Definition: For a numeric predictor \(X\), let \(m\) be its sample median, let \(Q_1\) and \(Q_3\) be its first and third sample quartiles, and let \(\operatorname{IQR}=Q_3-Q_1\). Provided \(\operatorname{IQR}>0\), the robust-scaled value corresponding to an observed value \(x\) is $$ x_{\mathrm{robust}}=\frac{x-m}{\operatorname{IQR}}. $$ The transformation subtracts the median and then divides by the interquartile range.

The result is dimensionless: the numerator and denominator have the same units, so those units cancel. A robust-scaled value of 1 means the observation is one original-sample IQR above the median; a value of \(-1\) means it is one IQR below the median. The scale is the full distance from the first to the third quartile, not half that distance.

The calculation depends on how sample quartiles are defined. R’s type 7 quantiles are its default and use linear interpolation between ordered observations. To make calculations reproducible, this tutorial uses that convention throughout. For a vector with \(n\) ordered values, type 7 locates quantile \(p\) at position \(1+(n-1)p\), interpolating between adjacent ordered values if the position is not an integer. The median is consistent with the type 7 50th percentile.

What the Transformation Guarantees

If the same quartile convention is used to calculate \(m\), \(Q_1\), and \(Q_3\), and the IQR is positive, robust scaling places the sample median at zero. The transformed IQR is one. These are the general guarantees; they do not guarantee that the transformed quartiles are \(-0.5\) and \(0.5\).

$$ \frac{m-m}{\operatorname{IQR}}=0, \qquad \frac{Q_3-m}{\operatorname{IQR}}-\frac{Q_1-m}{\operatorname{IQR}} =\frac{Q_3-Q_1}{\operatorname{IQR}}=1. $$

The derivation follows directly from the transformation. Subtracting the median from itself gives zero. To find the distance between the transformed quartiles, subtract the transformed \(Q_1\) from the transformed \(Q_3\); the shared median terms cancel, leaving \((Q_3-Q_1)/\operatorname{IQR}=1\). The transformed quartiles are individually \((Q_1-m)/\operatorname{IQR}\) and \((Q_3-m)/\operatorname{IQR}\). They equal \(-0.5\) and \(0.5\) only if \(m\) is exactly halfway between \(Q_1\) and \(Q_3\). Skewed data need not have that property.

Worked Example: Calculating Robust-Scaled Values in R

A hypothetical portfolio analyst uses the predictor “annual service calls per contract” for five contracts. The observed counts are 2, 4, 5, 7, and 9. Calculate the median, type 7 quartiles, IQR, and robust-scaled value for each observation. Use the same quartile convention in the manual calculation and in R.

Method. Order the data, calculate the type 7 quartiles, and apply the defining formula. Here \(n=5\). The type 7 position for \(Q_1\) is \(1+(5-1)(0.25)=2\), so \(Q_1=4\). The median is the third value, 5. The \(Q_3\) position is \(1+(5-1)(0.75)=4\), so \(Q_3=7\). Therefore, \(\operatorname{IQR}=7-4=3\).

The scaled values are calculated observation by observation:

$$ \begin{aligned} 2 &: (2-5)/3=-1,\\ 4 &: (4-5)/3=-1/3,\\ 5 &: (5-5)/3=0,\\ 7 &: (7-5)/3=2/3,\\ 9 &: (9-5)/3=4/3. \end{aligned} $$

R calculation. With x <- c(2, 4, 5, 7, 9), use med <- median(x), q <- quantile(x, c(0.25, 0.75), type = 7), and robust_x <- (x - med) / diff(q). These give med = 5, q = (4, 7), diff(q) = 3, and scaled values \(-1\), \(-1/3\), \(0\), \(2/3\), \(4/3\), respectively.

Check. The transformed median is 0. The transformed quartiles are \(-1/3\) and \(2/3\), whose difference is 1. They are not \(-0.5\) and \(0.5\), because the median 5 is not halfway between \(Q_1=4\) and \(Q_3=7\). The common mistake this avoids is assuming that robust scaling always places the two quartiles symmetrically around zero.

R Implementation and Interpretation

For a numeric vector x, the core R calculation is med <- median(x), qs <- quantile(x, c(0.25, 0.75), type = 7), iqr <- qs[2] - qs[1], and x_robust <- (x - med) / iqr. R also provides an IQR function; by default it uses type 7 quartiles. Writing the quantile calculation explicitly makes the convention visible and helps prevent a mismatch if a different convention is used elsewhere.

Before dividing, check that the IQR is positive. If it is zero, the formula is undefined; handling constant and near-constant predictors is a separate practical issue. Do not interpret an extremely small positive IQR as equivalent to a typical scale: division by a small number can produce very large transformed values. Also decide how missing observations are being handled before calculating the center and spread. For example, median(x, na.rm = TRUE) removes missing values for the median, but the quantile calculation must be configured consistently if missing values are present.

Robust scaling does not change the ordering of observations when the IQR is positive, because every value is transformed by subtracting the same number and dividing by the same positive number. Values below the median remain below zero; values above it remain above zero. The distance between two transformed observations is their original distance divided by the IQR. Consequently, the transformation changes the units and center, but not the rank order.

Worked Example: Comparing Robust Scaling With Z-Score Scaling

A hypothetical claims team records annual claim counts for ten policy groups. For a simple comparison, first use the values 0 through 9, then consider a second data set in which the final value 9 is replaced by 1,000. Compare the robust-scaled value and z-score of the unchanged observation 5. For z-scores, use the sample mean and sample standard deviation, with denominator \(n-1\) in the sample variance.

Method. For the original values \(0,1,\ldots,9\), \(n=10\). The type 7 positions are \(1+9(0.25)=3.25\) and \(1+9(0.75)=7.75\). The third and fourth ordered values are 2 and 3, so \(Q_1=2+0.25(3-2)=2.25\). The seventh and eighth values are 6 and 7, so \(Q_3=6+0.75(7-6)=6.75\). Thus the IQR is \(6.75-2.25=4.5\). The median is \((4+5)/2=4.5\), and the robust-scaled value for 5 is \((5-4.5)/4.5=0.1111\), to four decimal places.

For the original z-score, the mean is \(4.5\). The sum of squared deviations from 4.5 is \(82.5\), so the sample variance is \(82.5/9=9.1667\), and the sample standard deviation is approximately \(3.0277\). The z-score for 5 is \((5-4.5)/3.0277=0.1651\), to four decimal places.

Now replace 9 by 1,000. The ordered values are \(0,1,2,3,4,5,6,7,8,1000\). The first and third quartile positions are still 3.25 and 7.75, and both interpolate among the same interior values as before. Thus \(Q_1=2.25\), \(Q_3=6.75\), IQR \(=4.5\), and median \(=4.5\). The robust-scaled value for 5 remains \(0.1111\).

For the new z-score, the sum of the observations is 1,036, so the mean is \(1,036/10=103.6\). The sum of squared observations is \(1,000,204\). The sum of squared deviations is \(1,000,204-(1,036)^2/10=892,874.4\), giving sample variance \(892,874.4/9=99,208.2667\) and sample standard deviation approximately \(314.9734\). The z-score for 5 is \((5-103.6)/314.9734=-0.3130\), to four decimal places.

Check and interpretation. The unchanged value 5 has the same robust-scaled value in both data sets, while its z-score changes from \(0.1651\) to \(-0.3130\). In this example, the single extreme observation does not alter the median or quartiles, but it strongly changes the mean and sample standard deviation. The common mistake is to conclude that robust scaling makes the extreme observation harmless: its own robust-scaled value is \((1000-4.5)/4.5=221.2222\), so it remains very far from the center. Robust scaling has limited the observation’s influence on the scaling parameters; it has not removed or capped the observation.

Quartile Asymmetry and the Main Trap

The relationship between the transformed quartiles and zero depends on the location of the median within the original IQR. A skewed sample can have a median much nearer one quartile than the other. The width between the transformed quartiles remains one, but their positions relative to zero can be asymmetric.

Worked Example: Why the Quartiles Need Not Be Minus 0.5 and Plus 0.5

A hypothetical book of seven contracts has predictor values \(1,2,3,4,5,100,100\). Using type 7 quartiles, calculate the robust-scaled median and quartiles. Then assess the claim that the quartiles must be \(-0.5\) and \(0.5\).

Method. The values are already ordered. The type 7 \(Q_1\) position is \(1+6(0.25)=2.5\), halfway between 2 and 3, so \(Q_1=2.5\). The median is the fourth value, \(m=4\). The \(Q_3\) position is \(1+6(0.75)=5.5\), halfway between 5 and 100, so \(Q_3=52.5\). Therefore, the IQR is \(52.5-2.5=50\).

$$ Q_{1,\mathrm{robust}}=\frac{2.5-4}{50}=-0.03, \qquad m_{\mathrm{robust}}=0, \qquad Q_{3,\mathrm{robust}}=\frac{52.5-4}{50}=0.97. $$

Check and correction. The transformed quartile difference is \(0.97-(-0.03)=1\), as required. The quartiles are not \(-0.5\) and \(0.5\). The incorrect calculation assumes that the median is midway between 2.5 and 52.5; it is not. The midpoint of those quartiles is \((2.5+52.5)/2=27.5\), whereas the median is 4. This example illustrates why the guaranteed statements are “transformed median zero” and “transformed IQR one,” not “transformed quartiles are always symmetric around zero.”

This distinction matters when interpreting scaled values. A transformed value of zero identifies the sample median, and a difference of one between transformed quartiles identifies the scaled IQR. Neither statement implies that half the IQR lies above the median and half below it. When reporting a scaled predictor, describe what the transformation actually guarantees rather than inferring symmetry.

When Robust Scaling Is Useful—and Its Limits

Median-and-IQR scaling can be useful when predictors have different units and a few unusually large or small observations would make mean-and-standard-deviation scaling less representative of the central data. Because the median and quartiles depend on ranks and interpolation among ordered observations, an extreme value may have little or no effect on them. This can help distance-based methods avoid having the scale of one predictor dominated by a small number of extremes.

There are important limits. First, an extreme observation remains extreme after scaling; robust scaling is not winsorization, trimming, or deletion. Second, the quartiles can change when the sample changes, especially in a small sample or when many observations occur near a quartile boundary. Third, if the IQR is zero, the transformation cannot be applied as written. Fourth, robust scaling does not make a predictor’s relationship with an outcome linear, remove skewness, or establish that an unusual observation is an error. Those questions require examining the data and the modeling purpose.

Scaling is also a modeling choice, not a claim about the underlying risk or causal effect. In a linear predictor, changing units changes the numerical coefficient needed to represent the same fitted relationship; the interpretation must be in the transformed units. For a method based on distances, the scaling can alter how much each predictor contributes to those distances. The choice between z-score, min-max, and robust scaling should therefore reflect the data’s shape and the method’s needs, rather than a rule that one scale is universally best.

Key takeaway: Robust scaling subtracts the sample median and divides by the sample IQR. With a positive IQR and a consistent quantile convention, it guarantees a transformed median of zero and transformed IQR of one. It does not guarantee quartiles of \(-0.5\) and \(0.5\), and it does not eliminate extreme observations.

Check Your Understanding

Use type 7 quantiles unless a question specifies otherwise. Answers involving probabilities are not needed; retain exact fractions where convenient.

  1. For the values \(3,5,7,9,11\), calculate the median, \(Q_1\), \(Q_3\), IQR, and robust-scaled value of 9. (Answer: median \(7\), \(Q_1=5\), \(Q_3=9\), IQR \(4\), scaled value \(0.5\).)
  2. For the values \(2,4,6,8,10,12\), type 7 interpolation gives \(Q_1=4.5\), median \(7\), and \(Q_3=9.5\). What is the IQR, and what is the robust-scaled value of 12? (Answer: IQR \(5\); scaled value \(1\).)
  3. A predictor has \(Q_1=10\), median \(13\), and \(Q_3=22\). Find the robust-scaled quartiles and explain whether they are symmetric around zero. (Answer: IQR \(12\); scaled quartiles \(-0.25\) and \(0.75\), so they are not symmetric.)
  4. Write the R expression to scale vector x using its median and type 7 IQR. (Answer: (x - median(x)) / IQR(x, type = 7), or explicitly calculate type 7 quartiles and divide by their difference.)
  5. A single very large observation leaves a sample’s type 7 median and quartiles unchanged. What happens to the robust-scaled values of the unchanged observations, and what conclusion should you avoid? (Answer: Their robust-scaled values are unchanged; do not conclude that the extreme observation has been removed or made small.)
Clinical Biostats