Sub topic 2
Measuring Fit and Comparing Models in Multiple Regression
This chapter assumes you know simple linear regression, the meaning of slope and intercept, and how t-tests and p-values work.
Measuring fit and comparing models
R-squared is the share of variation in the dependent variable that the model explains. Adding any regressor never lowers R-squared, so it cannot alone justify a larger model. Adjusted R-squared penalizes extra regressors. AIC and BIC also penalize complexity, and lower values indicate a better trade-off between fit and parsimony. BIC penalizes extra variables more heavily than AIC.
Representing categories with dummy variables
A dummy variable takes the value 1 when an observation belongs to a category and 0 otherwise. For a category with two levels, one dummy is enough. Its coefficient measures the average difference between that category and the omitted baseline, holding the other regressors fixed. Including a dummy for every level plus a constant causes perfect collinearity, so drop one level.
Heteroskedasticity: detection with Breusch-Pagan and robust standard errors
Heteroskedasticity means the error variance changes with the regressors. Coefficients stay unbiased, but the usual standard errors are wrong. The Breusch-Pagan test regresses squared residuals on the regressors. A small p-value signals heteroskedasticity. Robust (HC1) standard errors fix the inference without changing the coefficients.
Serial correlation: Durbin-Watson, Breusch-Godfrey and corrected standard errors
Serial correlation means residuals are correlated across observations, which is common in time-series data. A Durbin-Watson statistic near 2 indicates no first-order serial correlation. The Breusch-Godfrey test checks for correlation at several lags. Newey-West (HAC) standard errors correct inference when serial correlation is present.
import numpy as np
import statsmodels.api as sm
from statsmodels.stats.diagnostic import het_breuschpagan, acorr_breusch_godfrey
from statsmodels.stats.stattools import durbin_watson
rng = np.random.default_rng(0)
n = 200
x = rng.normal(size=n)
d = rng.integers(0, 2, size=n)
y = 1 + 2 * x + 1.5 * d + rng.normal(size=n) * (1 + np.abs(x))
X = sm.add_constant(np.column_stack([x, d]))
model = sm.OLS(y, X).fit()
print("R2, adj R2:", model.rsquared, model.rsquared_adj)
print("AIC, BIC:", model.aic, model.bic)
_, bp_pvalue, _, _ = het_breuschpagan(model.resid, model.model.exog)
print("Breusch-Pagan p-value:", bp_pvalue)
print("Robust SEs:", sm.OLS(y, X).fit(cov_type="HC1").bse)
print("Durbin-Watson:", durbin_watson(model.resid))
_, bg_pvalue, _, _ = acorr_breusch_godfrey(model, nlags=2)
print("Breusch-Godfrey p-value:", bg_pvalue)
print("HAC SEs:", sm.OLS(y, X).fit(cov_type="HAC", cov_kwds={"maxlags": 2}).bse)Exercise
A model has R-squared 0.80, n = 200 and k = 2 regressors. What is adjusted R-squared?
Show answer
Adjusted R-squared = 1 - [(n - 1)/(n - k - 1)] x (1 - R2) = 1 - (199/197) x 0.20 = 1 - 0.2020 = 0.798, or about 0.798.