Tutorials › Regression Analysis (Undergraduate) › Constructing a Holdout Evaluation

Train-Test Splits and Cross-Validation · Tutorial 743 of 991

Constructing a Holdout Evaluation

Work through a holdout evaluation from assigning observations to sets through fitting the regression and calculating test-set error.

Intermediate 10 min read

What You'll Learn

  • Define disjoint training and test sets for a holdout evaluation
  • Fit a regression model using training observations only
  • Calculate test-set predictions without refitting the model
  • Compute MAE, MSE, and RMSE from held-out outcomes
  • Check when a random split represents the prediction setting of interest
  • Recognize how using test outcomes to change a model compromises the evaluation

From a Data Set to a Holdout Evaluation

A regression model can fit the data used to estimate it well and still predict new observations poorly. To assess prediction on cases not used to fit the model, set aside some observations before fitting. The set-aside cases form the test set; the remaining cases form the training set. A holdout evaluation fits on the training set and scores predictions on the untouched test set.

The separation is essential: if a test outcome influences the fitted model or a choice made about it, the resulting score is no longer an evaluation on untouched cases. A holdout score is an estimate of predictive performance for a model fitted to this particular training set, when predictions are made for cases from the intended setting.

Definition: Let the observed data contain \(n\) cases with predictors and outcomes. A holdout split partitions their indices into a training set \(I_{\mathrm{train}}\) and a test set \(I_{\mathrm{test}}\), such that the sets have no indices in common and together contain all \(n\) indices. Fit the specified regression procedure using cases in \(I_{\mathrm{train}}\) only. For each test case \(i\), calculate a prediction \(\hat{y}_i\) without using its observed outcome \(y_i\). A test-set performance measure is a summary of the differences between \(y_i\) and \(\hat{y}_i\) over the test cases.

For a quantitative outcome, common summaries use the prediction error \(e_i=y_i-\hat{y}_i\). The sign records whether the prediction was too low or too high; error measures based on absolute values or squares summarize its size.

Common test-set measures: If the test set contains \(m\) cases, the mean absolute error (MAE), mean squared error (MSE), and root mean squared error (RMSE) are
$$ \mathrm{MAE}=\frac{1}{m}\sum_{i\in I_{\mathrm{test}}}|y_i-\hat{y}_i|,\qquad \mathrm{MSE}=\frac{1}{m}\sum_{i\in I_{\mathrm{test}}}(y_i-\hat{y}_i)^2,\qquad \mathrm{RMSE}=\sqrt{\mathrm{MSE}}. $$
MAE and RMSE have the same units as the outcome. MSE has squared outcome units. These are averages over test cases, so their denominator is \(m\), not a degrees-of-freedom adjustment.

Constructing the Split and Fitting the Model

A straightforward holdout evaluation follows a fixed sequence. Decide which observations the prediction task concerns, randomly assign cases to the two sets, and keep the test outcomes aside while fitting. The split should be made at the level of the unit for which predictions are intended. If the intended task is to predict outcomes for new people, for example, randomly separating individual records may be unsuitable when several records belong to the same person.

A random split is useful when the cases are exchangeable for the intended task: in particular, test cases should be drawn from the same kind of setting as the cases to which the reported performance is meant to apply. Random allocation alone cannot make a test set representative of a different population or a different prediction setting.

1
Specify the prediction procedure.
Identify the outcome, predictors, and regression fitting method to be evaluated. Check that the training data will allow that method to be fitted; for a simple least-squares line with an intercept, for example, training predictor values must not all be identical.
2
Randomly partition the cases.
Assign each case to exactly one set. Keep the sets disjoint, and do not use test outcomes to make the assignment.
3
Fit using the training set.
Estimate the regression coefficients using only training cases. Calculate test predictions from their predictor values and this fitted model; do not refit using test outcomes.
4
Calculate and report test error.
Compare each test prediction with its observed outcome, calculate the chosen measure or measures, and state their units and the cases to which the evaluation applies.

The fitting method and any data-dependent choices that affect the model must be determined without using test outcomes. For example, fitting a line on the training set and then changing its coefficients after looking at test errors means the reported score is not an untouched holdout evaluation. The same principle applies to using test outcomes to select predictors or decide between alternative fitted models.

Worked Example: Fit a Line and Score Two Held-Out Cases

Suppose six hypothetical observations relate study time \(x\), in hours, to a quiz score \(y\), in points. A random assignment puts the first four observations in the training set and the last two in the test set.

CaseStudy time \(x\) (hours)Quiz score \(y\) (points)Set
112Training
223Training
335Training
444Training
556Test
667Test

Fit the least-squares line with an intercept using only cases 1–4. Their means are \(\bar{x}=2.5\) hours and \(\bar{y}=3.5\) points. The slope is

$$ b_1=\frac{\sum(x_i-\bar{x})(y_i-\bar{y})}{\sum(x_i-\bar{x})^2} =\frac{4}{5}=0.8\ \text{points per hour}. $$

The intercept is \(b_0=\bar{y}-b_1\bar{x}=3.5-(0.8)(2.5)=1.5\) points. Thus the fitted model is \(\hat{y}=1.5+0.8x\). The training predictor values include more than one distinct value, so the line can be fitted.

Apply that unchanged line to the two test predictor values. At \(x=5\), the prediction is \(1.5+(0.8)(5)=5.5\) points, giving error \(6-5.5=0.5\) points. At \(x=6\), the prediction is \(1.5+(0.8)(6)=6.3\) points, giving error \(7-6.3=0.7\) points. Therefore,

$$ \mathrm{MAE}=\frac{|0.5|+|0.7|}{2}=0.6\ \text{points}, \qquad \mathrm{MSE}=\frac{(0.5)^2+(0.7)^2}{2} =\frac{0.25+0.49}{2}=0.37\ \text{points}^2, $$

and \(\mathrm{RMSE}=\sqrt{0.37}=0.608\) points. For these two held-out cases, the average absolute prediction error is 0.6 quiz points. This result describes this evaluation; it does not establish that future prediction errors will have exactly that size.

Why the Test Average Is an Evaluation

The test-set measure averages losses on cases that did not determine the fitted coefficients. To see why that matters, consider a fitted model as fixed and suppose the test cases are independent draws from the prediction setting. For a loss function \(L(y,\hat{y})\), define the model's expected prediction loss as the mean loss for a new case from that setting. Each test loss then has this same expectation. By linearity of expectation, the average of \(m\) test losses has that expectation too:

$$ E\left[\frac{1}{m}\sum_{i\in I_{\mathrm{test}}}L(y_i,\hat{y}_i) \;\middle|\;\text{fitted model}\right] = \frac{1}{m}\sum_{i\in I_{\mathrm{test}}} E[L(y_i,\hat{y}_i)\mid\text{fitted model}] = E[L(Y_{\mathrm{new}},\hat{Y}_{\mathrm{new}})\mid\text{fitted model}]. $$

This justification depends on the test cases representing the intended prediction setting. It explains the score as an estimate of average loss for the fitted model, not as a guarantee about every future case. It also does not mean the estimate is exact: a different random test set could produce a different score. A single holdout evaluation gives one estimate for one fitted model and one split.

For ordinary least-squares point predictions, calculating a test MAE or MSE does not require normally distributed errors. The test outcomes are compared with predictions; the calculation itself is an average of observed losses. This does not remove the need for a suitable sampling design, a meaningful target population, or an appropriate fitted regression model.

Worked Example: Calculate Several Scores From Test Predictions

In a hypothetical evaluation of a model for daily water use, the regression was fitted using training cases only. On three untouched test cases, the observed use and predictions are as follows. The unit is litres per household per day.

Test caseObserved \(y_i\) (litres)Predicted \(\hat{y}_i\) (litres)Error \(y_i-\hat{y}_i\) (litres)
11091
21214-2
315141

The absolute errors are 1, 2, and 1 litres, so \(\mathrm{MAE}=(1+2+1)/3=1.333\) litres per household per day. The squared errors are 1, 4, and 1 litres squared, giving \(\mathrm{MSE}=(1+4+1)/3=2\) litres squared. Thus \(\mathrm{RMSE}=\sqrt{2}=1.414\) litres per household per day.

MAE and RMSE are on the original outcome scale; MSE is not. The second case contributes more to MSE because its error is squared: an error of 2 contributes 4, while an error of 1 contributes 1. The measures therefore summarize the same test predictions in different ways.

Common Pitfall: Letting the Test Set Change the Model

A common mistake is to inspect test errors, adjust the regression, and then report the improved error on those same cases as if they were still untouched. For instance, if one test case has a large residual, a person might add a predictor or alter the model until that case is predicted more closely. The test outcomes have now influenced the fitted procedure. The score on those cases no longer provides the same independent check of predictions as before.

The correction is to preserve the test outcomes for evaluation: fit and make model-related choices without them, calculate predictions on the test cases, and report the chosen test measure. If test results are used to alter the procedure, the score from that test set should not be presented as an untouched evaluation of the altered procedure.

Key takeaway: A holdout evaluation requires more than calculating residuals. The cases must be separated before fitting, the model must be fitted using training data only, and test outcomes must remain uninvolved in model choices. The resulting test-set error estimates predictive performance for the fitted model in the setting represented by the test cases.

Check Your Understanding

Answer each question using the definitions and procedures in this tutorial.

  1. What condition must the training and test index sets satisfy, and why must the test outcomes be excluded from fitting?
  2. A regression gives test errors of \(-1\), \(2\), and \(1\) units. Calculate the MAE and MSE.
  3. Why does RMSE have the same units as the outcome, while MSE has squared units?
  4. Does a low test-set MAE guarantee small errors for every future case? Explain.
  5. A model is changed after its test errors are inspected. Can its score on that same set still be described as an untouched holdout evaluation of the changed model? Why or why not?