Sub topic 6
Measuring fit and comparing models: R-squared, adjusted R-squared, AIC, BIC
This chapter assumes you can read a regression output table, you know what SSE (sum of squared errors, the residual sum of squares) and SST (total sum of squares) mean, and you know that the F-test checks coefficients jointly. The question here is how well a model fits and how to choose between competing models.
What R-squared measures
R-squared is the share of the variation in the dependent variable that the regressors explain:
R² = 1 − SSE / SST = RSS / SST
Here SST is the total variation of y around its mean, RSS is the variation the model explains, and SSE is what is left over. R² runs from 0 to 1 in a regression with an intercept.
R² has three limits that the exam likes to test:
- It never falls when you add a variable. Adding any regressor, even pure noise, cannot raise SSE. Least squares can always set the new coefficient to zero, so R² stays the same or rises.
- It says nothing about bias. A model with a high R² can still have omitted variables, a wrong functional form, or serial correlation. Those problems are covered in later chapters.
- It is not comparable across different dependent variables. The R² of a model for y and the R² of a model for ln(y) measure different things. You also cannot compare R² across samples of different sizes.
Adjusted R-squared
Adjusted R-squared charges a penalty for each added regressor. It replaces the sums of squares with variance estimates that use degrees of freedom:
Adjusted R² = 1 − [(n − 1) / (n − k − 1)] × (1 − R²)
Here n is the number of observations and k is the number of independent variables, not counting the intercept.
n, k = 200, 2
r2 = 0.80
adj_r2 = 1 - (n - 1) / (n - k - 1) * (1 - r2)
print(round(adj_r2, 4))This prints 0.798. The factor (n − 1)/(n − k − 1) is always above 1 when k ≥ 1, so three properties follow:
- Adjusted R² is less than or equal to R² whenever k ≥ 1.
- Adjusted R² rises when you add a variable only if that variable's t-statistic has an absolute value above 1. So it is a weak screen. A variable can raise adjusted R² and still be insignificant at the 5% level.
- Adjusted R² can be negative when R² is small and k is large relative to n.
AIC and BIC
Adjusted R² penalizes mildly. Information criteria penalize more explicitly. The CFA curriculum defines them from SSE:
- AIC = n × ln(SSE / n) + 2(k + 1)
- BIC = n × ln(SSE / n) + ln(n) × (k + 1)
In both formulas the first term falls as the fit improves, and the second term grows with the number of estimated parameters (k slopes plus the intercept). Lower is better for both. A criterion has no meaning on its own. Only the difference between models fitted to the same data matters.
The two criteria differ in their penalty per parameter. AIC charges 2. BIC charges ln(n), which exceeds 2 once n is above about 7. For any realistic sample, BIC therefore punishes extra variables harder and tends to pick smaller models.
The curriculum links the choice to your purpose:
- Use AIC when the goal is prediction.
- Use BIC when the goal is the best goodness of fit, meaning the most parsimonious model that still describes the data.
A worked comparison
Suppose you fit two models to the same n = 60 observations, with SST = 300. Model A has k = 2 and SSE = 120. Model B has k = 4 and SSE = 108.
| Measure | Model A (k = 2) | Model B (k = 4) | Preferred |
|---|---|---|---|
| R² | 0.6000 | 0.6400 | B (always higher) |
| Adjusted R² | 0.5860 | 0.6138 | B |
| AIC | 47.59 | 45.27 | B (lower) |
| BIC | 53.87 | 55.74 | A (lower) |
The calculations for Model A:
- R² = 1 − 120/300 = 0.60.
- Adjusted R² = 1 − (59/57) × 0.40 = 0.5860.
- AIC = 60 × ln(120/60) + 2 × 3 = 41.59 + 6 = 47.59.
- BIC = 41.59 + ln(60) × 3 = 41.59 + 12.28 = 53.87.
Model B improves the fit enough to beat A under AIC. Under BIC, the penalty of ln(60) ≈ 4.09 per parameter outweighs the improvement. The criteria disagree, and that is a legitimate result. The decision then rests on your purpose: prediction favors B, and parsimony favors A.
Seeing the penalty in code
The next script fits the spine's two-variable regression and prints every fit measure. It also computes AIC and BIC from the curriculum formulas so you can see how they relate to the library's values.
import numpy as np
import statsmodels.api as sm
rng = np.random.default_rng(42)
n = 200
x1 = rng.normal(size=n)
x2 = rng.normal(size=n)
y = 1.0 + 2.0 * x1 - 1.0 * x2 + rng.normal(size=n)
X = sm.add_constant(np.column_stack([x1, x2]))
model = sm.OLS(y, X).fit()
print(model.params)
print(model.tvalues)
print(model.rsquared, model.rsquared_adj)
print(model.fvalue, model.f_pvalue)
print(model.aic, model.bic)
k = 2
sse = model.ssr
aic_cfa = n * np.log(sse / n) + 2 * (k + 1)
bic_cfa = n * np.log(sse / n) + np.log(n) * (k + 1)
print(aic_cfa, bic_cfa)
print(model.aic - aic_cfa, model.bic - bic_cfa)The last line prints the same number twice. That number is n × (ln(2π) + 1), the constant that separates the library's likelihood-based criteria from the curriculum formulas. Rankings are unaffected.
Adding a useless variable
To see the penalties work, compare nested models where the last regressor is pure noise.
import numpy as np
import statsmodels.api as sm
rng = np.random.default_rng(7)
n = 120
x1 = rng.normal(size=n)
x2 = rng.normal(size=n)
noise = rng.normal(size=n)
y = 1.0 + 2.0 * x1 - 1.0 * x2 + rng.normal(size=n)
specs = {
"x1 only": np.column_stack([x1]),
"x1, x2": np.column_stack([x1, x2]),
"x1, x2, noise": np.column_stack([x1, x2, noise]),
}
for name, cols in specs.items():
res = sm.OLS(y, sm.add_constant(cols)).fit()
print(f"{name:14s} R2={res.rsquared:.4f} adjR2={res.rsquared_adj:.4f} "
f"AIC={res.aic:.2f} BIC={res.bic:.2f}")Two patterns appear in the output. R² rises or stays flat as you move down the list, even for the noise column. AIC and BIC reach their lowest values at the true specification (x1, x2) and rise when noise enters, with BIC rising by more. Adjusted R² may or may not fall, depending on the noise draw, because it only needs |t| > 1 to rise.
Choosing among the measures
Edge cases and common errors
- Different samples. Missing values can silently drop rows from one model but not another. Check that n matches before you compare any measure.
- Transformed dependent variables. AIC and BIC from a model of y and from a model of ln(y) are not comparable without adjusting for the transformation.
- A high R² is not a good model. Regressions on trending series can produce R² near 1 with a meaningless relationship. Serial correlation and misspecification can coexist with an excellent fit.
- A low R² is not a bad model. In financial returns data, a small R² can still carry a statistically and economically significant slope.
- Adjusted R² versus the F-test. Adjusted R² is a descriptive screen. To decide whether a group of added variables is jointly worth keeping, use the F-test from the previous chapter. For non-nested models the F-test does not apply, and AIC or BIC is the tool.
- Criteria do not test anything. AIC and BIC give no p-value. A one-point AIC gap is weak evidence, and larger gaps are more convincing, but no threshold is fixed by the curriculum.
Summary table
| Measure | Formula idea | Direction | Penalty for extra variables | Best used for |
|---|---|---|---|---|
| R² | 1 − SSE/SST | Higher | None | Describing fit of one model |
| Adjusted R² | Degrees-of-freedom correction | Higher | Mild | Quick comparison of models with the same y |
| AIC | n ln(SSE/n) + 2(k + 1) | Lower | 2 per parameter | Prediction |
| BIC | n ln(SSE/n) + ln(n)(k + 1) | Lower | ln(n) per parameter | Parsimonious best fit |
Exercise
A model fitted to n = 50 observations with k = 3 independent variables has R² = 0.70. (a) Compute adjusted R². (b) Another model on the same data adds two regressors and has R² = 0.71. Compute its adjusted R² and say which model adjusted R² prefers.
Show answer
(a) Adjusted R² = 1 − (49/46) × 0.30 = 1 − 1.06522 × 0.30 = 1 − 0.31957 = 0.6804. (b) With k = 5, adjusted R² = 1 − (49/44) × 0.29 = 1 − 1.11364 × 0.29 = 1 − 0.32295 = 0.6770. The larger model has a higher R² (0.71 versus 0.70) but a lower adjusted R² (0.6770 versus 0.6804), so adjusted R² prefers the three-variable model.
Exercise
Two models are fitted to the same 100 observations. Model 1 has k = 2 and SSE = 80. Model 2 has k = 3 and SSE = 76. Using n ln(SSE/n) + penalty, which model does AIC prefer, and which does BIC prefer? Use ln(100) = 4.605, ln(0.8) = −0.2231 and ln(0.76) = −0.2744.
Show answer
Model 1 first term: 100 × (−0.2231) = −22.31. Model 2 first term: 100 × (−0.2744) = −27.44. AIC: Model 1 = −22.31 + 2 × 3 = −16.31. Model 2 = −27.44 + 2 × 4 = −19.44. AIC prefers Model 2. BIC: Model 1 = −22.31 + 4.605 × 3 = −8.50. Model 2 = −27.44 + 4.605 × 4 = −9.02. BIC also prefers Model 2, but only narrowly (a gap of about 0.5), because the extra parameter costs 4.605 under BIC.
Exercise
A colleague says her model is the best because its R² is 0.95 and it beats a rival with R² = 0.90. The rival uses ln(y) as the dependent variable and hers uses y. What is wrong with the comparison?
Show answer
R² measures the share of variation in the dependent variable explained. The two models have different dependent variables (y and ln y), so their total variation, SST, is measured on different scales and the R² values are not comparable. The same applies to AIC and BIC computed on different dependent variables. A valid comparison needs a common dependent variable, for example by converting the log model's predictions back to y and comparing prediction errors.