Tutorials › Regression Analysis (Undergraduate) › Scaling Predictors Before Penalization

Ridge and Lasso Regression · Tutorial 669 of 991

Scaling Predictors Before Penalization

You will learn to standardize quantitative predictors before ridge or lasso, understand how scaling changes the penalty, and convert fitted coefficients back to the original units.

Advanced 9 min read

What You'll Learn

  • Define centering and standardizing a quantitative predictor using its sample mean and sample standard deviation.
  • Explain why a predictor’s measurement units affect a penalty on its coefficient.
  • Convert standardized-scale coefficients and intercepts to the original predictor units.
  • Check that changing coefficient units preserves fitted values when the intercept is transformed consistently.
  • Choose and describe a scaling convention for binary indicator variables.
  • Distinguish preprocessing choices from assumptions about the regression model.

Why Predictor Scale Matters

Ridge and lasso add a penalty on regression coefficients to the least-squares objective. But a coefficient’s numerical size depends on the units of its predictor. A slope measured in grams is numerically different from the slope for the same relationship expressed in kilograms. If coefficients are penalized without accounting for this, a change of units can change the penalty and therefore the fitted model. Scaling predictors puts quantitative variables on a comparable footing before penalization.

A linear model for the response is \(y_i=\beta_0+\sum_{j=1}^{p}\beta_jx_{ij}+\epsilon_i\), where \(y_i\) is the response for observation \(i\), \(x_{ij}\) is predictor \(j\), and \(\epsilon_i\) is the error. The fitted value is \(\hat y_i=\hat\beta_0+\sum_{j=1}^{p}\hat\beta_jx_{ij}\). The error term belongs in the model for the response, not in the fitted-value equation.

For clarity, use the penalized least-squares objectives \( \sum_{i=1}^{n}(y_i-\beta_0-\sum_{j=1}^{p}\beta_jx_{ij})^2+\lambda\sum_{j=1}^{p}\beta_j^2 \) for ridge and \( \sum_{i=1}^{n}(y_i-\beta_0-\sum_{j=1}^{p}\beta_jx_{ij})^2+\lambda\sum_{j=1}^{p}|\beta_j| \) for lasso. Here \(\lambda\) is nonnegative, and the intercept \(\beta_0\) is not penalized. These expressions show why scaling matters: the penalty acts on coefficient values, and those values depend on predictor units.

Definition: To standardize a quantitative predictor \(x_j\), calculate its sample mean \(\bar{x}_j\) and sample standard deviation \(s_j\), then replace each value by \(z_{ij}=(x_{ij}-\bar{x}_j)/s_j\). The standardized predictor has sample mean 0 and sample standard deviation 1, provided \(s_j>0\).

Centering subtracts the predictor mean; scaling divides by its standard deviation. Standardizing is therefore different from merely changing a unit such as centimeters to meters: standardizing uses the observed predictor distribution. It is a preprocessing choice, not an assumption that the predictor is normally distributed. A predictor with no variation has \(s_j=0\) and cannot be standardized this way; it provides no information for estimating a slope and must be handled separately.

How Scaling Changes the Penalty

Suppose a fitted model uses the standardized predictor \(z_{ij}=(x_{ij}-\bar{x}_j)/s_j\), with coefficient \(\theta_j\). Its contribution to the fitted value is \(\theta_jz_{ij}\). In original units, the same contribution is \(\beta_jx_{ij}\), where \(\beta_j=\theta_j/s_j\). Thus a coefficient on a predictor with a larger standard deviation is divided by a larger number when expressed per original unit.

The standardized coefficient is \(\theta_j=s_j\beta_j\). Consequently, penalizing standardized coefficients is equivalent to penalizing \(s_j\beta_j\) when written in original units. For ridge, the penalty becomes \(\lambda\sum_j(s_j\beta_j)^2\); for lasso, it becomes \(\lambda\sum_j|s_j\beta_j|\). Standardizing continuous predictors therefore makes the penalty compare coefficient sizes on the scale of a one-standard-deviation change, rather than on arbitrary original measurement units.

$$ \theta_j=s_j\beta_j,\qquad \text{ridge penalty}=\lambda\sum_{j=1}^{p}(s_j\beta_j)^2,\qquad \text{lasso penalty}=\lambda\sum_{j=1}^{p}|s_j\beta_j| $$

This does not make all predictors identical in importance or guarantee that their estimated coefficients will be equal. It removes one avoidable source of difference: the numerical units in which predictors happen to be recorded. The choice of which columns to standardize still matters, especially for indicator variables.

Worked Example: Equivalent Effects in Different Units

Suppose a hypothetical model has one quantitative predictor, length. A fitted relationship in centimeters has slope \(0.02\) response units per centimeter. The same relationship expressed in meters has slope \(2\) response units per meter, because one meter is 100 centimeters. The predictions are identical: a 100-centimeter increase contributes \(0.02(100)=2\), and a 1-meter increase contributes \(2(1)=2\).

Now compare the ridge penalty with \(\lambda=1\), ignoring the common residual sum of squares for this illustration. In centimeters, the squared-slope penalty is \(1(0.02)^2=0.0004\). In meters, it is \(1(2)^2=4\). The same physical relationship has a penalty 10,000 times larger when the predictor is recorded in meters.

For lasso, the corresponding penalties are \(1|0.02|=0.02\) and \(1|2|=2\), a factor of 100 difference. Therefore, fitting the same penalized procedure to differently scaled raw predictors can produce different fits even though the underlying information has not changed. Standardizing quantitative predictors avoids this particular unit dependence by expressing their effects per standard deviation.

Converting Coefficients Back to Original Units

Penalized fitting is often carried out using standardized predictors, while interpretation or reporting may require the original units. Converting back requires changing the intercept as well as the slopes. For standardized predictors \(z_{ij}=(x_{ij}-\bar{x}_j)/s_j\), suppose the fitted equation is \(\hat y_i=\hat\alpha_0+\sum_j\hat\theta_jz_{ij}\). Substituting the definition of \(z_{ij}\) gives the original-scale intercept and slopes.

$$ \hat\beta_j=\frac{\hat\theta_j}{s_j},\qquad \hat\beta_0=\hat\alpha_0-\sum_{j=1}^{p}\frac{\hat\theta_j\bar{x}_j}{s_j} $$

The original-scale fitted equation is \(\hat y_i=\hat\beta_0+\sum_j\hat\beta_jx_{ij}\). Because this conversion is an algebraic substitution, it preserves every fitted value, up to rounding. It does not undo the shrinkage introduced during penalized fitting: it expresses the same penalized fit using different coefficient units.

Worked Example: Converting a Standardized Slope

Suppose a hypothetical fitted model has one predictor, study time in hours. In the data, \(\bar{x}=20\) hours and \(s=10\) hours. The fitted standardized-scale equation is \(\hat y=50+8z\), where \(z=(x-20)/10\) and \(y\) is a test-score measure.

The original-scale slope is \(\hat\beta_1=\hat\theta_1/s=8/10=0.8\) score units per hour.

The original-scale intercept is \(\hat\beta_0=\hat\alpha_0-\hat\theta_1\bar{x}/s=50-8(20)/10=50-16=34\). Thus the equivalent equation is \(\hat y=34+0.8x\).

Check a prediction at \(x=30\) hours. The standardized value is \(z=(30-20)/10=1\), so the standardized equation gives \(\hat y=50+8(1)=58\). The original-scale equation gives \(\hat y=34+0.8(30)=34+24=58\). The identical prediction confirms that both coefficients and the intercept were converted consistently.

Handling Indicator Variables

A binary indicator has values 0 and 1, often representing membership in one of two categories. Unlike a continuous predictor, a one-unit change in an indicator already has a direct interpretation: it changes the category from 0 to 1. A common practice is to leave indicators coded 0 and 1 rather than standardize them. This keeps their coefficients on the category-contrast scale, while continuous predictors are standardized.

Leaving indicators unscaled is a convention, not a mathematical requirement. Since the penalty acts on coefficient values, including an unscaled indicator means its penalty is based on the 0-to-1 category contrast, while a standardized continuous predictor’s penalty is based on a one-standard-deviation change. That difference should be intentional and reported. Standardizing an indicator is possible, but changes the scale of its coefficient and the penalty it receives. Do not standardize category labels as if their numerical codes were quantitative measurements.

Worked Example: Converting a Model With an Unscaled Indicator

Consider a hypothetical fitted model with study hours \(x\) and an indicator \(d\), where \(d=1\) denotes an evening class and \(d=0\) denotes a morning class. Suppose study hours were centered at \(\bar{x}=20\) and scaled by \(s=10\), while the indicator was left as 0 or 1. The fitted equation is \(\hat y=50+8z+3d\), with \(z=(x-20)/10\).

The study-hours slope in original units is \(\hat\beta_x=8/10=0.8\) response units per hour. Its intercept adjustment is \(8(20)/10=16\). Since \(d\) was not centered or scaled, its coefficient remains 3 response units. The converted equation is \(\hat y=(50-16)+0.8x+3d=34+0.8x+3d\).

At 30 study hours, the predicted response is \(34+0.8(30)+3(0)=58\) for a morning-class indicator and \(34+0.8(30)+3(1)=61\) for an evening-class indicator. The fitted contrast is 3 response units at the same study hours. This describes the fitted association, not a causal effect of class time.

If the indicator had instead been centered and standardized, its coefficient would also need to be divided by its scaling factor, and the intercept would need the corresponding centering adjustment. The safest practice is to record, for every predictor, whether it was centered or scaled and by which values. Apply that same transformation when making predictions from new observations.

A Practical Scaling Workflow

Scaling must be applied consistently between model fitting and prediction. For each quantitative predictor, retain its training-data mean and standard deviation, then use those same values to transform later observations. Recalculating them on new observations changes the meaning of the fitted coefficients and can make predictions inconsistent with the fitted model.

1
Identify predictor types.
Separate quantitative variables from indicators and other categorical encodings. Check that each quantitative predictor varies across the fitting data.
2
Choose a convention.
For example, standardize quantitative predictors using their sample means and sample standard deviations, leave 0/1 indicators unchanged, and do not penalize the intercept.
3
Transform and fit.
Use the specified transformed predictors in the ridge or lasso objective. Record the means, standard deviations, and indicator coding used.
4
Interpret or predict consistently.
For original-unit reporting, convert slopes and intercept with the formulas above. For new data, apply the training-data transformations before calculating fitted values.

Derivation: Why the Intercept Must Change

For one standardized predictor, write the fitted equation as \(\hat y=\hat\alpha_0+\hat\theta(x-\bar{x})/s\). Distribute the slope across the numerator: \(\hat y=\hat\alpha_0+(\hat\theta/s)x-(\hat\theta\bar{x}/s)\). Collect the constant terms to obtain \(\hat y=(\hat\alpha_0-\hat\theta\bar{x}/s)+(\hat\theta/s)x\). Therefore the original-unit slope is \(\hat\theta/s\), and the original-unit intercept is \(\hat\alpha_0-\hat\theta\bar{x}/s\). With several standardized predictors, each predictor contributes its own intercept adjustment, so the adjustments add.

This derivation also shows why changing slopes alone is not enough. If the intercept is left at its standardized-scale value, the fitted value at a given original predictor value will generally change. The same substitution applies when some predictors are indicators left unscaled: those terms remain as written, while only the standardized predictors contribute to the intercept adjustment.

Common Mistakes and Why This Choice Matters

  • Penalizing raw coefficients without considering units. The reasoning “the model uses the same physical measurements, so changing units cannot matter” overlooks that the penalty uses numerical coefficient values. Convert units and the coefficients—and hence the penalty—change. Standardize quantitative predictors or otherwise define a deliberate penalty scaling.
  • Converting slopes but not the intercept. This generally changes predictions. Use the full intercept formula and verify at least one prediction by both equations.
  • Standardizing new data separately. This changes the transformation used by the fitted model. Apply the training means and standard deviations to new observations.
  • Treating a 0/1 indicator as an ordinary continuous measurement. Its contrast has a category interpretation. Decide explicitly whether to leave it unscaled or scale it, and explain the resulting penalty convention.
  • Assuming standardized coefficients are in original units. A standardized-scale coefficient describes a change per standard deviation of its predictor. Divide by that predictor’s standard deviation to express the slope per original unit.

Standardization makes penalization less sensitive to arbitrary units; it does not determine which predictors belong in a model, remove correlation among predictors, or make penalized coefficients ordinary least-squares estimates. The essential reporting details are the predictor transformations, how indicators were treated, whether the intercept was penalized, and the scale in which reported coefficients are expressed.

Key takeaway: Standardize quantitative predictors so ridge or lasso penalizes effects on comparable predictor scales. To return to original units, divide each standardized slope by its predictor’s standard deviation and adjust the intercept for every centered predictor. State separately how indicator variables were handled.

Check Your Understanding

Try these questions without looking back.

  1. A predictor is standardized using \(\bar{x}=12\) and \(s=4\). What is its standardized value when \(x=20\), and what does that value mean?
  2. Explain why the same physical relationship can have different ridge penalties when a predictor is recorded in meters rather than centimeters.
  3. A one-predictor standardized fitted equation is \(\hat y=30+6z\), with \(\bar{x}=5\) and \(s=2\). Find the equivalent original-scale slope and intercept.
  4. Why must the intercept change when standardized predictors are converted back to their original units?
  5. Give one reason to leave a 0/1 indicator unscaled and one point that should be reported if it is left unscaled.
  6. When predicting for new observations, which means and standard deviations should be used to standardize quantitative predictors?