Method · Statistics Library

Cross-validated model comparison

K-fold (or leave-one-out) cross-validation compares candidate model families by their held-out prediction error, and the one-standard-error rule then picks the simplest model whose score is not meaningfully worse than the best.

Topic indexA reference reading outside the guided chapter sequence

Understand the idea

A model's error on the SAME data it was trained on is optimistic: a flexible enough model can essentially memorize the training rows and look perfect, then fall apart on anything new. This is like studying with the answer key already in hand and calling that a fair exam. Cross-validation gives a fairer exam: split the rows into folds, hold one out as the 'unseen' test, train on the rest, score it, then rotate which fold is held out until every row has had its turn being the test set.

Averaging the held-out scores across folds gives a much more honest picture of how the model would do on genuinely new data than any in-sample number ever could.

Once several candidate models are scored this way, it's tempting to just pick whichever has the single lowest error. But cross-validated scores are themselves estimates with their own uncertainty (a standard error). The one-standard-error rule starts from the BEST model's score and that model's own standard error: any model whose score lands within one standard error of the best is not clearly separated from it by this rule of thumb, and among those the SIMPLER one is preferred. This guards against chasing a difference so small it may be just noise. It is a rule of thumb, not a significance test: no test of the gap between two models is run.

k-fold CV splits the data into k parts, trains on all but one of them and tests on the one left out, rotating until each part has been the test set once. Leave-one-out CV is the extreme case where each part is a single row. Comparing cross-validated scores across candidate models is a sound basis for model selection.

Practitioner explanation

KernelStats compares a fixed candidate set by k-fold (default k = 5, a shuffle seeded with 42 for reproducibility) or leave-one-out cross-validation: for regression, Lasso, Ridge, OLS, and KNN scored by CV RMSE; for classification, a Ridge classifier, Logistic regression, and KNN scored by CV accuracy. Every candidate is wrapped as Pipeline(StandardScaler, estimator), so the scaler is refit INSIDE each training fold: the held-out fold never leaks into preprocessing, avoiding a classic and easy-to-miss source of optimistically-biased CV scores.

Each candidate carries a fixed complexity rank used only for the one-standard-error tie-break: for regression, Lasso < Ridge < OLS < KNN; for classification, Ridge < Logistic < KNN, from most to least regularized/simplest. The one-SE band is every model within one standard error of the best score; KernelStats reports the single simplest model inside that band as the recommended pick, with ties inside the band broken by the raw CV score.

Two limitations are stated directly rather than glossed over: ordinary k-fold assumes the rows are exchangeable, and this function always uses a seeded random shuffle-split (stratified by class for classification) with no grouping or blocking option; a time-ordered series or clustered/grouped data needs Forecast Backtest (Rolling-Origin) or a blocked/grouped split instead, neither of which this function performs. And because the SAME folds are used here to both score and choose the winner, tuning the ridge_alpha/lasso_alpha/knn_k options by eye against this same output makes the winning score optimistically biased; an honest estimate of the tuned model's performance needs a further, nested round of cross-validation that this single pass does not run automatically.

When to use it

Consider it when

You have one outcome and a predictor set and want to choose among several model families (or check whether a flexible model is worth its complexity) by honest out-of-sample error, rather than by in-sample fit.

Use caution when

The rows are time-ordered (use the forecast backtest instead, since ordinary CV assumes exchangeable rows), the data are clustered or grouped in a way plain k-fold would leak across, or you already know which single model you want to fit and just need to interpret its coefficients (use linear or logistic regression directly).

Formulas and symbols

k-fold cross-validation score

CV(k)=1k∑i=1kRMSEi\text{CV}_{(k)} = \frac{1}{k}\sum_{i=1}^{k} \text{RMSE}_i

RMSE_i the held-out error on fold i (classification averages accuracy instead). Estimates the out-of-sample error of the model family.

Standard error of the CV score

SE(CV)=sd(fold scores)k\text{SE}(\text{CV}) = \frac{\text{sd}(\text{fold scores})}{\sqrt{k}}

The fold-to-fold uncertainty of ONE model's own CV score; it is not a test of the difference between two models (their fold scores come from the same folds and are correlated). A gap smaller than this is not clearly separated by the one-standard-error rule; no significance test is run.

One-standard-error band

CV≤CVmin⁡+SE(CVmin⁡)\text{CV} \le \text{CV}_{\min} + \text{SE}(\text{CV}_{\min})

Built from the best model's score and its own standard error. Every candidate whose score falls inside this band is not clearly separated from the best by the one-standard-error rule, and the simplest model in the band is the recommended pick (ISLR §6.1).

Assumptions and checks

  • Independence of observations. A random k-fold split lets information leak across time or across a cluster/group boundary (e.g. repeated measurements on the same subject landing in both the training and test fold), so the reported CV error is optimistically biased and does not reflect true out-of-sample performance for that structure.

    In KernelStats: Not checked by KernelStats: the engine always applies a seeded random (or exhaustive, for LOOCV) row split and does not detect time ordering or grouping structure among the predictors; a time-indexed or clustered dataset needs to be recognized by the analyst, who should use Forecast Backtest (Rolling-Origin) for a time series instead.

Alternatives

  • Forecast Backtest (Rolling-Origin). The rows are time-ordered (a forecasting problem) rather than exchangeable: use rolling-origin backtesting instead of ordinary k-fold, since random folds would leak information across time.
  • Stepwise Model Selection. You want automatic predictor selection within a single model family, rather than comparing several whole model families against each other.

Worked example

Scenario

Predicting penguin body mass from bill length and flipper length, comparing Lasso, Ridge, OLS, and KNN (k=5) by 5-fold CV RMSE with a seeded split.

Result

Mean CV RMSE ± SE across folds: Lasso 392.6 ± 16.2 g, Ridge 392.6 ± 16.3 g, OLS 392.6 ± 16.2 g, KNN 384.2 ± 8.3 g. KNN is best, so the one-SE band is RMSE <= 384.2 + 8.3 = 392.5 g; the three linear models miss it by about 0.1 g, so the band holds only KNN and the rule picks KNN.

Interpretation

The rule is mechanical: it uses only the best model's standard error, so a 0.1 g miss sends the pick to KNN even though an 8 g difference on errors of about 390 g, with fold-to-fold standard errors of 8-16 g, is practically nothing. The honest reading is that all four candidates predict about equally well; the analyst can reasonably prefer the interpretable linear model, saying so explicitly rather than presenting the automatic pick as a verdict.

Dataset: penguins.csv

Common misconceptions

Misconception

The model with the numerically lowest CV score is definitely the better model.

Correction

CV scores are noisy estimates. A gap smaller than one standard error of the best score is not clearly separated by the one-standard-error rule, which is exactly the gap the rule is built to catch: it prefers the simplest model inside the band rather than chasing the lowest number on the page. The rule is a heuristic, not a significance test, so even a larger gap is a signal to weigh, not proof that one model is better.

Misconception

The winning cross-validated model is 'the true model' for this problem.

Correction

Cross-validation only ranks the CANDIDATES that were actually compared, on this particular sample. It says nothing about a model family that wasn't included, and a winning predictive model is not automatically a causally correct description of how the outcome is generated.

References

  1. An Introduction to Statistical Learning, James, G., Witten, D., Hastie, T. and Tibshirani, R. (2021), 2nd ed., ch. 5 and §6.1.
  2. The Elements of Statistical Learning, Hastie, T., Tibshirani, R. and Friedman, J. (2009), 2nd ed., §7.10.