Linear regression

core 12 minlinear-modelsregression

Builds on:Slope measures changeCommon distributions

The idea

Linear regression models the target as a weighted sum of features plus noise:

y=β0+β1x1+⋯+βpxp+ε,β^=(X⊤X)−1X⊤yy = \beta_0 + \beta_1 x_1 + \cdots + \beta_p x_p + \varepsilon, \qquad \hat{\beta} = (X^{\top}X)^{-1}X^{\top}y

OLS picks the β^\hat{\beta} minimizing the sum of squared residuals. It is the interview workhorse because every part of it can be probed:

Assumptions — know them in two tiers.

  1. For unbiased coefficients: linearity in parameters, and exogeneity (E[ε∣X]=0\mathbb{E}[\varepsilon \mid X] = 0 — no omitted confounders driving both xx and yy).
  2. For valid inference (SEs, t-tests, CIs): independent errors, homoscedasticity (constant error variance), and normal errors for exact small-sample tests. Plus no perfect multicollinearity, or (X⊤X)−1(X^{\top}X)^{-1} does not exist at all.

The distinction is the interview point: heteroscedasticity does not bias β^\hat\beta — it invalidates the error bars. Prediction survives many violations that kill inference.

Coefficients. βj\beta_j = expected change in yy for a one-unit increase in xjx_j, holding the other features fixed — a partial effect, not a marginal one, which is why coefficients shift when you add or remove correlated features. With log⁡(y)\log(y), effects become multiplicative: eβje^{\beta_j} per unit of xjx_j.

Multicollinearity. Correlated features make X⊤XX^{\top}X nearly singular: the data cannot tell which of the twins deserves the credit. Detection: VIFj=1/(1−Rj2)\text{VIF}_j = 1/(1 - R_j^2), where Rj2R_j^2 comes from regressing xjx_j on the other features; VIF >5> 5-1010 is the usual alarm, and SE(β^j)\text{SE}(\hat{\beta}_j) inflates by VIFj\sqrt{\text{VIF}_j}. Remedies: drop or combine the redundant features, ridge regression, or PCA. Predictions are mostly fine; coefficient stories are not.

Scoreboards.

R2=1−SSresSStot,Rˉ2=1−(1−R2)n−1n−p−1R^2 = 1 - \frac{SS_{res}}{SS_{tot}}, \qquad \bar{R}^2 = 1 - (1 - R^2)\frac{n-1}{n-p-1}

R2R^2 never drops when you add a feature — even noise — so adjusted R2R^2 (or CV error) is the honest comparator across model sizes. RMSE (1n∑ei2\sqrt{\frac{1}{n}\sum e_i^2}) amplifies large errors; MAE (1n∑∣ei∣\frac{1}{n}\sum |e_i|) treats them linearly and resists outliers.

Worked example

Apartment prices (in thousands of dollars) modeled on size and rooms:

price^=50+0.9⋅sqm+5⋅rooms\widehat{\text{price}} = 50 + 0.9 \cdot \text{sqm} + 5 \cdot \text{rooms}

Rˉ2=1−0.40⋅4939≈0.497\bar{R}^2 = 1 - 0.40 \cdot \frac{49}{39} \approx 0.497

Ten features bought a headline 0.60, but after the parameter tax the explanatory power is closer to half the variance.

Cheat sheet

Estimator: β^=(X⊤X)−1X⊤y\hat{\beta} = (X^{\top}X)^{-1}X^{\top}y — exists iff no perfect multicollinearity.

Assumptions ladder: linearity + exogeneity → unbiased β^\hat\beta; add independence + homoscedasticity → valid SEs (Gauss-Markov: OLS is BLUE); add normal errors → exact t/F tests.

SymptomLikely violationStandard fix
Curved residual-vs-fitted plotnon-linearitytransforms, polynomial/interaction terms
Residual fan (spread grows with fit)heteroscedasticityrobust (White) SEs, log-transform yy, WLS
Residuals autocorrelated (time series)dependent errorsNewey-West SEs, time-series models
Huge SEs, sign-flipping coefficientsmulticollinearityVIF check, drop/combine, ridge, PCA
Coefficient changes when adding a controlcorrelated features / confoundingexpected — interpret as partial effects

Metrics: R2=1−SSres/SStotR^2 = 1 - SS_{res}/SS_{tot} (train: never falls with more features; test: can be negative). Rˉ2=1−(1−R2)n−1n−p−1\bar{R}^2 = 1-(1-R^2)\frac{n-1}{n-p-1}. RMSE punishes outliers quadratically; MAE linearly.