DS701 Session 12 — Wed Oct 14, 2026
In one or two sentences: what does the least-squares line minimize, and what are the normal equations that its coefficients \(\hat\beta\) satisfy? (Write the equations; no derivation needed.)
\(R^2 = 1 - \text{RSS}/\text{TSS}\) is “the fraction of the variance of \(y\) explained by the model.” Explain why \(R^2\) can never go down when you add another feature (column) to \(X\) and refit — and why that makes a high in-sample \(R^2\) weak evidence on its own that the model is good.
A colleague says “linear regression can only fit straight lines, so it is useless for my data, which clearly curves.” Using the lecture’s fits of a parabola and of \(y = \beta_0 + \beta_1 \log x\), explain what “linear” in linear model* actually refers to, and what changes (and what does not) in the least-squares procedure when you fit a curve.*



Once \(X\) is built, the procedure never changes:
| model | columns of \(X\) |
|---|---|
| line | \(\mathbf{1},\ x\) |
| parabola | \(\mathbf{1},\ x,\ x^2\) |
| log | \(\mathbf{1},\ \log x\) |
| plane / multiple regression | \(\mathbf{1},\ u,\ v,\ \dots\) |
| trend surface | \(\mathbf{1},\ u,\ v,\ u^2,\ uv,\ v^2\) |
“Linear” = linear in \(\beta\). Requirements for a unique \(\hat\beta\): full column rank (\(n \ge p\), no column a combination of the others) — otherwise \(X^\top X\) is singular and the coefficients, though not the fit, are undetermined.
From the Ames model (seven features, sale price in dollars):
| feature | \(\hat\beta\) | reads as |
|---|---|---|
| gross living area | $48 / sq ft | one more square foot, other features held fixed |
| full bath | $14,900 | one more full bath, others fixed |
| garage area | $99 / sq ft | |
| lot area | $0.23 / sq ft | CI includes 0 — not distinguishable from no effect |

\[R^2 = \frac{\text{ESS}}{\text{TSS}} = 1 - \frac{\text{RSS}}{\text{TSS}} \in [0, 1]\]

Goal: on a real dataset (200 diabetes patients — your section’s own subset), build \(X\) with an intercept, solve the normal equations, verify against lstsq and scikit-learn, compute \(R^2\); form the projection matrix and check the residual is orthogonal to the columns; add a correlated feature and watch the coefficients move. Then the stretch: a diagnostics function, a mystery dataset with a hidden problem, and the fix.
numpy, pandas, matplotlib, scipy, scikit-learn.The activity notebook goes live on the day of the lecture. Colab is optional — you can also open it on GitHub and run it locally.
Full lecture notes: Linear Regression