Least squares as projection

Week 3 — Least squares as projection

Quantitative biology and integrative omics

About this lecture

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 equations X^\mathsf{T}X\boldsymbol\beta = X^\mathsf{T}\mathbf{y}, which is what lm solves.
  • When X^\mathsf{T}X cannot 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

\mathbf{p} = \frac{\mathbf{u}\cdot\mathbf{v}}{\mathbf{v}\cdot\mathbf{v}}\,\mathbf{v}, \qquad (\mathbf{u} - \mathbf{p})\cdot\mathbf{v} = 0 .

  • The leftover \mathbf{u} - \mathbf{p} meets the line at a right angle.
  • Because of that right angle, \mathbf{p} is the point on the line closest to \mathbf{u}: any other point is farther away (Pythagoras).

Today we project onto a plane instead of a line. The idea is the same: drop a perpendicular.

Fitting a line to three points

A stress gene, log2 expression measured 0, 1 and 2 hours after heat shock:

hours x 0 1 2
log2 expression y 1 2 6

We want a line y = a + bx. Each point gives one equation:

a + 0b = 1, \qquad a + 1b = 2, \qquad a + 2b = 6 .

Too many equations: Case 2 again

The same three equations as a matrix system X\boldsymbol\beta = \mathbf{y}:

\underbrace{\begin{pmatrix} 1 & 0 \\ 1 & 1 \\ 1 & 2 \end{pmatrix}}_{X} \underbrace{\begin{pmatrix} a \\ b \end{pmatrix}}_{\boldsymbol\beta} = \underbrace{\begin{pmatrix} 1 \\ 2 \\ 6 \end{pmatrix}}_{\mathbf{y}}

  • 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}:

X^\mathsf{T}(\mathbf{y} - X\boldsymbol\beta) = \mathbf{0} \quad\Longrightarrow\quad \boxed{X^\mathsf{T}X\,\boldsymbol\beta = X^\mathsf{T}\mathbf{y}}

For our three points:

X^\mathsf{T}X = \begin{pmatrix} 3 & 3 \\ 3 & 5 \end{pmatrix},\quad X^\mathsf{T}\mathbf{y} = \begin{pmatrix} 9 \\ 14 \end{pmatrix} \quad\Longrightarrow\quad \begin{aligned} 3a + 3b &= 9 \\ 3a + 5b &= 14 \end{aligned}

Subtract: 2b = 5, so b = 2.5 and a = 0.5. A square 2 × 2 system: solvable with Week 1 tools.

R formulas: include or omit the intercept

The formula tells lm which coefficients to fit:

R formula Model Columns of X
y ~ x + 1 y \approx a + bx a column of 1’s and the observed x values
y ~ x + 0 y \approx bx only the observed x values
y ~ 1 y \approx a only a column of 1’s
  • + 1 includes an intercept: R estimates a; it does not fix a=1.
  • + 0 omits the intercept: the fitted line must pass through the origin.

R includes an intercept by default: y ~ x means y ~ x + 1. We write + 1 explicitly below so both coefficients are visible in the formula.

In R: this is what lm does

x <- c(0, 1, 2)
y <- c(1, 2, 6)
X <- cbind(1, x)                      # same as model.matrix(~ x)

solve(t(X) %*% X, t(X) %*% y)         # normal equations
#   [,1]
#    0.5
# x  2.5

fit <- lm(y ~ x + 1)
coef(fit)                             # (Intercept) 0.5   x 2.5
fitted(fit)                           # 0.5 3.0 5.5  = X %*% beta, the projection
resid(fit)                            # 0.5 -1.0 0.5
t(X) %*% resid(fit)                   # 0 0  (up to rounding): orthogonal

lm gives the same \boldsymbol\beta. (Inside, it uses a QR decomposition, qr.solve, which is more accurate on a computer but solves the same problem.)

Intercept only: the best constant is the mean

The smallest model, y ~ 1, has one column \mathbf{1} = (1, 1, 1). Projecting \mathbf{y} onto a single vector is exactly the Week 1 formula:

\hat{\mathbf{y}} = \frac{\mathbf{y}\cdot\mathbf{1}}{\mathbf{1}\cdot\mathbf{1}}\,\mathbf{1} = \frac{1 + 2 + 6}{3}\,\mathbf{1} = 3\cdot\mathbf{1} = (3, 3, 3).

  • The coefficient \frac{\mathbf{y}\cdot\mathbf{1}}{\mathbf{1}\cdot\mathbf{1}} is the sum divided by the count: the mean.
  • The residuals \mathbf{y} - \bar{y}\,\mathbf{1} are the deviations from the mean, and their squared length is what the variance is built from.
coef(lm(y ~ 1))   # 3
mean(y)           # 3

Dependent columns give no unique coefficients

Fit y \approx a+bx to \mathbf{y}=(1,2,6), now with all x=1. The data and model are:

\underbrace{\begin{pmatrix}1&1\\1&1\\1&1\end{pmatrix}}_{X} \underbrace{\begin{pmatrix}a\\b\end{pmatrix}}_{\boldsymbol\beta} \approx \underbrace{\begin{pmatrix}1\\2\\6\end{pmatrix}}_{\mathbf{y}}.

To find the least-squares fit, form X^\mathsf{T}X\boldsymbol\beta=X^\mathsf{T}\mathbf{y}:

\begin{pmatrix}3&3\\3&3\end{pmatrix} \begin{pmatrix}a\\b\end{pmatrix} = \begin{pmatrix}1+2+6\\1+2+6\end{pmatrix} = \begin{pmatrix}9\\9\end{pmatrix} \quad\Longrightarrow\quad \begin{cases}3a+3b=9\\3a+3b=9.\end{cases}

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 time
y <- c(1, 2, 6)
fit <- lm(y ~ x + 1)
coef(fit)       # (Intercept) 3   x NA
fitted(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.