David Mashiah

Note · Machine learning · Measurement

R squared and adjusted R squared, worked on eight points

R² is the most quoted number in regression and one of the most misread. It does not say how accurate a model is. It says how much better the model is than guessing the average every time. Worked on eight points, a straight line scores 0.854, a column of junk data raises that, adjusted R² catches the junk, and a model worse than the average scores below zero.

R², the coefficient of determination, is a comparison with a baseline. The baseline is the dumbest prediction possible: the mean of the data, for every point. R² asks what share of the baseline's squared error your model removes.

The formula, with numbers

Eight points, x = 1 to 8, with y = 4.0, 3.5, 8.1, 6.9, 12.4, 10.2, 15.8, 14.1. The mean of y is 9.375.

SS_tot, the total sum of squares, is the squared miss of the baseline: Σ (y − mean)². Here it is 144.6.

SS_res, the residual sum of squares, is the squared miss of your model. A least-squares line through these points is y = 1.66 + 1.71x, and its squared misses add to 21.2.

R² = 1 − SS_res / SS_tot = 1 − 21.2 / 144.6 = 0.854

Engineering drawing. Two panels of the same eight points. Left: a horizontal line at the mean, 9.375, with dashed vertical misses from each point, labelled SS_tot, misses of the mean, 144.6. Right: the fitted line y = 1.66 + 1.71x in red with much shorter dashed misses, labelled SS_res, misses of the line, 21.2. Below: R² = 1 − 21.2 / 144.6 = 0.854, the line removes 85 percent of the squared miss of guessing the mean. Right: a table. Line with one input, R² 0.854, adjusted 0.829. With a junk column added, R² 0.859, adjusted 0.802. A fixed model y = 2x + 5, R² −0.35.
Same eight points, two predictions. R² is how much shorter the right-hand misses are than the left-hand ones, in squared terms.

So the line removes 85 percent of the squared error of guessing the mean. That is all R² says. It does not say the predictions are within 15 percent, or that the line is the right model, or that it will do as well on new data.

R² can be negative

The name suggests a square, so it cannot be below zero. It can. Take a model that was not fitted to these points, say y = 2x + 5, from some other dataset. Its squared misses here add to 195.7, more than the baseline's 144.6:

R² = 1 − 195.7 / 144.6 = −0.35

A negative R² means the model is worse than predicting the mean every time. It cannot happen for a least-squares line scored on its own training data, because the mean is one of the lines it could have chosen. It happens all the time on test data, which is where it matters. It is worked through as a real failure in the out-of-distribution note, and in the cross-validation note, where the same model scores 0.80 or −0.67 depending on how the data was split.

More inputs always help, on paper

Add a second input to the model: a column z = 3, 7, 1, 5, 8, 2, 6, 4, which is just the numbers 1 to 8 shuffled and has nothing to do with y. Fit y = a + b·x + c·z by least squares.

R² goes up, from 0.854 to 0.859. It always does, or stays the same, when you add an input to a least-squares fit on its training data, because the fit can always set the new coefficient to zero and do no worse. With enough junk columns, R² climbs toward 1 on data the model has simply memorised.

Adjusted R²

Adjusted R² charges for each input. With n points and k inputs:

adjusted R² = 1 − (1 − R²) · (n − 1) / (n − k − 1)

ModelkR²Adjusted R²
Line in x10.8541 − 0.146 × 7 / 6 = 0.829
x plus the junk column z20.8591 − 0.141 × 7 / 5 = 0.802

The junk column raised R² and lowered adjusted R². The penalty factor (n − 1) / (n − k − 1) grew from 7/6 to 7/5, and the small gain in fit was not enough to pay for it. An input has to earn its place.

Adjusted R² is a correction on training data, though, not a replacement for testing. The honest check is still the score on data the model has not seen, from a held-out test set or cross-validation.

Reading R² in practice