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

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.

Signature

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
Inputs
InputsDefinitionUnit
y_3Observed annual cost of patient 3, who belongs to fold 2pounds per patient per year
y_4Observed annual cost of patient 4, who belongs to fold 2pounds per patient per year
y_5Observed annual cost of patient 5, who belongs to fold 3pounds per patient per year
y_1Observed annual cost of patient 1, who belongs to fold 1pounds per patient per year
y_2Observed annual cost of patient 2, who belongs to fold 1pounds per patient per year
Output
f_1Mean cost of patients 3, 4 and 5, the prediction for patients 1 and 2, the fit f_hat with fold 1 removedpounds per patient per year
f_2Mean cost of patients 1, 2 and 5, the prediction for patients 3 and 4, the fit f_hat with fold 2 removedpounds per patient per year
f_3Mean cost of patients 1, 2, 3 and 4, the prediction for patient 5, the fit f_hat with fold 3 removedpounds per patient per year
L_1Absolute difference between the cost of patient 1 and the prediction f_1 made without that patient's foldpounds per patient per year
L_2Absolute difference between the cost of patient 2 and the prediction f_1 made without that patient's foldpounds per patient per year
L_3Absolute difference between the cost of patient 3 and the prediction f_2 made without that patient's foldpounds per patient per year
L_4Absolute difference between the cost of patient 4 and the prediction f_2 made without that patient's foldpounds per patient per year
L_5Absolute difference between the cost of patient 5 and the prediction f_3 made without that patient's foldpounds per patient per year
CVCross-validation estimate of prediction error, the mean of the five held-out absolute errorspounds per patient per year

Function

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

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.

Computational function

  • Computational function for the K-fold cross-validated squared error of a straight-line cost regression

    Takes a predictor, an outcome and a fold number for each observation and returns the K-fold cross-validation estimate of mean squared prediction error for a straight-line regression, refitting ordinary least squares with each fold removed as in equation (7.48) of Hastie and colleagues. It replaces the training mean of HE-FM-CROV-001 with a fitted line and its absolute loss with squared error. Each refit's slope and intercept take the form of the weighted least squares fit of HE-IM-MREG-001 with equal weights and of beta in HE-CF-CJA-001 with the predictor in place of its dummy codes (derived); those records fit study estimates and conjoint ratings, and neither removes a fold. The calculator line writes out six observations in three folds of two, a fixed illustrative allocation, with observations 1 and 4 in fold 1, 2 and 5 in fold 2, and 3 and 6 in fold 3. The Excel, R and Python versions take any number of rows and any fold numbering, so that setting every fold number different gives leave-one-out.

    Inputs and outputs: x_1 to x_6 are the predictor values, such as the number of long-term conditions. y_1 to y_6 are the outcomes, such as annual hospital cost in GBP thousand. Sx_k, Sy_k, Sxx_k and Sxy_k are the training sums with fold k removed. b_k and a_k are the slope and intercept fitted with fold k removed. e_i is the held-out residual of observation i, in the outcome's unit. CV is the mean of the squared held-out residuals, in the outcome's unit squared.

    Assumption: Each training set has at least two different predictor values, otherwise the slope has a zero divisor. Folds are allocated at random, or by whole patient or centre where records are grouped, and the fit is the only learned step.

    Worked example (Six patients in three folds): Illustrative data, the six patients of the Leave-One-Out article with 0, 1, 2, 3, 4 and 8 conditions and costs of 1.0, 2.2, 2.4, 3.6, 3.8 and 6.0 GBP thousand. The estimate is 0.2199, against an apparent mean squared error of 0.0722 for the line fitted to all six. x_1 = 0; x_2 = 1; x_3 = 2; x_4 = 3; x_5 = 4; x_6 = 8; y_1 = 1; y_2 = 2.2; y_3 = 2.4; y_4 = 3.6; y_5 = 3.8; y_6 = 6; Sx_1 = 15; Sy_1 = 14.4; Sxx_1 = 85; Sxy_1 = 70.2; Sx_2 = 13; Sy_2 = 13; Sxx_2 = 77; Sxy_2 = 63.6; Sx_3 = 8; Sy_3 = 10.6; Sxx_3 = 26; Sxy_3 = 28.2; b_1 = 0.5635; a_1 = 1.487; b_2 = 0.6144; a_2 = 1.2532; b_3 = 0.7; a_3 = 1.25; e_1 = -0.487; e_4 = 0.4226; e_2 = 0.3324; e_5 = 0.0892; e_3 = -0.25; e_6 = -0.85; CV = 0.2199

    Worked example (Outcomes exactly on a line): Illustrative limiting case with outcomes equal to 1 + 0.5x. Every refit recovers the same line, every held-out residual is 0 and the estimate is 0, which checks an implementation. x_1 = 0; x_2 = 1; x_3 = 2; x_4 = 3; x_5 = 4; x_6 = 8; y_1 = 1; y_2 = 1.5; y_3 = 2; y_4 = 2.5; y_5 = 3; y_6 = 5; Sx_1 = 15; Sy_1 = 11.5; Sxx_1 = 85; Sxy_1 = 57.5; Sx_2 = 13; Sy_2 = 10.5; Sxx_2 = 77; Sxy_2 = 51.5; Sx_3 = 8; Sy_3 = 8; Sxx_3 = 26; Sxy_3 = 21; b_1 = 0.5; a_1 = 1; b_2 = 0.5; a_2 = 1; b_3 = 0.5; a_3 = 1; e_1 = 0; e_4 = 0; e_2 = 0; e_5 = 0; e_3 = 0; e_6 = 0; CV = 0

    Excel: Name the ranges XValues (A2:A7), YValues (B2:B7) and FoldId (C2:C7). In row 2 enter =SUMPRODUCT(--(FoldId<>C2)) in D, =SUMPRODUCT((FoldId<>C2)*XValues) in E, =SUMPRODUCT((FoldId<>C2)*YValues) in F, =SUMPRODUCT((FoldId<>C2)*XValues^2) in G, =SUMPRODUCT((FoldId<>C2)*XValues*YValues) in H, =(D2*H2-E2*F2)/(D2*G2-E2^2) in I for the slope, =(F2-I2*E2)/D2 in J for the intercept and =(B2-J2-I2*A2)^2 in K for the squared held-out residual, and fill all down. A cell named CvEstimateOls holds =AVERAGE(K2:K7). A blank in XValues or YValues counts as 0 in these products, so rows with missing values should be deleted first. A training set with one predictor value returns #DIV/0! on that fold's rows and in CvEstimateOls.

    R: kfold_ols <- function(x, y, fold) { e <- numeric(length(y)); for (k in unique(fold)) { tr <- fold != k; b <- sum((x[tr]-mean(x[tr]))*(y[tr]-mean(y[tr])))/sum((x[tr]-mean(x[tr]))^2); a <- mean(y[tr])-b*mean(x[tr]); e[!tr] <- y[!tr]-a-b*x[!tr] }; mean(e^2) } Called as kfold_ols(c(0,1,2,3,4,8), c(1,2.2,2.4,3.6,3.8,6), c(1,2,3,1,2,3)) it gives 0.2198592. With fold numbers 1 to 6 it gives 0.2365246. A training set with one predictor value, or one fold, gives NaN.

    Python: def fold_sse(x, y, fold, k): tr = [i for i in range(len(y)) if fold[i] != k]; xm = sum(x[i] for i in tr)/len(tr); ym = sum(y[i] for i in tr)/len(tr); b = sum((x[i]-xm)*(y[i]-ym) for i in tr)/sum(pow(x[i]-xm,2) for i in tr); a = ym-b*xm; return sum(pow(y[i]-a-b*x[i],2) for i in range(len(y)) if fold[i] == k) and def kfold_ols(x, y, fold): return sum(fold_sse(x, y, fold, k) for k in set(fold))/len(y) Standard library only; kfold_ols([0,1,2,3,4,8], [1,2.2,2.4,3.6,3.8,6], [1,2,3,1,2,3]) is 0.2198592. A training set with one predictor value, one fold or an empty list raises ZeroDivisionError.

    Test (Three folds): With the first worked example's data and FoldId 1, 2, 3, 1, 2, 3, =ABS(CvEstimateOls-0.2199)<0.0005 is expected TRUE. A fit on all six rows gives 0.0722 and FALSE, folds taken in file order, 1, 1, 2, 2, 3, 3, give 0.5139 and FALSE, and doubling the cell to 0.4397 or halving it to 0.1099 also gives FALSE.

    Test (Leave-one-out): With FoldId 1, 2, 3, 4, 5, 6, =ABS(CvEstimateOls-0.2365)<0.0005 is expected TRUE, the leave-one-out value in the Leave-One-Out article, which that article obtains by a different route. A fit on all six rows gives 0.0722 and FALSE, and doubling the cell to 0.4730 or halving it to 0.1183 also gives FALSE.

    Common error (Fitting once and scoring all rows): Fitting the line once and reusing its coefficients on every row gives the apparent error of 0.0722, about a third of the 0.2199 held out in the first worked example (derived).

    Source: Hastie T, Tibshirani R, Friedman J. The Elements of Statistical Learning. 2nd ed. New York: Springer; 2009. 12th printing, authors' PDF. Section 7.10.1, equation (7.48), page 242.

    Sx_1 = x_2 + x_3 + x_5 + x_6; Sy_1 = y_2 + y_3 + y_5 + y_6; Sxx_1 = x_2^2 + x_3^2 + x_5^2 + x_6^2; Sxy_1 = x_2 * y_2 + x_3 * y_3 + x_5 * y_5 + x_6 * y_6; b_1 = (4 * Sxy_1 - Sx_1 * Sy_1) / (4 * Sxx_1 - Sx_1^2); a_1 = (Sy_1 - b_1 * Sx_1) / 4; Sx_2 = x_1 + x_3 + x_4 + x_6; Sy_2 = y_1 + y_3 + y_4 + y_6; Sxx_2 = x_1^2 + x_3^2 + x_4^2 + x_6^2; Sxy_2 = x_1 * y_1 + x_3 * y_3 + x_4 * y_4 + x_6 * y_6; b_2 = (4 * Sxy_2 - Sx_2 * Sy_2) / (4 * Sxx_2 - Sx_2^2); a_2 = (Sy_2 - b_2 * Sx_2) / 4; Sx_3 = x_1 + x_2 + x_4 + x_5; Sy_3 = y_1 + y_2 + y_4 + y_5; Sxx_3 = x_1^2 + x_2^2 + x_4^2 + x_5^2; Sxy_3 = x_1 * y_1 + x_2 * y_2 + x_4 * y_4 + x_5 * y_5; b_3 = (4 * Sxy_3 - Sx_3 * Sy_3) / (4 * Sxx_3 - Sx_3^2); a_3 = (Sy_3 - b_3 * Sx_3) / 4; e_1 = y_1 - (a_1 + b_1 * x_1); e_4 = y_4 - (a_1 + b_1 * x_4); e_2 = y_2 - (a_2 + b_2 * x_2); e_5 = y_5 - (a_2 + b_2 * x_5); e_3 = y_3 - (a_3 + b_3 * x_3); e_6 = y_6 - (a_3 + b_3 * x_6); CV = (e_1^2 + e_2^2 + e_3^2 + e_4^2 + e_5^2 + e_6^2) / 6

Try this function

Implementations

  • Excel

    Excel cross-validated absolute error from a cost column and a fold column

    Enter the costs in cells A2 to A6, named Costs, and the fold numbers in cells B2 to B6, named FoldIds. In C2 enter the training mean, in D2 the held-out absolute error, and fill both down; a cell named CvEstimate holds the mean of column D. The layout takes any number of rows and folds if the names and the last range are extended. AVERAGEIF leaves a blank cost out of the training mean, but ABS treats a blank in column A as 0, so a blank cost on a row adds a false loss and the row should be deleted. A single fold leaves no training rows and returns #DIV/0!.

    =AVERAGEIF(FoldIds,"<>"&B2,Costs); =ABS(A2-C2); =AVERAGE(D2:D6)

Assumptions

  • Every observation in exactly one randomly allocated fold

    The folds partition the data, so every patient is held out once and contributes one loss. Hastie and colleagues define kappa as an indexing function that gives the partition to which each observation is allocated by the randomisation, with K roughly equal parts. The fold pattern written here, sizes 2, 2 and 1, is one such allocation and is fixed for the calculator. The fold sizes are unequal on purpose, to show the weighting. K is at least 2, so that a training set exists.

  • Every learned step repeated inside each cross-validation fold

    Hastie and colleagues state that with a multistep modelling procedure cross-validation must cover the entire sequence of steps and that samples must be left out before any selection or filtering. Their one qualification is initial unsupervised screening, which ignores the outcome. Here the only learned step is the training mean, and it is recomputed for each fold.

  • Target is the expected error of the fitting procedure

    Hastie and colleagues state that cross-validation typically estimates well only the expected prediction error, averaged over training sets, and that the chosen model is then fitted to all the data. Each training set here is smaller than the full sample, so the estimate describes fits on fewer patients, and they state that five- or tenfold cross-validation overestimates the true prediction error when the learning curve has a considerable slope at the given training set size.

  • Independent units as the unit of splitting

    Each patient is one independent unit and contributes one record. Where a patient contributes repeated records, or patients are grouped by practice, the article keeps each patient or practice whole within one fold, otherwise the model is credited for recognising units it has already seen. In that case kappa is defined on the unit and the formula is otherwise unchanged (derived).

Worked examples

  • Five illustrative patient costs in folds of two, two and one

    Illustrative costs of 800, 1,500, 1,100, 2,600 and 1,700 pounds. The three training means are 1,800, 1,333.33 and 1,500 pounds, the held-out absolute errors sum to 3,000 pounds and the cross-validated mean absolute error is 600 pounds per patient. The same costs judged against a mean fitted to all five patients give 488 pounds.

    y_1 = 800; y_2 = 1500; y_3 = 1100; y_4 = 2600; y_5 = 1700; f_1 = 1800; f_2 = 1333.33; f_3 = 1500; L_1 = 1000; L_2 = 300; L_3 = 233.33; L_4 = 1266.67; L_5 = 200; CV = 600
  • Same five patient costs with folds taken in cost order

    The same illustrative costs entered in ascending order, so that the folds hold the two lowest costs, the next two and the highest. This allocation deliberately breaks HE-AS-CROV-001, since the folds follow the outcome. The lowest and highest folds now differ sharply from their training sets, with held-out means of 950 and 2,600 pounds against training means of 1,933.33 and 1,275 pounds, and the estimate rises to 698.33 pounds per patient, about 16% above the 600 pounds of HE-EX-CROV-001. With five patients this is one case and not a measure of bias, since the 30 possible allocations to folds of these sizes give estimates of 490 to 810 pounds with a mean of 636.67 pounds, and 10 of them reach 698.33 pounds or more (derived).

    y_1 = 800; y_2 = 1100; y_3 = 1500; y_4 = 1700; y_5 = 2600; f_1 = 1933.33; f_2 = 1500; f_3 = 1275; L_1 = 1133.33; L_2 = 833.33; L_3 = 0; L_4 = 200; L_5 = 1325; CV = 698.33
  • Limiting case of five identical patient costs in cross-validation

    Illustrative limiting case. When every patient costs 1,500 pounds every training mean equals 1,500 pounds, every held-out error is zero and the estimate is 0, which checks an implementation.

    y_1 = 1500; y_2 = 1500; y_3 = 1500; y_4 = 1500; y_5 = 1500; f_1 = 1500; f_2 = 1500; f_3 = 1500; L_1 = 0; L_2 = 0; L_3 = 0; L_4 = 0; L_5 = 0; CV = 0

Common errors

  • Judging a prediction model on its fitting data

    Testing the model on the patients used to fit it gives the apparent error. In HE-EX-CROV-001 a mean fitted to all five patients gives 488 pounds against 600 pounds held out, about 19% too low (derived). The gap can reverse a choice. In the article's illustrative table Model B looks better by 130 pounds per patient on the development data, at 820 against 950 pounds, and is worse by 77.80 pounds held out, at 1,078.60 against 1,000.80 pounds.

  • Simple average of fold means with unequal folds

    Averaging the fold means without their sizes departs from the mean over all observations when folds differ in size. In HE-EX-CROV-001 the three fold means of 650, 750 and 200 average 533.33 pounds against 600 pounds, because the single patient in fold 3 counts as much as two patients in each other fold. In the article's table the gap is smaller, at 1,002 against 1,000.80 pounds, because the folds are close in size.

  • Folds taken in file or cost order

    Assigning folds in the order of a sorted file leaves training and held-out groups that differ systematically, most of all in the lowest and highest folds. HE-EX-CROV-002 returns 698.33 pounds for the same five costs that give 600 pounds in HE-EX-CROV-001, about 16% higher (derived). With five patients part of that gap is chance, since the 30 allocations of those costs give 490 to 810 pounds. Hastie and colleagues allocate observations by randomisation.

  • Screening predictors on all the data before the folds

    Selecting predictors using the outcome in all the data and cross-validating only the later steps lets the held-out patients influence the model. Hastie and colleagues report a simulation with 50 samples and 5,000 predictors unrelated to the class labels in which this recipe gave an average cross-validation error of 3% against a true error rate of 50%.

  • Naive interval around a cross-validation estimate

    An interval of the Wald form HE-FM-AN-001, with a standard error of the form of HE-FM-ANTV-002 taken over the per-observation losses, treats those losses as independent. Bates and colleagues call this the naive cross-validation interval and find it too small on average, because each data point is used for both training and testing and the usual estimate of variance is too small, so coverage may fall far below the desired level. They introduce a nested cross-validation scheme to estimate this variance. Reading CV as the error of the one fitted model in hand is a related mistake, since they prove for ordinary least squares that it estimates the average error of models fitted on other training sets.

Sources

  • Hastie, Tibshirani and Friedman on K-fold cross-validation

    Hastie T, Tibshirani R, Friedman J. The Elements of Statistical Learning. 2nd ed. New York: Springer; 2009. 12th printing, authors' PDF. Section 7.10.1 (K-Fold Cross-Validation), pages 241 to 243, splits the data into K roughly equal-sized parts, fits the model to the other K-1 parts, defines the indexing function kappa and gives the estimate as equation (7.48). The same section gives 5 or 10 as typical choices of K, K = N as leave-one-out, the overestimate when the learning curve is steep, and the fit of the chosen model to all the data. The opening of section 7.10, page 241, states that cross-validation typically estimates well only the expected prediction error. The Chapter 7 bibliographic notes report that Stone (1977) showed AIC and leave-one-out cross-validation to be asymptotically equivalent.

    View source →

  • Hastie, Tibshirani and Friedman on the wrong and right way to cross-validate

    Hastie T, Tibshirani R, Friedman J. The Elements of Statistical Learning. 2nd ed. New York: Springer; 2009. 12th printing, authors' PDF. Section 7.10.2 (The Wrong and Right Way to Do Cross-validation), pages 245 to 247, gives the simulation with N = 50 samples and p = 5000 predictors, an average CV error rate of 3% against a true error rate of 50%, the rule that cross-validation must be applied to the entire sequence of modelling steps, and the qualification for initial unsupervised screening.

    View source →

  • Bates, Hastie and Tibshirani on what cross-validation estimates

    Bates S, Hastie T, Tibshirani R. Cross-validation: what does it estimate and how well does it do it? arXiv 2104.00673, version dated 19 July 2022 (as read). Published as Journal of the American Statistical Association. 2024;119(546):1434-1445, doi:10.1080/01621459.2023.2197686 (not seen). The abstract states the proof for the linear model fit by ordinary least squares, coverage far below the desired level and a usual estimate of variance that is too small. Section 2 (Setting and notation) gives the mean error as equation (2), the empirical standard deviation of the errors divided by the root of n as the standard error, and names the resulting naive cross-validation intervals.

    View source →

Canonical Identity

This formula 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/formula/cross-validated-mean-absolute-error-of-a-training-mean-cost-prediction-for-five-patients-in-three-folds

Cross-validated mean absolute error of a training-mean cost prediction for five patients in three folds | HealthEconomics.wiki