Automated model comparison: choosing a model by cross-validation

Updated

Automated model comparison fits several candidate models to the same data and ranks them by how well they predict rows they were not fitted on, usually with k-fold cross-validation. It guards against choosing the model that only memorizes the data, and it shows whether a complex model really beats a simple one by enough to be worth it. The final choice is still yours: accuracy is one criterion, and how the model will be used is another.

When to use it

  • You want to predict a response from process data and do not know which kind of model suits it.
  • You need evidence that a machine learning model earns its complexity over a regression equation.
  • You have to report how accurate a prediction model will be on new parts.

How it works

k-fold cross-validation. Split the rows at random into k folds, usually 5 or 10. For each fold, fit the model on the other k − 1 folds and predict the held-out fold. Every row ends up with exactly one out-of-fold prediction, made by a model that never saw it.

ŷ₍₋ₖ₎ᵢ = prediction for row i from the model fitted without its fold k Test R² = 1 − Σ (yᵢ − ŷ₍₋ₖ₎ᵢ)² / Σ (yᵢ − ȳ)² RMSE = √( Σ (yᵢ − ŷ₍₋ₖ₎ᵢ)² / n )

Three rules make the comparison fair:

  • Same folds for every model. Differences then come from the models, not from the luck of the split, and you can compare models fold by fold.
  • Everything that learns from data happens inside the folds. That includes choosing terms, the number of trees or the tree depth. Tuning done on all the data and then cross-validated leaks information and flatters the model. Tuning inside each training fold is called nested cross-validation.
  • Rows that belong together stay together. Repeated measurements of one part, or consecutive parts from one run, go into the same fold; if the process drifts, validate on later data.

For a pass/fail outcome, use the log loss or the share of defects caught at an acceptable false-alarm rate rather than plain accuracy.

Example

The data are 200 molded parts with warpage (mm) and five process inputs, simulated with a known structure (the same data as in the random forest and gradient boosting articles). Five models were compared with the same 5 folds:

ModelTraining R²CV R²CV RMSE (mm)Fold R² range
Linear, main effects0.7190.6960.1470.63 to 0.79
Linear, full quadratic (21 terms)0.8650.8340.1080.77 to 0.88
Decision tree, depth 40.8630.7350.1370.70 to 0.77
Random forest, 300 trees0.9220.8040.1180.78 to 0.83
Gradient boosting, 200 depth-2 trees, η = 0.10.9760.8710.0950.84 to 0.89
Cross-validated R² by fold for five models0.70.80.9Test R² in each foldLinearQuadraticTreeForestBoosting
Cross-validated R² by fold for five models

Training R² would have misled. The depth-4 tree fits the training data as well as the quadratic model (0.863 against 0.865) but predicts new parts far worse (0.735 against 0.834). The forest's training R² of 0.922 is also much higher than its 0.804 under cross-validation.

Boosting wins, by a modest margin. Its CV R² is 0.871 against 0.834 for the quadratic model, its RMSE 12% lower, and it is better in 4 of the 5 folds.

The tuning was checked. The boosting settings were picked from an earlier look at the same data, which could flatter it. Repeating the whole comparison with the number of trees chosen inside each training fold (nested cross-validation, 4 inner folds) picked 124 to 156 trees and gave a CV R² of 0.874 and an RMSE of 0.094 mm, the same result.

The choice. If the model will run as a soft sensor that flags parts likely to warp, boosting is the better predictor. If the team wants an equation to understand the process and find settings with the response optimizer, the quadratic model is nearly as good and far easier to explain. Either way, refit the chosen model on all 200 parts.

How to read the results

  • Rank by the cross-validated score, never the training score. The gap between them shows how much a model overfits.
  • Look at the spread across folds. If two models' ranges overlap heavily and neither wins most folds, treat them as tied and prefer the simpler one.
  • Compare with a baseline. A test R² of 0 means no better than predicting the mean for every part.
  • The best score is not the whole decision. Explainability, behavior outside the data and the cost of maintaining the model count too.

Common mistakes

  • Choosing inputs or settings on all the data, then cross-validating. Do every data-driven step inside the folds.
  • Using different random splits for different models. Part of the difference is then just the split.
  • Splitting repeated measurements of the same part across folds. The model "predicts" parts it has already seen.
  • Declaring a winner from a tiny difference. A 0.005 gap in R² is well within the fold-to-fold noise here.

Frequently asked questions

How many folds should I use?

5 or 10 is the usual choice. With small data sets, repeat the cross-validation with several random splits and average the scores.

Can test R² be negative?

Yes. It is negative when the model predicts new rows worse than their mean would, which is a sign of severe overfitting or a broken model.

Do I still need a separate test set?

If you compared many models and settings, the best cross-validated score is slightly optimistic. Nested cross-validation or a final untouched test set gives the honest number.

Which model is used for predictions after the comparison?

The chosen model type, refitted on all the data with the chosen settings. The fold models exist only to measure accuracy.

Try it with your own data

StatLab SPC is free, open-source statistics for engineers. It runs in your browser, works offline, and never uploads your data.

Open StatLab