Every linear model you fit, with lm, limma or DESeq2, has to find the coefficients \boldsymbol\beta somehow. Today: how, and why it is the Week 1 projection in disguise.
Fitting a line to a few points gives more equations than unknowns: no exact solution.
Least squares: pick the line with the smallest sum of squared residuals.
The geometry: the fitted values are the projection of the data onto the columns of X, and the residual is orthogonal to every column.
That gives the normal equationsX^\mathsf{T}X\boldsymbol\beta = X^\mathsf{T}\mathbf{y}, which is what lm solves.
When X^\mathsf{T}Xcannot be inverted, there is no unique \boldsymbol\beta.
Recap: the shadow is the closest point
From Vectors, dot product & projection: the shadow of \mathbf{u} on the line through \mathbf{v} is
Column 1 of X is all 1’s (the intercept); column 2 is the hours \mathbf{x}. This is the design matrix of y ~ x.
3 rows (samples) and 2 columns (coefficients): over-determined, Case 2 from Linear systems in R. No exact solution.
solve(X, y) refuses: X is not square.
So we change the question: not “which \boldsymbol\beta fits exactly?” but “which \boldsymbol\beta fits best?”
The best line has the smallest sum of squared residuals
For any line, the residuals are data minus fitted: \mathbf{r} = \mathbf{y} - X\boldsymbol\beta.
Least squares picks the \boldsymbol\beta that makes \lVert\mathbf{r}\rVert^2 = \mathbf{r}\cdot\mathbf{r}, the sum of squared residuals, as small as possible.
The best line is y = 0.5 + 2.5x: residuals (0.5, -1, 0.5), sum of squares 0.25 + 1 + 0.25 = 1.5. The line through the first and last point misses the middle one by 1.5: sum of squares 2.25.
The best fit is a projection onto a plane
Think of \mathbf{y} = (1, 2, 6) as one vector in 3-D, one axis per sample.
Every possible fit X\boldsymbol\beta = a\,\mathbf{1} + b\,\mathbf{x} is a combination of the two columns of X. All of them together form a plane: the column space of X.
\mathbf{y} is not in the plane (no exact solution).
The closest point of the plane to \mathbf{y} is the projection\hat{\mathbf{y}}, and the residual \mathbf{r} = \mathbf{y} - \hat{\mathbf{y}} is orthogonal to the plane.
The residual is orthogonal to every column of X
“Orthogonal to the plane” means orthogonal to both columns of X: \mathbf{1}\cdot\mathbf{r} = 0 (the residuals sum to zero) and \mathbf{x}\cdot\mathbf{r} = 0 (no trend with x left over). X^\mathsf{T} (transpose, t(X) in R) is X with rows and columns swapped, so its rows are the columns of X, and X^\mathsf{T}\mathbf{r} stacks the dot products \mathbf{1}\cdot\mathbf{r} and \mathbf{x}\cdot\mathbf{r}:
Both rows say a+b=3. There is only one independent equation for two coefficients: X^\mathsf{T}X has no inverse. (a,b)=(3,0) or (2,1) gives the same fit (3,3,3).
x <-c(1, 1, 1) # observed x values: all samples at the same timey <-c(1, 2, 6)fit <-lm(y ~ x +1)coef(fit) # (Intercept) 3 x NAfitted(fit) # 3 3 3
The fitted values are unique; the intercept and slope cannot be determined separately.
Summary
Concept
Key fact
R
Over-determined
more samples than coefficients: no exact \boldsymbol\beta
solve(X, y) fails
Least squares
minimise the sum of squared residuals \lVert\mathbf{y} - X\boldsymbol\beta\rVert^2
lm(y ~ x + 1)
Geometry
fitted values \hat{\mathbf{y}} = X\boldsymbol\beta = projection of \mathbf{y} onto the column space of X
fitted(fit)
Orthogonality
residual has dot product 0 with every column of X
t(X) %*% resid(fit)
Normal equations
X^\mathsf{T}X\boldsymbol\beta = X^\mathsf{T}\mathbf{y}: square, one equation per coefficient
solve(t(X) %*% X, t(X) %*% y)
Intercept only
the best constant is the mean
coef(lm(y ~ 1))
Not unique
dependent columns: X^\mathsf{T}X singular, lm reports NA
qr(X)$rank
Next:Design matrices for covariates: building X for real experiments (treatment, sex, age, batch). The fitting step is always the one from today.