Functions & Formulae

Each applied formula has its own function page, with a signature, implementations, and tests.

K-fold cross-validation estimate of prediction error and the one-standard-error rule

CV(y, x, kappa, L) = (1 / N) * sum_(i=1)^N [L(y_i, f_hat_(-kappa(i))(x_i))]; keep(E_k, CV_s) = (CV_s <= E_bar + SE)

Maps outcomes, predictors, a fold allocation kappa and a loss function to the K-fold cross-validation estimate of prediction error, the mean loss over all observations when each prediction comes from a model fitted with that observation's fold removed. A second mapping takes the best model's fold errors and a simpler model's error and returns the one-standard-error choice between them, and its threshold takes the form of the upper limit U of HE-FM-AN-001 with z at 1 (derived). Once predictions are given, the averaging step takes the MAE form of HE-FM-DFC-003, or the form of HE-FN-BRIER-001 for squared error on predicted risks, and these records add the refit. HE-AS-BRIER-002 and HE-AS-DCA-003 name cross-validation as a correction for optimistic performance in development data. The article's second equation, the mean of the fold mean losses weighted by fold size, takes the form of the size-weighted mean HE-FM-CRAT-001 and of the first part of HE-FM-CWT-001 and has no record of its own; on the article's illustrative table it returns 1,000.8 for Model A and 1,078.6 for Model B. Hastie and colleagues report Stone's 1977 result that AIC, HE-FM-AIC-001, and leave-one-out cross-validation are asymptotically equivalent. The other use of the word, comparing one decision model's results with those of other models, has no formula here. Notation follows the Cross-Validation article.

  • Cross-validated mean absolute error of a training-mean cost prediction for five patients in three folds

    f_1 = (y_3 + y_4 + y_5) / 3; f_2 = (y_1 + y_2 + y_5) / 3; f_3 = (y_1 + y_2 + y_3 + y_4) / 4; L_1 = abs(y_1 - f_1); L_2 = abs(y_2 - f_1); L_3 = abs(y_3 - f_2); L_4 = abs(y_4 - f_2); L_5 = abs(y_5 - f_3); CV = (L_1 + L_2 + L_3 + L_4 + L_5) / 5

    Writes equation (7.48) of Hastie and colleagues for five patients in three folds, with the training mean as the fitted model and absolute error as the loss, so that the calculator can run. Patients 1 and 2 form fold 1, patients 3 and 4 form fold 2 and patient 5 forms fold 3. Each f_k is the mean cost of the patients outside fold k, each L_i is a held-out absolute error, and CV is the mean of the L_i, so the last step takes the MAE form of HE-FM-DFC-003 with refitted predictions in place of its forecasts. Every patient counts once, so a larger fold carries more weight. The general form takes any model, loss and K, and HE-CF-CROV-001 computes it for a straight-line regression.

  • One-standard-error threshold from five fold errors and the choice of a simpler model

    E_bar = (E_1 + E_2 + E_3 + E_4 + E_5) / 5; SS = (E_1 - E_bar)^2 + (E_2 - E_bar)^2 + (E_3 - E_bar)^2 + (E_4 - E_bar)^2 + (E_5 - E_bar)^2; SE = sqrt(SS / (4 * 5)); T = E_bar + SE; keep = CV_s <= T

    Hastie and colleagues describe a rule often used with cross-validation, which chooses the most parsimonious model whose error is no more than one standard error above the error of the best model. E_1 to E_5 are the best model's errors in five equal folds, E_bar their mean, SS the sum of squared deviations and SE the standard error of the mean across folds, which takes the form of HE-FM-ANTV-002 with folds in place of pairs. T is the threshold, which takes the form of the upper limit U of HE-FM-AN-001 with E_bar as theta_hat and z at 1, used as a selection margin and not read as a confidence limit (derived). The flag keep is 1 when the simpler model's error CV_s is at or below T, so a tie keeps the simpler model, and 0 otherwise. Five folds are written out so that the calculator can run, and the Excel form takes any K.

This set of formulae is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0).

Author: Dr Darrin Baines · Publisher: Intelligent Parameters Ltd · Identifier: https://healtheconomics.wiki/concept/cross-validation/formulae