Tutorials › Exam PA - Predictive Analytics › Standardizing Predictors Before Ridge Fitting

Exam PA · Penalties and Ridge Regression · Tutorial 404 of 966

Standardizing Predictors Before Ridge Fitting

See how predictor units affect ridge shrinkage, and how to standardize predictors using training-set means and standard deviations before fitting.

Intermediate 12 min read

What You'll Learn

  • Explain why a common ridge penalty can shrink coefficients differently when predictor units differ
  • Center predictors and scale them using their training-set standard deviations
  • Translate standardized coefficients and predictions back to original predictor units
  • Apply training-set transformations consistently to validation or new observations
  • Check ridge calculations by computing fitted values and residual sums of squares
  • Identify unit-change and standardization mistakes that alter the fitted model

Why Predictor Units Matter in Ridge Regression

A ridge penalty adds a squared-coefficient cost to a model's residual sum of squares. That cost is not automatically comparable across predictors: the numerical size of a coefficient depends on the units in which its predictor is recorded. A coefficient measured per dollar, for example, will generally have a different numerical size from one measured per thousand dollars, even when both describe the same relationship.

For this tutorial, consider a linear regression with an intercept and \(p\) numeric predictors. The ridge objective is the residual sum of squares plus a common penalty on the slopes. The intercept is not penalized. The response remains in its original units; the issue here is how predictor units affect the slope penalty.

$$ \min_{\beta_0,\boldsymbol{\beta}}\left\{ \sum_{i=1}^n\left(y_i-\beta_0-\sum_{j=1}^p\beta_jx_{ij}\right)^2 +\lambda\sum_{j=1}^p\beta_j^2 \right\},\qquad \lambda\geq 0. $$

Here \(y_i\) is the response for case \(i\), \(x_{ij}\) is predictor \(j\), \(\beta_0\) is the intercept, \(\beta_j\) is its slope, and \(\lambda\) controls the penalty strength. This objective uses an unscaled residual sum of squares; another convention may divide the residual term by a constant, which changes the numerical meaning of \(\lambda\). The unit-dependence argument is the same.

Definition: For a numeric predictor \(j\), center and scale it by calculating its mean \(\bar{x}_j\) and standard deviation \(s_j>0\), then forming \(z_{ij}=(x_{ij}-\bar{x}_j)/s_j\). Unless specified otherwise, \(s_j\) here is the sample standard deviation, calculated with denominator \(n-1\). The standardized predictor has mean zero and sample standard deviation one in the data used to calculate those quantities.

Standardization makes the penalty act on slopes expressed per one standard deviation of a predictor, rather than per one unit in whatever scale happened to be recorded. It does not make every modeling decision automatic: the means and standard deviations must be calculated from the training data, and the same values must be used to transform validation, test, and future observations.

A One-Predictor Derivation: Why Changing Units Changes Ridge

The unit-dependence can be seen without solving a general regression problem. Suppose the response and predictor are centered, so the intercept estimate is zero, and there is one predictor \(x\). Differentiating the objective with respect to its slope gives the ridge estimate below.

$$ \widehat{\beta}_{\mathrm{ridge}} =\frac{\sum_i x_i y_i}{\sum_i x_i^2+\lambda}. $$

The numerator measures the predictor-response cross-product. The denominator adds \(\lambda\) to the predictor's sum of squares. If the predictor is changed to \(x^*=c x\), where \(c\neq0\), its coefficient is measured in different units. The new estimate is \(\widehat{\gamma} = c\sum_i x_i y_i/(c^2\sum_i x_i^2+\lambda)\). Its predictions, expressed using the original predictor, have slope \(c\widehat{\gamma}\). In general this is not the original ridge slope, because the fixed \(\lambda\) has not changed along with the predictor's units.

By contrast, if the predictor is replaced by its standardized form \(z=(x-\bar{x})/s\), the coefficient \(\theta\) describes the change in fitted response per one standard deviation of \(x\). Since \(x=\bar{x}+sz\), the original-unit slope is \(\beta=\theta/s\). Substitution into the objective shows exactly what scaling does to the penalty.

$$ \lambda\theta^2=\lambda(s\beta)^2=\lambda s^2\beta^2. $$

Thus, fitting a ridge model with a standardized predictor is equivalent, in original slope units, to penalizing \(\beta^2\) with weight \(\lambda s^2\). With several predictors, the corresponding penalty is \(\lambda\sum_j s_j^2\beta_j^2\). This is why standardization gives a shared penalty a more comparable interpretation: each coefficient is measured for a one-standard-deviation change. It does not mean the resulting original-unit slopes will be equal or that the penalty has no effect.

Worked Example: The Same Predictor in Two Units

A hypothetical portfolio study has three centered observations with predictor values \(x=(-2,0,2)\) and response values \(y=(-2,0,2)\). Fit the one-predictor ridge slope with \(\lambda=4\). Then recode the predictor as \(x^*=2x\) and compare the fitted slopes and predictions. Finally, standardize \(x\) using its sample standard deviation.

Method. Use the centered one-predictor ridge formula. The intercept is zero because both vectors have mean zero. First, \(\sum x_i y_i=4+0+4=8\) and \(\sum x_i^2=4+0+4=8\), so the slope on the original \(x\) scale is \(8/(8+4)=2/3\). The fitted values are \((-4/3,0,4/3)\).

For \(x^*=2x=(-4,0,4)\), the cross-product is \(\sum x_i^*y_i=8+0+8=16\), and \(\sum (x_i^*)^2=16+0+16=32\). The slope per unit of \(x^*\) is therefore \(16/(32+4)=4/9\). Since \(x^*=2x\), the predictions expressed as a slope on the original \(x\) scale use slope \(2(4/9)=8/9\). The fitted values are \((-16/9,0,16/9)\), not the earlier \((-4/3,0,4/3)\). Merely changing units changed the ridge fit.

For standardization, \(\bar{x}=0\) and the sample standard deviation is \(s=\sqrt{(4+0+4)/(3-1)}=2\). Hence \(z=(-1,0,1)\). Now \(\sum z_i y_i=2+0+2=4\) and \(\sum z_i^2=2\), giving standardized slope \(\widehat{\theta}=4/(2+4)=2/3\). Its fitted values are \((-2/3,0,2/3)\). The original-unit slope is \(\widehat{\theta}/s=(2/3)/2=1/3\), which gives the same fitted values when multiplied by \(x=(-2,0,2)\).

Check. The original-scale equivalent objective penalizes \(\beta^2\) with weight \(\lambda s^2=4(2^2)=16\). It therefore gives slope \(8/(8+16)=1/3\), matching the standardized calculation. The common mistake avoided is to assume that a ridge fit is unchanged when predictor units change while leaving the same numerical \(\lambda\).

Calculating and Applying the Transformation

The transformation is part of the fitted model, not a separate calculation to redo for each new data set. For each predictor, calculate its mean and standard deviation from the training observations. Transform the training observations with those values, fit the model, and retain the values for future transformations. If a standard deviation is zero, the predictor is constant in the training data and cannot be standardized by division; it must be handled rather than divided by zero.

1
Calculate training summaries.
For each eligible numeric predictor, calculate its training-set mean and sample standard deviation.
2
Transform the training predictors.
Subtract the corresponding training mean and divide by the corresponding training standard deviation.
3
Fit and retain the transformation.
Fit the ridge model using the transformed training predictors, and keep every training mean and standard deviation with the fitted model.
4
Transform later observations consistently.
Use the retained training summaries on validation, test, and new observations; do not recalculate summaries on those data.

If a standardized-predictor model has intercept \(\alpha\) and slopes \(\theta_j\), its predictions can also be written in original predictor units. Since \(z_j=(x_j-\bar{x}_j)/s_j\), the original slopes are \(\beta_j=\theta_j/s_j\), and the original intercept is \(\beta_0=\alpha-\sum_j\theta_j\bar{x}_j/s_j\). This conversion preserves predictions; it does not turn the original-unit coefficients into the coefficients that would have been obtained by penalizing all original-unit slopes equally.

Worked Example: Transforming a New Observation Correctly

A hypothetical model uses two numeric predictors. In the training data, predictor \(x_1\) has values \((1,2,3)\), while \(x_2\) has values \((0,0,6)\). A ridge model fitted to standardized predictors has intercept \(\alpha=4\) and slopes \(\theta_1=1/2\), \(\theta_2=\sqrt{3}/2\). Transform the new observation \((x_1,x_2)=(4,6)\), calculate its prediction, and express the model coefficients in original units.

For \(x_1\), the training mean is \(\bar{x}_1=(1+2+3)/3=2\). Its sample variance is \(((1-2)^2+(2-2)^2+(3-2)^2)/2=2/2=1\), so \(s_1=1\). For \(x_2\), the training mean is \(\bar{x}_2=(0+0+6)/3=2\). Its sample variance is \(((-2)^2+(-2)^2+4^2)/2=24/2=12\), so \(s_2=\sqrt{12}=2\sqrt{3}\).

Apply those training summaries to the new observation. Its standardized values are \(z_1=(4-2)/1=2\) and \(z_2=(6-2)/(2\sqrt{3})=2/\sqrt{3}\). The prediction is \[ 4+\frac{1}{2}(2)+\frac{\sqrt{3}}{2}\frac{2}{\sqrt{3}} =4+1+1=6. \] The standardized second contribution is \(1\), because \(\sqrt{3}/\sqrt{3}=1\).

In original units, \(\beta_1=\theta_1/s_1=(1/2)/1=1/2\) and \(\beta_2=\theta_2/s_2=(\sqrt{3}/2)/(2\sqrt{3})=1/4\). The original intercept is \(\beta_0=\alpha-\beta_1\bar{x}_1-\beta_2\bar{x}_2=4-(1/2)(2)-(1/4)(2)=5/2\). Substituting the new observation into this equivalent model gives \(5/2+(1/2)(4)+(1/4)(6)=5/2+2+3/2=6\), checking the prediction.

Method and trap avoided. The method is to transform with training summaries and then use the standardized model, or to use the equivalent original-unit coefficients. Recalculating means or standard deviations using the new observation would define a different transformation and would not reproduce the fitted model's prediction.

Centering, the Intercept, and a Fully Checked Fit

Centering makes the intercept correspond to the fitted response at average predictor values when the predictors are centered. With an unpenalized intercept, fitting centered predictors makes the intercept equal to the response mean at the optimum: the residuals sum to zero because changing the intercept shifts every fitted value equally, while the penalty contains no intercept term. Centering alone does not make predictor scales comparable; scaling is also needed for that purpose.

The next example checks a two-predictor ridge calculation directly. It also illustrates why fitted values must be calculated from the stated coefficients and predictor rows rather than inferred from a pattern.

Worked Example: Checking Ridge Fits After Standardizing

A hypothetical centered response is \(y=(-5/2,-3/2,3/2,5/2)\). Two centered predictors have rows \((-1,-1),(-1,1),(1,-1),(1,1)\). Fit ridge regression with \(\lambda=2\), first using these predictors in their original units, then using predictors standardized by their sample standard deviations. The intercept is unpenalized.

Original-unit fit. Each predictor has sum of squares \(4\), and their cross-product is zero. Their cross-products with \(y\) are \(8\) and \(2\): for the first, \(5/2+3/2+3/2+5/2=8\); for the second, \(5/2-3/2-3/2+5/2=2\). The ridge normal equations are diagonal here, so the coefficients are \(8/(4+2)=4/3\) and \(2/(4+2)=1/3\). The intercept is zero because the response and both predictors have mean zero.

Calculate the fitted values row by row from these coefficients. They are \(-4/3-1/3=-5/3\), \(-4/3+1/3=-1\), \(4/3-1/3=1\), and \(4/3+1/3=5/3\). Thus the fitted-value vector is \((-5/3,-1,1,5/3)\). Subtracting these fitted values from \(y\) gives residuals \((-5/6,-1/2,1/2,5/6)\). The residual sum of squares is \[ \left(-\frac56\right)^2+\left(-\frac12\right)^2+\left(\frac12\right)^2+\left(\frac56\right)^2 =\frac{25}{36}+\frac{9}{36}+\frac{9}{36}+\frac{25}{36} =\frac{68}{36}=\frac{17}{9}. \]

Standardized fit. Each predictor's sample standard deviation is \(\sqrt{4/3}=2/\sqrt{3}\). Thus every predictor entry is multiplied by \(\sqrt{3}/2\). Each standardized predictor has sum of squares \(3\); their cross-product remains zero. The cross-products with \(y\) become \(4\sqrt{3}\) and \(\sqrt{3}\), because the original cross-products \(8\) and \(2\) are each multiplied by \(\sqrt{3}/2\). The standardized ridge coefficients are therefore \(4\sqrt{3}/(3+2)=4\sqrt{3}/5\) and \(\sqrt{3}/(3+2)=\sqrt{3}/5\).

The standardized fitted values, computed row by row, are \(-3/2,-9/10,9/10,3/2\). For example, the second row is \((-\sqrt{3}/2)(4\sqrt{3}/5)+(\sqrt{3}/2)(\sqrt{3}/5)=-6/5+3/10=-9/10\). The residuals are \((-1,-3/5,3/5,1)\), so the residual sum of squares is \(1+9/25+9/25+1=68/25\). As a check on the coefficient conversion, the standardized slopes correspond to original-unit slopes \((4\sqrt{3}/5)/(2/\sqrt{3})=6/5\) and \((\sqrt{3}/5)/(2/\sqrt{3})=3/10\). Their fitted values on the original predictors are the same \((-3/2,-9/10,9/10,3/2)\).

Check and common mistake avoided. The standardized predictors have sum of squares \(3\), not \(4\), because the sample standard deviation uses denominator \(n-1=3\). Also, the third original-unit fitted value is \((4/3)(1)+(1/3)(-1)=1\), not \(-1/3\). Using the wrong fitted value would give incorrect residuals and an incorrect residual sum of squares. The values above are checked directly against the coefficients and rows.

Common Traps and Practical Interpretation

A common mistake is to call a coefficient "larger" and infer that its predictor receives more shrinkage without checking units. For example, if one predictor is recorded in dollars and then changed to thousands of dollars, its numerical coefficient changes by a factor of one thousand. A shared penalty on the numerical coefficients does not remain the same penalty on the original relationship. The appropriate question is whether predictors were put on a comparable scale before applying a shared ridge penalty.

  • Do not standardize each data partition separately. Use training-set means and standard deviations for all later data. Separate transformations change the predictor values that the model receives.
  • Do not confuse centering with scaling. Centering changes the reference point and helps interpret the intercept; scaling addresses differences in predictor spread and units.
  • Do not divide by a zero standard deviation. A constant training predictor has no variation to scale and must be removed or otherwise handled.
  • Do not assume standardization means no shrinkage. The standardized coefficients are still penalized; the scale simply gives a common interpretation to a one-standard-deviation change.
  • Do not change the response transformation in this calculation. This tutorial concerns predictor units. Changing response units also changes the objective's relative penalty and is a separate issue.
Key takeaway: A shared squared-coefficient penalty is sensitive to predictor units because slopes are measured in those units. Center numeric predictors and divide by their training-set standard deviations before fitting ridge when a comparable penalty across predictor scales is intended; retain those training summaries and apply them unchanged to later observations.

Check Your Understanding

For each problem, use the stated ridge objective convention and check any coefficient conversion against the fitted prediction.

  1. A centered predictor has \(\sum x_i^2=10\), \(\sum x_i y_i=15\), and \(\lambda=5\). Find its ridge slope. If it is rescaled to \(x^*=3x\), find the slope per unit of \(x^*\) and the equivalent slope per unit of \(x\). (Answer: original slope \(1\); rescaled slope \(45/95=9/19\); equivalent original-scale slope \(27/19\).)
  2. A training predictor has values \((2,4,6)\). Find its mean and sample standard deviation, then standardize a new value of \(8\) using the training summaries. (Answer: mean \(4\), sample standard deviation \(2\), standardized new value \(2\).)
  3. A centered predictor has sample standard deviation \(s=3\), and a ridge model using its standardized version has slope \(\theta=1.2\). Find the equivalent original-unit slope and the penalty term in original slope units for \(\lambda=2\). (Answer: original slope \(0.4\); penalty \(18\beta^2\), since \(\lambda s^2=2(9)=18\).)
  4. A training predictor has mean \(10\) and standard deviation \(4\). A validation observation has value \(18\). What standardized value should be supplied to the fitted model, and why should the validation set's own mean not be used? (Answer: \(2\); use the training summaries so the validation value is on the same scale as the model's predictors.)
  5. A standardized-predictor model has intercept \(5\), slope \(\theta=2\), training mean \(\bar{x}=3\), and training standard deviation \(s=2\). Find the equivalent original-unit intercept and slope, then check the prediction at \(x=5\). (Answer: original slope \(1\), intercept \(2\); prediction \(2+1(5)=7\), matching \(5+2((5-3)/2)=7\).)
Clinical Biostats