Skip to main content

17  Model Comparison Criteria

“When a measure becomes a target, it ceases to be a good measure.” - Marilyn Strathern

So far we have used the coefficient of determination, \(R^2\), and the adjusted coefficient of determination, \(R^2_a\), to judge how well a model fits the data and to compare models. This chapter covers other ways to measure fit and compare models. These criteria help when building a model.

17.1 Possible Models

From a set of \(p - 1\) predictors, we can build \(2^{p-1}\) different models.

The count comes from the fact that each predictor is either in the model or out of it.

ExampleCounting possible subset models

If there are 4 possible predictors, each predictor has two choices:

  • included in the model
  • excluded from the model

This gives

\[ 2^4 = 16 \]

possible subset models. With 10 possible predictors, there are

\[ 2^{10}=1024 \]

possible subset models. The number of models grows quickly, which is why we need model-selection criteria.

For example, the \[ 2^4 = 16 \] subset models that can be formed from a pool of four \(X\) variables are \[ \begin{align*} & \text{None}\\ & x_{1}\\ & x_{2}\\ & x_{3}\\ & x_{4}\\ & x_{1},x_{2}\\ & x_{1},x_{3}\\ & x_{1},x_{4}\\ & x_{2},x_{3}\\ & x_{2},x_{4}\\ & x_{3},x_{4}\\ & x_{1},x_{2},x_{3}\\ & x_{1},x_{2},x_{4}\\ & x_{1},x_{3},x_{4}\\ & x_{2},x_{3},x_{4}\\ & x_{1},x_{2},x_{3},x_{4} \end{align*} \]

Model selection procedures (also called subset selection or variable selection procedures) are designed to find a small group of regression models that are “good” by some criterion.

That group might hold three to six “good” subsets, and the investigator can then study these models closely before choosing the final one.

Many criteria for comparing regression models have been proposed. We will look at six: \[ \begin{align*} & R_{p}^{2}\\ & R_{a,p}^{2}\\ & AIC_{p}\\ & SBC_{p}\\ & PRESS_{p}\\ & \text{pred }R_{p}^{2} \end{align*} \]

NoteWhich direction is better?

For some of these criteria larger is better; for others, smaller is better.

Criterion Preferred direction Main idea
\(R_p^2\) Larger Measures in-sample fit, but never decreases when predictors are added.
\(R_{a,p}^2\) Larger Adjusts \(R^2\) for the number of parameters in the model.
\(AIC_p\) Smaller Rewards fit but penalizes model complexity.
\(BIC_p\) or \(SBC_p\) Smaller Similar to AIC, but usually penalizes complexity more strongly.
\(PRESS_p\) Smaller Measures leave-one-out prediction error.
pred \(R_p^2\) Larger Converts PRESS into an \(R^2\)-style prediction measure.

No criterion should replace judgment. The best model usually answers the scientific question, predicts well, and is no more complicated than it needs to be.

ExampleExhaustive subset selection with four predictors

When there are only a few possible predictors, we can fit every subset model and compare the criteria. This is called exhaustive subset selection.

This example uses four possible predictors from mtcars: disp, hp, wt, and qsec. With 4 predictors there are \(2^4=16\) subset models.

library(tidyverse)
library(broom)

press_stat <- function(model) {
  sum((residuals(model) / (1 - hatvalues(model)))^2)
}

candidate_predictors <- c("disp", "hp", "wt", "qsec")

all_subsets <- map(
  0:length(candidate_predictors),
  \(k) {
    if (k == 0) {
      list(character(0))
    } else {
      combn(candidate_predictors, k, simplify = FALSE)
    }
  }
) |>
  unlist(recursive = FALSE)

sst_mpg <- sum((mtcars$mpg - mean(mtcars$mpg))^2)

exhaustive_results <- tibble(predictors = all_subsets) |>
  mutate(
    model = map_chr(
      predictors,
      \(vars) {
        if (length(vars) == 0) {
          "Intercept only"
        } else {
          paste(vars, collapse = " + ")
        }
      }
    ),
    formula = map(
      predictors,
      \(vars) {
        right_side <- if (length(vars) == 0) {
          "1"
        } else {
          paste(vars, collapse = " + ")
        }

        as.formula(paste("mpg ~", right_side))
      }
    ),
    fit = map(formula, \(form) lm(form, data = mtcars)),
    p = map_int(predictors, length) + 1,
    PRESS = map_dbl(fit, press_stat),
    pred_r_squared = 1 - PRESS / sst_mpg,
    glance = map(fit, glance)
  ) |>
  unnest(glance) |>
  select(
    model,
    p,
    r.squared,
    adj.r.squared,
    AIC,
    BIC,
    PRESS,
    pred_r_squared
  )

exhaustive_results |>
  arrange(BIC) |>
  print(width = Inf)
# A tibble: 16 × 8
   model                     p r.squared adj.r.squared   AIC   BIC PRESS
   <chr>                 <dbl>     <dbl>         <dbl> <dbl> <dbl> <dbl>
 1 hp + wt                   3     0.827         0.815  157.  163.  247.
 2 wt + qsec                 3     0.826         0.814  157.  163.  249.
 3 hp + wt + qsec            4     0.835         0.817  157.  164.  245.
 4 disp + hp + wt            4     0.827         0.808  159.  166.  261.
 5 disp + wt + qsec          4     0.826         0.808  159.  166.  259.
 6 disp + hp + wt + qsec     5     0.835         0.811  159.  168.  258.
 7 disp + wt                 3     0.781         0.766  164.  170.  309.
 8 wt                        2     0.753         0.745  166.  170.  328.
 9 disp + hp                 3     0.748         0.731  169.  174.  344.
10 disp                      2     0.718         0.709  170.  175.  366.
11 disp + hp + qsec          4     0.754         0.728  170.  177.  357.
12 disp + qsec               3     0.722         0.702  172.  178.  378.
13 hp                        2     0.602         0.589  181.  186.  552.
14 hp + qsec                 3     0.637         0.612  180.  186.  540.
15 qsec                      2     0.175         0.148  205.  209. 1041.
16 Intercept only            1     0             0      209.  212. 1200.
   pred_r_squared
            <dbl>
 1         0.781 
 2         0.779 
 3         0.782 
 4         0.768 
 5         0.770 
 6         0.771 
 7         0.725 
 8         0.709 
 9         0.695 
10         0.675 
11         0.683 
12         0.664 
13         0.510 
14         0.520 
15         0.0754
16        -0.0656

Sorted by BIC, the simpler models that still fit well come out on top. Sorting by a different criterion could change the order.

17.2 Notation

Before we look at the criteria, we need some notation. The number of potential \(X\) variables in the pool is \(P-1\).

We assume every regression model contains an intercept term \(\beta_0\).

So the regression function with all potential \(X\) variables has \(P\) parameters, and the function with no \(X\) variables has one parameter (\(\beta_0\)).

As always, the number of \(X\) variables in a subset is \(p-1\), so there are \(p\) parameters in the regression function for that subset. This gives \[ 1\le p \le P < n \]

17.3 Coefficient of Determination and SSE

We want models that fit the data well, so we want the coefficient of multiple determination, \(R^2\), to be high.

We write the number of parameters in the model as a subscript, so the coefficient of determination becomes \(R^2_p\).

Using \(R^2_p\) as the criterion is equivalent to using the error sum of squares \(SSE_p\). The \(R^2_p\) criterion is not meant to pick out the subsets that maximize it.

\(R^2_p\) can never decrease when more \(X\) variables are added to the model, so it is largest when all \(P - 1\) potential X variables are in the model.

The idea behind the \(R^2_p\) criterion is to find the point where adding more \(X\) variables is not worth it, because \(R^2_p\) goes up only a little.

This point is often reached with only a few \(X\) variables in the model.

WarningDo not choose a model using R-squared alone

Because \(R_p^2\) cannot decrease when a predictor is added, the model with the most predictors always looks at least as good by this criterion. \(R_p^2\) is useful for describing fit, but it is risky as the only tool for choosing a model.

When comparing candidate models, ask whether the added variables give a meaningful improvement, not just any improvement.

ExampleOverfitting by adding noise predictors

This example adds randomly generated noise predictors to a useful model. The noise variables have nothing to do with mpg, but ordinary least squares can still use them to lower the in-sample error a little.

set.seed(3386)

press_stat <- function(model) {
  sum((residuals(model) / (1 - hatvalues(model)))^2)
}

noise_dat <- mtcars |>
  mutate(
    noise_1 = rnorm(n()),
    noise_2 = rnorm(n()),
    noise_3 = rnorm(n()),
    noise_4 = rnorm(n())
  )

sst_noise <- sum((noise_dat$mpg - mean(noise_dat$mpg))^2)

noise_models <- list(
  "Base model" = lm(mpg ~ wt + qsec, data = noise_dat),
  "Add 1 noise predictor" = lm(
    mpg ~ wt + qsec + noise_1,
    data = noise_dat
  ),
  "Add 2 noise predictors" = lm(
    mpg ~ wt + qsec + noise_1 + noise_2,
    data = noise_dat
  ),
  "Add 4 noise predictors" = lm(
    mpg ~ wt + qsec + noise_1 + noise_2 + noise_3 + noise_4,
    data = noise_dat
  )
)

noise_comparison <- purrr::imap_dfr(
  noise_models,
  \(fit_object, model_name) {
    glance(fit_object) |>
      mutate(
        model = model_name,
        predictors = length(coef(fit_object)) - 1,
        PRESS = press_stat(fit_object),
        pred_r_squared = 1 - PRESS / sst_noise
      )
  }
) |>
  select(
    model,
    predictors,
    r.squared,
    adj.r.squared,
    AIC,
    BIC,
    PRESS,
    pred_r_squared
  )

noise_comparison |>
  print(width = Inf)
# A tibble: 4 × 8
  model                  predictors r.squared adj.r.squared   AIC   BIC PRESS
  <chr>                       <dbl>     <dbl>         <dbl> <dbl> <dbl> <dbl>
1 Base model                      2     0.826         0.814  157.  163.  249.
2 Add 1 noise predictor           3     0.830         0.812  158.  165.  257.
3 Add 2 noise predictors          4     0.830         0.805  160.  169.  269.
4 Add 4 noise predictors          6     0.834         0.795  163.  175.  328.
  pred_r_squared
           <dbl>
1          0.779
2          0.772
3          0.761
4          0.709

\(R^2\) goes up as the noise predictors are added. That does not make the model better. The other criteria show that the extra predictors add complexity and do nothing for prediction.

17.4 Adjusted Coefficient of Determination and MSE

\(R^2_{p}\) ignores the number of parameters in the regression model, and max(\(R^2_{p}\)) can never decrease as \(p\) increases. For these reasons the adjusted coefficient of multiple determination \(R^2_{a,p}\) has been suggested as an alternative criterion.

It accounts for the number of parameters in the model through the degrees of freedom.

With the \(R^2_{a,p}\) criterion, we look for a few subsets where \(R^2_{a,p}\) is at its maximum, or so close to it that adding more variables is not worth it.

Using \(R^2_{a,p}\) as the criterion is equivalent to using the mean square error \(MSE_p\). This follows from \[ \begin{align*} R_{a}^{2} & =1-\frac{\frac{SSE}{n-p}}{\frac{SSTO}{n-1}}\\ & =1-\left(\frac{n-1}{n-p}\right)\frac{SSE}{SSTO}\\ & = 1-\frac{MSE}{\frac{SSTO}{n-1}} \end{align*} \]

17.5 AIC and BIC

We have seen that \(R_{a,p}^2\) penalizes models with many predictors.

Two popular alternatives that also penalize added predictors are Akaike’s information criterion (\(AIC_p\)) and Schwarz’s Bayesian criterion (\(SBC_p\)).

\(SBC_p\) is more often called the Bayesian information criterion (\(BIC_p\)).

We look for models with small values of \(AIC_p\) or \(BIC_p\), where \[ \begin{align} AIC_{p} & =n\ln SSE_{p}-n\ln n+2p\\ BIC_{p} & =n\ln SSE_{p}-n\ln n+\left(\ln n\right)p \end{align} \tag{17.1}\]

In both measures the first term is \[ n\ln SSE_{p} \] which decreases as \(p\) increases.

The second term is fixed for a given sample size \(n\), and the third term grows with the number of parameters, \(p\).

Models with small \(SSE_p\) do well on these criteria as long as the penalties \[ \begin{align*} 2p & \text{ for }AIC_p \\ (\ln n)p & \text{ for }BIC_p \end{align*} \] are not too large.

If \(n \ge 8\), the penalty for \(BIC_p\) is larger than the penalty for \(AIC_p\), so \(BIC_p\) tends to favor more parsimonious models.

A parsimonious model reaches the level of explanation or prediction you want with as few predictor variables as possible.

We treat the models with the lowest \(AIC_p\) or \(BIC_p\) as the “best” ones.

ImportantCompare AIC and BIC only for models fit to the same response

AIC and BIC are for comparing candidate models fit to the same response variable on the same set of observations. They should not be used to compare, for example, a model for mpg with a model for log(mpg).

Software may report versions of AIC and BIC that differ from the textbook formulas by a constant. This is usually not a problem, because model selection depends on the differences between models fit to the same data.

17.6 Criteria for Prediction

The \(PRESS_p\) (prediction sum of squares) criterion measures how well the fitted values from a subset model predict the observed responses \(y_i\).

PRESS differs from SSE in how each fitted value \(\hat{y}_i\) is obtained. For PRESS, we

  1. delete the \(i\)th case from the data set,

  2. estimate the regression function for the subset model from the remaining \(n - 1\) cases, and

  3. use that fitted regression function to get the predicted value \(\hat{y}_{i(i)}\) for the \(i\)th case.

In the notation \(\hat{y}_{i(i)}\), the first subscript \(i\) says this is a predicted value for the \(i\)th case, and the second subscript \((i)\) says the \(i\)th case was left out when the regression function was fit.

The PRESS prediction error for the \(i\)th case is \[ y_i - \hat{y}_{i(i)} \] and the \(PRESS_p\) criterion is the sum of the squared prediction errors over all \(n\) cases: \[ \begin{align} PRESS_p = \sum_{i=1}^n\left(y_i - \hat{y}_{i(i)} \right)^2 \end{align} \tag{17.2}\]

Models with small \(PRESS_p\) values are good candidates. When the prediction errors \(y_i - \hat{y}_{i(i)}\) are small, so are their squares and the sum of their squares.

So models with small \(PRESS_p\) values fit well in the sense that their prediction errors are small.

Another measure of prediction is the predicted \(R^{2}\), which is related to PRESS by \[ \begin{align} \text{pred }R_{p}^{2} & =1-\frac{PRESS_{p}}{SSTO} \end{align} \tag{17.3}\] A large value of pred \(R_{p}^{2}\) means the model is “good” at prediction.

17.6.1 PRESS and Leave-One-Out Cross-Validation

PRESS is closely tied to leave-one-out cross-validation (LOOCV). In LOOCV, we leave out one observation, fit the model to the rest, and predict the one we left out, repeating this for every observation. PRESS is the sum of the squared prediction errors from that process.

For ordinary least squares, PRESS can be computed without refitting the model \(n\) times. If \(e_i\) is the ordinary residual from the full model and \(h_{ii}\) is the leverage of observation \(i\), then

\[ \begin{align*} y_i-\hat{y}_{i(i)} = \frac{e_i}{1-h_{ii}}. \end{align*} \]

So for ordinary least squares,

\[ \begin{align*} PRESS = \sum_{i=1}^n \left(\frac{e_i}{1-h_{ii}}\right)^2. \end{align*} \]

This is why PRESS measures prediction: each observation is judged by how well the model predicts it when that observation was not used to fit the model.

Example 17.1  

ExampleComparing candidate models with several criteria

glance() gives \(R^2\), \(R^2_a\), AIC, and BIC for a fitted model. For an ordinary least squares model, we can also compute PRESS from the residuals and leverage values.

Let’s compare several candidate models for mtcars. None of them is meant as the final answer; they are a small set of models used to show how the criteria work.

library(tidyverse)
library(broom)

press_stat <- function(model) {
  sum((residuals(model) / (1 - hatvalues(model)))^2)
}

sst_mpg <- sum((mtcars$mpg - mean(mtcars$mpg))^2)

candidate_models <- list(
  "Full untransformed" = lm(
    mpg ~ disp + hp + drat + wt + qsec,
    data = mtcars
  ),
  "Full transformed" = lm(
    mpg ~ log(disp) + log(hp) + drat + log(wt) + qsec,
    data = mtcars
  ),
  "Smaller transformed" = lm(
    mpg ~ log(hp) + log(wt) + qsec,
    data = mtcars
  ),
  "Weight and time" = lm(
    mpg ~ log(wt) + qsec,
    data = mtcars
  )
)

model_comparison <- purrr::imap_dfr(
  candidate_models,
  \(fit_object, model_name) {
    glance(fit_object) |>
      mutate(
        model = model_name,
        PRESS = press_stat(fit_object),
        pred_r_squared = 1 - PRESS / sst_mpg
      )
  }
) |>
  select(
    model,
    r.squared,
    adj.r.squared,
    AIC,
    BIC,
    PRESS,
    pred_r_squared
  )

model_comparison |>
  arrange(AIC) |>
  print(width = Inf)
# A tibble: 4 × 7
  model               r.squared adj.r.squared   AIC   BIC PRESS pred_r_squared
  <chr>                   <dbl>         <dbl> <dbl> <dbl> <dbl>          <dbl>
1 Smaller transformed     0.891         0.879  144.  151.  156.          0.861
2 Weight and time         0.878         0.870  145.  151.  167.          0.852
3 Full transformed        0.892         0.871  147.  158.  172.          0.847
4 Full untransformed      0.849         0.820  158.  169.  263.          0.767

The output shows that the criteria do not always prefer the same model. A model with more predictors may have a larger \(R^2\), while AIC, BIC, PRESS, or predicted \(R^2\) may favor a simpler model when the extra predictors do not improve it enough.

TipWhat if the criteria disagree?

The criteria often disagree because they value different things. When they do, compare the models more closely.

  1. Start with the goal. If prediction is the main goal, give more weight to PRESS, predicted \(R^2\), validation error, or cross-validation. If explanation is the main goal, give more weight to interpretability and subject-matter reasons.

  2. Prefer the simpler model when performance is about the same. If two models perform almost identically, the more parsimonious one is usually easier to explain, teach, and defend.

  3. Look for unstable variables. If a predictor appears only in the model one criterion favors, ask whether it has a meaningful interpretation or is mostly taking advantage of this sample.

  4. Check diagnostics. A model with good criteria values can still violate the assumptions. Residual plots, influential observations, and multicollinearity still need to be checked.

  5. Avoid automatic selection. The criteria narrow the search. They do not replace thinking about the research question, the quality of the data, and the cost of a more complex model.

17.7 Recap

This chapter introduced several criteria for comparing candidate regression models.

Idea Meaning
Subset model A model that includes only some of the available predictors.
Number of possible models With \(p-1\) possible predictors, there are \(2^{p-1}\) possible subset models.
Exhaustive subset selection Fitting every possible subset model from a chosen pool of predictors. This is feasible only when there are few candidate predictors.
\(R_p^2\) Measures in-sample fit for a model with \(p\) parameters; larger is better, but it never decreases when predictors are added.
\(R_{a,p}^2\) Adjusted \(R^2\); penalizes unnecessary predictors through the degrees of freedom.
AIC A model-selection criterion that balances fit and complexity; smaller is better.
BIC/SBC Similar to AIC but usually with a stronger penalty for complexity; smaller is better.
Parsimonious model A model that meets the goal with relatively few predictors.
PRESS A leave-one-out prediction error criterion; smaller values mean better prediction.
LOOCV Leave-one-out cross-validation; fit the model without one observation, then predict that observation.
Predicted \(R^2\) An \(R^2\)-style measure based on PRESS; larger values mean better prediction.
Overfitting A model fitting random or sample-specific noise instead of real signal.
Criterion disagreement Different criteria may select different models because they reward fit and penalize complexity differently.

17.8 Check your understanding

NoteProblems
  1. Why are there \(2^{p-1}\) possible subset models when there are \(p-1\) candidate predictors?

  2. Why is \(R^2\) risky to use as the only model-selection criterion?

  3. What problem is adjusted \(R^2\) trying to address?

  4. For AIC and BIC, do we prefer larger or smaller values? Why?

  5. Why does BIC often favor simpler models than AIC?

  6. What does it mean for a model to be parsimonious?

  7. How is PRESS different from SSE?

  8. Why is predicted \(R^2\) useful when the goal is prediction?

  9. If one model has the best AIC and another has the best BIC, does that automatically mean one of them is wrong? Explain.

  10. Why should AIC and BIC usually be compared only among models fit to the same response using the same observations?

  11. Why does exhaustive subset selection become difficult as the number of candidate predictors grows?

  12. How is PRESS connected to leave-one-out cross-validation?

  13. Why can adding random noise predictors increase \(R^2\) without improving the model?

  14. If several criteria disagree, why is it usually better to compare a small set of candidate models than to automatically choose the winner from one criterion?

  1. Each predictor has two choices: it is either included or excluded. With \(p-1\) predictors, that gives \(2^{p-1}\) possible combinations.

  2. It always rewards adding predictors. \(R^2\) cannot decrease when predictors are added, even if they add little information, so it can push us toward models that are more complex than necessary.

  3. It accounts for model size. Adjusted \(R^2\) penalizes a model for using more parameters, to separate real improvement from improvement that comes only from adding predictors.

  4. Smaller values are better. AIC and BIC both combine a measure of lack of fit with a penalty for complexity, and a smaller value means a better tradeoff between fit and model size.

  5. BIC has a larger penalty when \(n \ge 8\). AIC uses a penalty of \(2p\), and BIC uses \((\ln n)p\). Since \(\ln n\) is larger than 2 for sample sizes of 8 or more, BIC discourages extra predictors more.

  6. It does the job with few predictors. A parsimonious model reaches the modeling goal with as few predictors as reasonably possible.

  7. PRESS measures leave-one-out prediction error. SSE uses residuals from a model fit to all the observations. PRESS predicts each observation from a model fit without it, so it reflects prediction performance more directly.

  8. It puts prediction performance on an \(R^2\)-like scale. Predicted \(R^2\) uses PRESS to summarize how well the model predicts observations that were left out. Larger values suggest better prediction.

  9. No. The criteria weigh the tradeoffs differently. AIC may prefer a somewhat larger model and BIC a simpler one. The disagreement is a reason to look at both models carefully; it does not mean one criterion is invalid.

  10. The criteria are relative comparisons. AIC and BIC are built to compare candidate models fit to the same data and response. If the response or the observations change, the values can no longer be compared directly.

  11. The number of models grows exponentially. Each predictor can be in or out, so each extra candidate predictor doubles the number of possible subset models.

  12. PRESS sums the leave-one-out squared prediction errors. Each observation is predicted by a model fit without that observation, and the squared errors from those predictions are added up to get PRESS.

  13. Ordinary least squares can chase noise in the sample. More predictors give the model more flexibility, so the in-sample SSE can go down and \(R^2\) can go up even when the new predictors mean nothing. Prediction criteria can show that the extra complexity does not carry over to new data.

  14. The criteria are tools, not rules. AIC, BIC, adjusted \(R^2\), and PRESS weigh different tradeoffs. Comparing a small set of candidate models lets us weigh prediction, interpretation, diagnostics, and subject-matter relevance together.