Tutorials › Regression Analysis (Undergraduate) › A Complete Penalized Prediction Analysis

Ridge and Lasso Regression · Tutorial 691 of 991

A Complete Penalized Prediction Analysis

Learn how to carry out a leakage-free penalized prediction workflow, choose a tuning value, evaluate held-out predictions, and describe fitted coefficients without overstating what they mean.

Advanced 11 min read

What You'll Learn

  • Standardize predictors using only the data available to each fitting step.
  • Compare candidate penalty values using fold-based prediction loss.
  • Apply the one-standard-error rule to select a more regularized model when appropriate.
  • Refit the selected lasso model on all development data before test-set evaluation.
  • Calculate and interpret test-set MSE, RMSE, and MAE in context.
  • Describe penalized coefficients cautiously, including the limits of coefficient and selection interpretations.

From Tuning to an Honest Prediction Check

A penalized regression analysis is not complete when a tuning parameter has been chosen. A useful prediction workflow must also keep held-out data out of every fitting decision, fit the selected model on the available development data, and evaluate predictions on cases that played no role in model selection. Each step answers a different question: cross-validation helps select a model, while a separate test set checks how well that completed process predicts new cases.

This tutorial follows a hypothetical task: predicting a student's end-of-term assessment score from study hours, recorded absences, and hours of sleep. The outcome is measured in points. The purpose is prediction, not a claim that changing any predictor would cause a change in score. The arithmetic below illustrates a complete workflow; the example data are fictional.

Definition: The development data are the cases used to prepare and tune a model. The test data are held apart until those choices are complete and are used for a final assessment of prediction error. A prediction analysis is leakage-free when information from a held-out case does not influence the fitted model, predictor transformations, or tuning decisions used to predict that case.

Specify the Model and the Workflow

Let \(y_i\) be the assessment score for case \(i\), and let \(x_{ij}\) be the value of predictor \(j\) for case \(i\), where \(j=1,\ldots,p\). For a lasso model, predictors are standardized and the response is kept in its original point scale. A common objective is

$$ \frac{1}{2n}\sum_{i=1}^{n} \left(y_i-\beta_0-\sum_{j=1}^{p}\beta_j z_{ij}\right)^2 +\lambda\sum_{j=1}^{p}|\beta_j|. $$

Here \(z_{ij}=(x_{ij}-\bar{x}_j)/s_j\), using the mean and sample standard deviation calculated from the data used to fit that particular model. The intercept \(\beta_0\) is not penalized. The tuning parameter \(\lambda\) controls the penalty: larger values impose more shrinkage, and lasso coefficients can be exactly zero. The numerical value of \(\lambda\) is meaningful only alongside the objective-function convention and predictor scaling used by the software.

For a reliable assessment, the test cases should represent the cases for which predictions are intended, and their outcomes must remain unavailable during model development. Predictors used at prediction time must also be available then. If several records come from the same person, school, or other shared unit, splitting individual records across development and test sets can make the test set too easy; the split should respect the unit relevant to future prediction. A random split is not automatically appropriate for time-ordered prediction, where later cases should generally be held out.

1
Set aside test data.
Do this before exploring tuning results. Do not use test outcomes to choose predictors, a penalty value, or a model.
2
Tune within development data.
For each cross-validation split, estimate predictor means and scales from that split's training portion alone, fit each candidate model, and score its validation portion.
3
Choose a penalty value and refit.
Use the cross-validation results and a stated selection rule, then refit the selected model using all development cases.
4
Evaluate once on the test set.
Apply the development-data scaling and fitted model to the test predictors, then compare predictions with the test outcomes.

Standardization Must Follow the Data Split

Standardizing predictors is useful because a penalty should not depend merely on whether a variable is recorded in hours or minutes. But calculating standardization constants using validation or test cases allows information from those cases into the modeling pipeline. Even though an outcome is not used to calculate a predictor mean, the transformation would still have been informed by held-out data.

Worked Example: Standardizing Within a Cross-Validation Fold

Suppose one training portion of a fold contains the following study-hour values: 2, 4, 6, and 8. Its absences values are 0, 1, 1, and 2. A held-out validation case has 5 study hours and 0 absences. Find the fold-specific standardized values for that case, using the sample standard deviation.

For study hours, the training mean is \(\bar{x}=(2+4+6+8)/4=5\). The sum of squared deviations is \(9+1+1+9=20\), so \(s=\sqrt{20/(4-1)}=\sqrt{6.6667}\approx2.582\). The validation value is therefore standardized as \(z=(5-5)/2.582=0\).

For absences, the training mean is \(\bar{x}=(0+1+1+2)/4=1\). The squared deviations sum to \(1+0+0+1=2\), giving \(s=\sqrt{2/3}\approx0.8165\). The validation value is \(z=(0-1)/0.8165\approx-1.225\).

Thus this case enters the validation prediction with standardized values 0 for study hours and about \(-1.225\) for absences. The calculation uses only the fold's training cases; the validation case is transformed afterward using those fixed training means and scales. Other folds generally have different means and scales.

Use Cross-Validation to Compare Penalty Values

Suppose development data are divided into five folds and a grid of candidate \(\lambda\) values is specified in advance. For each candidate, fit the model on four folds and calculate mean squared prediction error on the remaining fold, repeating until each fold has been held out once. The cross-validation estimate for a candidate is the average of its five fold losses. For this example, the response is scored in points, so these losses are in squared points.

Standardization must be repeated inside each fold: calculate the means and sample standard deviations on the four training folds, transform their predictors, fit the model, then use those same constants to transform the held-out fold. This estimates the performance of the whole modeling procedure rather than a model that has already seen information from its validation cases.

Worked Example: Applying the One-Standard-Error Rule

A hypothetical five-fold analysis gives the following validation mean squared errors. Each entry is one fold's loss in squared assessment-score points.

\(\lambda\)Fold 1Fold 2Fold 3Fold 4Fold 5Mean loss
0.02121513141614.00
0.08101311121412.00
0.2010121191311.00
0.5011121212.51011.50

The minimum average loss occurs at \(\lambda=0.20\). To calculate the standard error used in the one-standard-error rule, first find the sample standard deviation of its five fold losses. Their mean is 11, and the deviations are \(-1,1,0,-2,2\). The sum of squared deviations is \(1+1+0+4+4=10\), giving

$$ s_L=\sqrt{\frac{10}{5-1}}=\sqrt{2.5}\approx1.581, \qquad SE(\bar{L})=\frac{1.581}{\sqrt{5}}\approx0.7071. $$

The one-standard-error cutoff is \(11+0.7071=11.7071\) squared points. The mean loss at \(\lambda=0.50\), 11.50, is below that cutoff. It is the largest candidate \(\lambda\) in the table within one standard error of the minimum, so the one-standard-error rule selects \(\lambda=0.50\). This is a rule for preferring a more regularized candidate whose estimated loss is close to the minimum; it does not establish that the candidates have equal predictive performance. Fold losses are also not independent observations, so this standard error is a practical selection summary, not a formal confidence interval.

Refit, Predict, and Evaluate on the Test Set

After choosing \(\lambda\), refit the model using all development cases, recalculating each predictor's mean and sample standard deviation from those cases. The final model uses those constants for every later prediction. Do not average the fold-specific coefficients or reuse one fold's scaling constants: the final fit is a new fit to the full development set.

Common summaries compare test outcomes \(y_i\) with their predictions \(\hat{y}_i\). Mean squared error (MSE) is the average squared prediction error, root mean squared error (RMSE) is its square root, and mean absolute error (MAE) is the average absolute prediction error. RMSE and MAE are in the response's units; MSE is in squared response units. Squaring errors gives relatively greater weight to large misses, while absolute errors are less sensitive to their magnitude.

Definition: For \(m\) test cases, \(\mathrm{MSE}=\frac{1}{m}\sum_{i=1}^{m}(y_i-\hat{y}_i)^2\), \(\mathrm{RMSE}=\sqrt{\mathrm{MSE}}\), and \(\mathrm{MAE}=\frac{1}{m}\sum_{i=1}^{m}|y_i-\hat{y}_i|\). These are descriptive summaries of the observed test-set prediction errors; they do not guarantee the same error on every future sample.

Worked Example: Calculating Test-Set Prediction Error

Suppose the final lasso fit, selected and refit as described above, has this hypothetical output. Predictors are standardized using the full development data, the response remains in assessment-score points, and \(\lambda=0.50\).

$$ \hat{y}=68+4.2z_{\text{study}}-3.0z_{\text{absence}}+0z_{\text{sleep}}. $$

The table gives four test cases. Their standardized values are already calculated using the development-data means and scales. The test outcomes were not used in fitting or tuning.

Case\(z_{\text{study}}\)\(z_{\text{absence}}\)\(z_{\text{sleep}}\)Observed score \(y\)
110074
201-163
3-10166
40.5-1070

The fitted predictions are \(68+4.2(1)-3(0)+0(0)=72.2\), \(68+4.2(0)-3(1)+0(-1)=65.0\), \(68+4.2(-1)-3(0)+0(1)=63.8\), and \(68+4.2(0.5)-3(-1)+0(0)=73.1\) points. Defining prediction error as observed minus predicted gives errors \(1.8,-2.0,2.2,-3.1\) points.

Their squared errors are \(3.24,4.00,4.84,9.61\), summing to 21.69 squared points. Therefore

$$ \mathrm{MSE}=\frac{21.69}{4}=5.4225\text{ points}^2, \qquad \mathrm{RMSE}=\sqrt{5.4225}\approx2.329\text{ points}. $$

The absolute errors sum to \(1.8+2.0+2.2+3.1=9.1\), so \(\mathrm{MAE}=9.1/4=2.275\) points. In this hypothetical test set, predictions miss observed scores by an average absolute amount of 2.275 points; the RMSE is 2.329 points, with more weight on the larger errors. These values summarize these four cases and should not be presented as guaranteed future performance.

Interpret Penalized Coefficients With Care

In the displayed model, the study coefficient of 4.2 means that, within this fitted penalized equation, a one-sample-standard-deviation increase in study hours corresponds to a 4.2-point increase in the model's prediction when the other standardized predictors are held fixed. The absence coefficient is \(-3.0\) points per one sample standard deviation. These are descriptions of the fitted prediction rule, not evidence that study hours cause higher scores or absences cause lower scores.

The zero sleep coefficient means that this lasso fit assigns sleep no contribution in its current representation. It does not prove that sleep is unrelated to scores or unimportant in another sample or model. With correlated predictors, a lasso can retain one variable and shrink another even when both contain overlapping predictive information. Nor should these penalized estimates be given ordinary least-squares standard errors or treated as if the model had been selected independently of the data.

The test-set loss evaluates the predictions from the complete selection-and-fitting workflow. It does not validate a causal interpretation, prove that individual coefficients are stable, or show that the selected variables are uniquely important. If the test results prompt changes to the model, the test set has become part of development; a fresh untouched test set would then be needed for an independent final assessment.

Common Mistake: Letting the Test Set Choose the Model

A tempting but incorrect argument is: “I tried several \(\lambda\) values and predictor sets, and the one with the lowest test MSE is best, so I will report that test MSE as an unbiased check.” This uses the test outcomes to select the reported model. The lowest observed loss is favored partly because of chance variation, so the test result no longer provides an untouched evaluation of the selection process.

Instead, choose the tuning rule and model using development data, including cross-validation within that data. Refit the chosen model on all development cases, then calculate the test loss once for that completed procedure. If several modeling decisions are made after examining test performance, report that the set has become development data rather than presenting it as an independent final check.

Key takeaway: Keep the test set out of preprocessing, tuning, and model selection. Standardize within each training split, choose \(\lambda\) using development data, refit on all development cases, and then report test-set errors in the response's units. Interpret penalized coefficients as features of a fitted prediction rule, not as causal effects or ordinary inferential estimates.

Check Your Understanding

Use the workflow and definitions above to answer these questions.

  1. Why must predictor means and standard deviations be recalculated within each cross-validation training portion?
  2. In the tuning example, what is the minimum-loss \(\lambda\), and why does the one-standard-error rule select a different value?
  3. When refitting after tuning, which cases are used to calculate the final predictor means and scales?
  4. If a test set has an RMSE of 3 points, what does that summary say, and what does it not guarantee?
  5. What does a zero lasso coefficient establish about a predictor in the fitted model, and what does it not establish about the predictor more generally?