Comparing models by predictive accuracy

Today let's understand how to compare models properly: not by how well each one fits the data it was trained on, but by how well each one predicts data it has not seen yet.

Picture eighteen boutique gyms. Each one reports last month's memberships sold and the profit it made, in thousands of dollars.

Three Bayesian regressions will compete to explain that profit over this lesson: one using nothing but an overall average, one adding memberships sold, and one adding floor size on top of that.

Here is memberships sold against profit for all eighteen gyms, the data every model in this lesson works from.

Profit rises with memberships, roughly along a line. But it is not a perfect line: gyms with similar memberships still land at noticeably different profits, so there is room for another predictor to matter, or to look like it matters.

Why fit alone cannot judge a model

So if profit rises with memberships, why not just fit a model and see which one explains the data best?

Call the three models by name from here on. Model A uses nothing but an overall average. Model B adds memberships sold. Model C adds floor size, each gym's floor space measured in hundreds of square feet, on top of memberships.

A natural way to judge them is R-squared, the share of the variation in profit a model accounts for in the data it was fit on. Fit all three with ordinary least squares and compare.

RInteractive R
# Fit three nested regressions and compare their training R-squared set.seed(11) n_gyms <- 18 memberships <- round(runif(n_gyms, 40, 210)) floor100 <- round(runif(n_gyms, 20, 60)) profit <- 2 + 0.18 * memberships + rnorm(n_gyms, 0, 6) gyms <- data.frame(memberships, floor100, profit) model_a <- lm(profit ~ 1, data = gyms) model_b <- lm(profit ~ memberships, data = gyms) model_c <- lm(profit ~ memberships + floor100, data = gyms) round(summary(model_a)$r.squared, 3) #> [1] 0 round(summary(model_b)$r.squared, 3) #> [1] 0.881 round(summary(model_c)$r.squared, 3) #> [1] 0.884

  

R-squared climbs at every step: 0 for Model A, 0.881 once memberships is added, 0.884 once floor size joins it too.

That last step should give you pause. This data was built with set.seed(11), with floor size carrying no real effect on profit at all. Yet adding it still nudged R-squared up, from 0.881 to 0.884.

Key Insight
Training R-squared can only rise or stay flat as you add predictors, whether or not they truly help. A number that never goes down cannot be the judge of which predictors belong. Something else has to decide, and that something is how well a model predicts data it was not fit on.