Tutorials › Regression Analysis (Undergraduate) › Repeated K-Fold Cross-Validation

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

Repeated K-Fold Cross-Validation

See how to calculate and summarize repeated K-fold cross-validation scores, and how to interpret the variation among repetitions.

Intermediate 10 min read

What You'll Learn

  • Describe how repeated K-fold cross-validation creates new fold assignments while keeping the number of folds fixed.
  • Calculate a performance score for each repetition and combine scores into an overall estimate.
  • Weight fold-level errors correctly when fold sizes differ.
  • Interpret the units and meaning of a cross-validation performance estimate.
  • Distinguish variation across random partitions from variation across independent samples.
  • Identify conditions needed for a cross-validation score to represent the intended prediction task.

Why Repeat K-Fold Cross-Validation?

In K-fold cross-validation, each observation is used for validation once: the data are divided into \(K\) folds, and a model is fitted \(K\) times, each time leaving out a different fold. The result is a performance estimate based on predictions for all observations. But a particular random assignment to folds is only one way to divide the data. Another assignment can produce somewhat different training sets and validation scores.

Repeated K-fold cross-validation runs the K-fold procedure several times, using a new random fold assignment for each run. It provides a way to summarize how the estimated performance changes across those assignments. Repeating the procedure does not add new observations: every repetition reuses the same dataset.

Definition: In repeated K-fold cross-validation, a dataset of \(n\) observations is randomly partitioned into \(K\) folds for each of \(R\) repetitions. For each repetition, the prediction procedure is fitted \(K\) times, each time using \(K-1\) folds for training and the remaining fold for validation. The validation results from each repetition are combined into one score; the \(R\) repetition-level scores can then be summarized.

Conditions for Interpreting the Estimate

Before calculating or interpreting a cross-validation score, specify what prediction task it is meant to represent. Suppose the data are \((x_i,y_i)\), where \(x_i\) denotes the predictor information for observation \(i\) and \(y_i\) its outcome. The prediction procedure is the complete rule that uses training data to produce predictions. The target is its expected performance on new observations from a specified population, when trained on data of roughly the size used in each fold.

The usual interpretation relies on several conditions. The observed cases should be suitable representatives of the target population, and the way cases are assigned to folds should respect the study design. For example, if several rows belong to the same person, assigning that person's rows to different folds may not represent prediction for a new person. The validation cases must not contribute information to fitting the model or other data-dependent parts of the prediction procedure. Finally, the procedure and performance measure should be defined consistently across folds and repetitions.

These conditions are not guaranteed just because software reports a cross-validation score. They are part of deciding whether the score answers the intended question. Repetition changes the random partition, not the target population or the underlying dataset.

Calculating and Combining Scores

Let \(F_{rk}\) be the set of observations in fold \(k\) of repetition \(r\), where \(r=1,\ldots,R\) and \(k=1,\ldots,K\). Write \(\hat{y}_i^{(-r,k)}\) for the prediction for observation \(i\) from a model fitted without fold \(F_{rk}\). A loss function \(\ell(y,\hat y)\) measures the error of a prediction. For example, squared-error loss is \((y-\hat y)^2\), and absolute-error loss is \(|y-\hat y|\).

The mean loss for fold \(k\) of repetition \(r\) is the average loss over that fold. To combine the folds into a repetition-level score, weight each fold's mean loss by its fraction of the observations. This gives each validation observation equal weight, including when fold sizes differ.

$$ M_{rk}=\frac{1}{|F_{rk}|}\sum_{i\in F_{rk}}\ell\!\left(y_i,\hat{y}_i^{(-r,k)}\right), \qquad M_r=\sum_{k=1}^{K}\frac{|F_{rk}|}{n}M_{rk}. $$

Here \(M_r\) is the cross-validation score for repetition \(r\). The overall repeated K-fold estimate is the mean of those scores:

$$ \bar{M}_{\mathrm{CV}}=\frac{1}{R}\sum_{r=1}^{R}M_r. $$

For a loss such as mean squared error (MSE) or mean absolute error (MAE), smaller scores indicate better predictive performance. Their units differ: MSE has the square of the outcome's units, whereas MAE has the outcome's units. If the desired measure is root mean squared error (RMSE), take the square root of the relevant mean squared error. Averaging fold-level RMSE values is generally not the same as first combining squared errors and then taking the square root.

Worked Example: Combining Two Repetitions

Suppose a hypothetical dataset has \(n=12\) observations. We use \(K=3\) folds and \(R=2\) random partitions, with squared-error loss. Each fold contains four observations, so each model is trained on eight observations. The fold MSE values are \(10, 14, 12\) in the first repetition and \(8, 11, 13\) in the second.

All folds are the same size, so the repetition-level score is the simple mean of the three fold MSE values. For repetition 1:

$$ M_1=\frac{10+14+12}{3}=\frac{36}{3}=12. $$

For repetition 2:

$$ M_2=\frac{8+11+13}{3}=\frac{32}{3}\approx10.67. $$

The overall estimate is the mean of the two repetition-level scores:

$$ \bar{M}_{\mathrm{CV}}=\frac{12+10.67}{2}\approx11.33. $$

Thus, the repeated cross-validation estimate is an MSE of about \(11.33\) outcome-units squared. It describes the average validation error across these two partitions; it does not mean that the error for every future observation will be \(11.33\).

Why Weighting Matters When Fold Sizes Differ

Fold sizes may differ by one observation when \(n\) is not divisible by \(K\). In that case, the unweighted mean of fold scores gives a small fold the same influence as a larger fold. The weighted formula instead equals the mean loss over all validation observations in that repetition. To see why, substitute the fold score definition:

$$ M_r =\sum_{k=1}^{K}\frac{|F_{rk}|}{n} \left(\frac{1}{|F_{rk}|}\sum_{i\in F_{rk}}\ell_i\right) =\frac{1}{n}\sum_{k=1}^{K}\sum_{i\in F_{rk}}\ell_i =\frac{1}{n}\sum_{i=1}^{n}\ell_i. $$

In this derivation, \(\ell_i\) is the validation loss for observation \(i\) under the model fitted without its fold in repetition \(r\). The final equality holds because the folds partition the data: each observation appears in exactly one validation fold during that repetition. The predictions, and therefore the losses, can differ from one repetition to another, but each observation contributes once per repetition.

Worked Example: Unequal Fold Sizes and MAE

Consider a hypothetical dataset with \(n=10\) observations divided into three validation folds of sizes \(4,3,3\). In one repetition, the absolute prediction errors in the folds are \((1,2,3,2)\), \((3,5,4)\), and \((4,6,5)\). The fold MAEs are:

$$ M_{r1}=\frac{1+2+3+2}{4}=2,\qquad M_{r2}=\frac{3+5+4}{3}=4,\qquad M_{r3}=\frac{4+6+5}{3}=5. $$

The weighted repetition-level MAE is:

$$ M_r=\frac{4}{10}(2)+\frac{3}{10}(4)+\frac{3}{10}(5) =0.8+1.2+1.5=3.5. $$

Checking directly, the absolute errors sum to \(8+12+15=35\), and \(35/10=3.5\). In contrast, the unweighted mean of the three fold MAEs is \((2+4+5)/3\approx3.67\). That number gives the four-observation fold the same weight as either three-observation fold, so it is not the mean absolute error across the ten validation observations.

What Variation Across Repetitions Means

A useful summary can include both the overall mean score and the observed spread of the repetition-level scores. A sample standard deviation across the \(R\) scores describes how much those scores varied across the random partitions that were run. It is a descriptive measure of split-to-split variation for this dataset and this procedure.

Those scores are not results from independent replications of a study. Every repetition uses the same observed cases, and the fitted models and validation predictions overlap substantially across partitions. Consequently, the variation across repetitions should not automatically be treated as the standard error from \(R\) independent datasets, or used by itself to form a confidence interval for population performance. Repeating K-fold cross-validation can show sensitivity to fold assignment; it cannot replace collecting new independent data.

Worked Example: Describing Repeat-to-Repeat Spread

Suppose four hypothetical repetitions of a fixed five-fold cross-validation procedure produce MSE scores of \(5.2, 6.1, 5.7,\) and \(7.0\) outcome-units squared. Their mean is:

$$ \bar{M}_{\mathrm{CV}}=\frac{5.2+6.1+5.7+7.0}{4} =\frac{24.0}{4}=6.0. $$

The deviations from \(6.0\) are \(-0.8, 0.1, -0.3,\) and \(1.0\). Their squared deviations sum to:

$$ (-0.8)^2+(0.1)^2+(-0.3)^2+(1.0)^2 =0.64+0.01+0.09+1.00=1.74. $$

The sample standard deviation of the four scores is therefore:

$$ s_M=\sqrt{\frac{1.74}{4-1}} =\sqrt{0.58}\approx0.762. $$

This says that the observed repetition scores had a sample standard deviation of about \(0.762\) outcome-units squared around their mean of \(6.0\). It describes their spread across these four partitions. It does not establish that \(0.762/\sqrt{4}\) is a valid standard error for generalization performance, because the four scores are not independent replications.

Common Mistake: Mistaking Repeated Splits for New Data

A common mistake is to say, “I ran ten repetitions, so I have ten independent estimates of model performance.” The repetitions do give ten scores, but each score reuses the same observations. A different random partition changes which observations are grouped together, not which people or cases were sampled. The mean can be a more stable summary across the partitions tried, but the number of repetitions is not the number of independent samples.

Another mistake is to average fold scores without checking fold sizes. Use a plain mean only when the folds have equal sizes; otherwise, weight each fold score by its size divided by \(n\). In either case, report the performance measure and its units, the number of folds and repetitions, and the mean score. If reporting the spread across repetitions, label it as variation across random partitions rather than as uncertainty from independent replication.

Key takeaway: Repeated K-fold cross-validation averages validation performance over several random fold assignments of the same dataset. It can reveal sensitivity to the partition, but repeated partitions are not new independent samples and their spread should be interpreted accordingly.

Check Your Understanding

Use the definitions and calculations in this tutorial to answer the following questions.

  1. In repeated four-fold cross-validation with six repetitions, how many times is each observation used for validation in each repetition? Does it become a new observation in the next repetition?
  2. A repetition has fold sizes \(5,5,4\) and fold MAEs \(2,3,6\). Calculate the correctly weighted repetition-level MAE.
  3. Explain why a simple mean of fold scores gives the correct observation-level average when all fold sizes are equal.
  4. Four repetition-level scores are calculated from the same dataset. Why should they not automatically be treated as four independent replications?
  5. What does the sample standard deviation of repetition-level scores describe, and what does it not establish by itself?