Cross-Validation in Practice

Today

  • Candidate models
  • Selection optimism
    • One training sample
    • Repeated samples
  • Test data workflow
    • Workflow steps
    • Choosing
    • Final evaluation
  • Activity

Ames home sales

Candidate models

Nested KNN candidates

\[\widehat f_{q,K}(x_0) = \frac{1}{K}\sum_{i \in \mathcal{N}_{q,K}(x_0)} y_i\]

  • \(y_i\) — \(\log_{10}\) price of training home \(i\)
  • \(q\) — number of explanatory variables used
  • \(\mathcal{N}_{q,K}(x_0)\) — the \(K\) training homes closest to \(x_0\)
  • \(K\) — number of neighbors

\(S = 240\) candidate models: \(q = 1, \ldots, 6\) and \(K = 1, \ldots, 40\)

Selection optimism

One training sample

Repeated samples

Test data workflow

Workflow steps

  1. Split off test data first
  2. Choose by cross-validation on the training data
    • Standardize inside each fold
    • Held-out folds are validation sets
  3. Refit the chosen model to all the training data
  4. Evaluate it once on the test data

Choosing

Final evaluation

  • Chosen model: \(q = 6\), \(K = 9\)
  • Cross-validation estimate: 0.00567
  • Test MSE, evaluated once: 0.00641

Choose again with the test data and they become validation data

Activity

  • modeldata::stackoverflow, 5,594 developers
  • Response variable: Remote
  • Question: Which developers work remotely?
  • Script: 15-cv-in-practice_activity.R

Conclusion

  • Candidate models
    • Cross-validation estimates a model fixed in advance with little bias
  • Selection optimism
    • One training sample
      • The winning estimate is the one that fell lowest by chance
    • Repeated samples
      • More near-tied, noisy candidates make the minimum more optimistic
  • Test data workflow
    • Workflow steps
      • Test data first; choose using the training data only
    • Choosing
      • Choose by cross-validation on the training data only
    • Final evaluation
      • Evaluate once; the test error is what you report
  • Activity
    • Apply the whole workflow to new data