4  Multiple Linear Regression in Matrix Form

When the response depends on several regressors, the scalar formulae of Chapter 3 become unwieldy. Matrix notation expresses the whole theory of linear regression in a few compact results that hold for any number of regressors (Draper and Smith 1998; Montgomery, Peck, and Vining 2021; Rao 1973). This chapter derives the least squares estimator, introduces the hat matrix, studies the residuals, and obtains an unbiased estimator of \(\sigma^2\). The results are illustrated on a small data set with twelve observations and two regressors that is used throughout the book.

4.1 The model and its assumptions

\[\mathbf y=\mathbf X\boldsymbol\beta+\boldsymbol\varepsilon,\qquad E(\boldsymbol\varepsilon)=\mathbf 0,\qquad D(\boldsymbol\varepsilon)=E(\boldsymbol\varepsilon\boldsymbol\varepsilon')=\sigma^2\mathbf I_n \tag{4.1}\]

where \(\mathbf y\) is \(n\times1\), \(\mathbf X\) is \(n\times p\) of full column rank \(p\), \(\boldsymbol\beta\) is \(p\times1\) and \(\boldsymbol\varepsilon\) is \(n\times1\). Consequently \(E(\mathbf y)=\mathbf X\boldsymbol\beta\) and \(D(\mathbf y)=\sigma^2\mathbf I_n\).

4.2 Ordinary least squares estimation

The error vector is \(\boldsymbol\varepsilon=\mathbf y-\mathbf X\boldsymbol\beta\). The method of least squares chooses \(\boldsymbol\beta\) to minimize the error sum of squares

\[S(\boldsymbol\beta)=\boldsymbol\varepsilon'\boldsymbol\varepsilon=\sum_{i=1}^n\varepsilon_i^2=(\mathbf y-\mathbf X\boldsymbol\beta)'(\mathbf y-\mathbf X\boldsymbol\beta).\]

Expanding,

\[\boldsymbol\varepsilon'\boldsymbol\varepsilon=\mathbf y'\mathbf y-\underset{1\times n}{\mathbf y'}\,\underset{n\times p}{\mathbf X}\,\underset{p\times1}{\boldsymbol\beta}-\boldsymbol\beta'\mathbf X'\mathbf y+\boldsymbol\beta'\mathbf X'\mathbf X\boldsymbol\beta.\]

Each of the two middle terms is a scalar (\(1\times1\)), and a scalar equals its own transpose, so \(\mathbf y'\mathbf X\boldsymbol\beta=(\mathbf y'\mathbf X\boldsymbol\beta)'=\boldsymbol\beta'\mathbf X'\mathbf y\). Hence

\[\boldsymbol\varepsilon'\boldsymbol\varepsilon=\mathbf y'\mathbf y-2\boldsymbol\beta'\mathbf X'\mathbf y+\boldsymbol\beta'\mathbf X'\mathbf X\boldsymbol\beta. \tag{4.2}\]

Using the rules of vector differentiation \(\partial(\boldsymbol\beta'\mathbf a)/\partial\boldsymbol\beta=\mathbf a\) and \(\partial(\boldsymbol\beta'\mathbf A\boldsymbol\beta)/\partial\boldsymbol\beta=2\mathbf A\boldsymbol\beta\) for symmetric \(\mathbf A\) (Appendix A), differentiate Equation 4.2 with respect to \(\boldsymbol\beta\) and equate to zero:

\[\frac{\partial\,\boldsymbol\varepsilon'\boldsymbol\varepsilon}{\partial\boldsymbol\beta}=-2\mathbf X'\mathbf y+2\mathbf X'\mathbf X\boldsymbol\beta=\mathbf 0.\]

This gives the normal equations

\[\mathbf X'\mathbf X\hat{\boldsymbol\beta}=\mathbf X'\mathbf y \tag{4.3}\]

and, since \(\mathbf X\) has full column rank so that \(\mathbf X'\mathbf X\) is non-singular, the ordinary least squares estimator (OLSE)

\[\boxed{\hat{\boldsymbol\beta}=(\mathbf X'\mathbf X)^{-1}\mathbf X'\mathbf y} \tag{4.4}\]

The solution is a minimum

The matrix of second derivatives is

\[\frac{\partial^2\,\boldsymbol\varepsilon'\boldsymbol\varepsilon}{\partial\boldsymbol\beta\,\partial\boldsymbol\beta'}=2\mathbf X'\mathbf X.\]

Since \(\mathbf X\) is of full rank, \(\mathbf X'\mathbf X\) is a positive definite matrix: it is symmetric and all its eigenvalues are positive (for any \(\mathbf c\neq\mathbf 0\), \(\mathbf c'\mathbf X'\mathbf X\mathbf c=\|\mathbf X\mathbf c\|^2>0\)). Hence \(\hat{\boldsymbol\beta}\) minimizes \(\boldsymbol\varepsilon'\boldsymbol\varepsilon\).

Note

For the simple linear regression model, \(\mathbf X=[\mathbf 1\ \ \mathbf x]\) and

\[\mathbf X'\mathbf X=\begin{bmatrix}n&\sum x_i\\\sum x_i&\sum x_i^2\end{bmatrix},\qquad \mathbf X'\mathbf y=\begin{bmatrix}\sum y_i\\\sum x_iy_i\end{bmatrix},\]

so Equation 4.3 reproduces the scalar normal equations Equation 3.2, and Equation 4.4 reproduces Equation 3.3.

4.3 Fitted values and the hat matrix

If \(\hat{\boldsymbol\beta}\) is the estimator of \(\boldsymbol\beta\) for the model \(\mathbf y=\mathbf X\boldsymbol\beta+\boldsymbol\varepsilon\), the vector of fitted values is

\[\hat{\mathbf y}=\mathbf X\hat{\boldsymbol\beta}=\mathbf X(\mathbf X'\mathbf X)^{-1}\mathbf X'\mathbf y=\mathbf H\mathbf y, \tag{4.5}\]

where

\[\underset{n\times n}{\mathbf H}=\underset{n\times p}{\mathbf X}\ \underset{p\times p}{(\mathbf X'\mathbf X)^{-1}}\ \underset{p\times n}{\mathbf X'} \tag{4.6}\]

is called the hat matrix, because it “puts the hat on \(\mathbf y\)” (Hoaglin and Welsch 1978). Geometrically, \(\mathbf H\) is the orthogonal projection onto the column space of \(\mathbf X\): \(\hat{\mathbf y}\) is the point in that space closest to \(\mathbf y\).

Properties of the hat matrix

  1. Symmetric: \(\mathbf H'=\left[\mathbf X(\mathbf X'\mathbf X)^{-1}\mathbf X'\right]'=\mathbf X(\mathbf X'\mathbf X)^{-1}\mathbf X'=\mathbf H\), because \((\mathbf X'\mathbf X)^{-1}\) is symmetric.

  2. Idempotent: \(\mathbf H\mathbf H=\mathbf X(\mathbf X'\mathbf X)^{-1}\mathbf X'\mathbf X(\mathbf X'\mathbf X)^{-1}\mathbf X'=\mathbf X(\mathbf X'\mathbf X)^{-1}\mathbf X'=\mathbf H\).

  3. Trace equals rank: using \(\operatorname{tr}(\mathbf A\mathbf B)=\operatorname{tr}(\mathbf B\mathbf A)\), \[\operatorname{tr}(\mathbf H)=\operatorname{tr}\left[\mathbf X(\mathbf X'\mathbf X)^{-1}\mathbf X'\right]=\operatorname{tr}\left[\underset{p\times n}{\mathbf X'}\underset{n\times p}{\mathbf X}(\mathbf X'\mathbf X)^{-1}\right]=\operatorname{tr}(\mathbf I_p)=p=\operatorname{rank}(\mathbf X).\]

  4. \(\mathbf H\mathbf X=\mathbf X\): projecting a vector already in the column space leaves it unchanged.

  5. The diagonal elements satisfy \(0\le h_{ii}\le1\) (and \(1/n\le h_{ii}\) when the model has an intercept), with \(\sum h_{ii}=p\).

Note

The trace of a square matrix is the sum of its main diagonal elements; it is defined only for square matrices. The trace of a matrix also equals the sum of its eigenvalues. An idempotent matrix has eigenvalues 0 or 1 only, so its trace equals its rank.

4.4 Residuals

The residual is the difference between the observed and fitted values of the study variable:

\[\mathbf e=\mathbf y-\hat{\mathbf y}=\mathbf y-\mathbf X\hat{\boldsymbol\beta}=\mathbf y-\mathbf H\mathbf y=(\mathbf I-\mathbf H)\mathbf y=\bar{\mathbf H}\mathbf y,\qquad \bar{\mathbf H}=\mathbf I-\mathbf H. \tag{4.7}\]

Properties of \(\mathbf I-\mathbf H\)

  1. \(\mathbf I-\mathbf H\) is symmetric.
  2. \((\mathbf I-\mathbf H)(\mathbf I-\mathbf H)=\mathbf I-2\mathbf H+\mathbf H\mathbf H=\mathbf I-\mathbf H\): it is idempotent.
  3. \(\operatorname{tr}(\mathbf I-\mathbf H)=\operatorname{tr}(\mathbf I_n)-\operatorname{tr}(\mathbf H)=n-p\).
  4. \((\mathbf I-\mathbf H)\mathbf X=\mathbf X-\mathbf X=\mathbf 0\), and therefore \(\mathbf X'(\mathbf I-\mathbf H)=\mathbf 0\).

Property 4 gives \(\mathbf X'\mathbf e=\mathbf 0\): the residuals are orthogonal to every column of \(\mathbf X\). With an intercept, the first column is \(\mathbf 1\), so \(\sum e_i=0\). It also gives a useful expression of the residuals in terms of the unobservable errors:

\[\mathbf e=(\mathbf I-\mathbf H)\mathbf y=(\mathbf I-\mathbf H)(\mathbf X\boldsymbol\beta+\boldsymbol\varepsilon)=(\mathbf I-\mathbf H)\boldsymbol\varepsilon. \tag{4.8}\]

4.5 Properties of the least squares estimator

(i) Estimation error

\[\begin{aligned} \hat{\boldsymbol\beta}-\boldsymbol\beta&=(\mathbf X'\mathbf X)^{-1}\mathbf X'\mathbf y-\boldsymbol\beta\\ &=(\mathbf X'\mathbf X)^{-1}\mathbf X'(\mathbf X\boldsymbol\beta+\boldsymbol\varepsilon)-\boldsymbol\beta\\ &=(\mathbf X'\mathbf X)^{-1}\mathbf X'\mathbf X\boldsymbol\beta+(\mathbf X'\mathbf X)^{-1}\mathbf X'\boldsymbol\varepsilon-\boldsymbol\beta\\ &=(\mathbf X'\mathbf X)^{-1}\mathbf X'\boldsymbol\varepsilon. \end{aligned} \tag{4.9}\]

(ii) Bias

\[E(\hat{\boldsymbol\beta}-\boldsymbol\beta)=(\mathbf X'\mathbf X)^{-1}\mathbf X'E(\boldsymbol\varepsilon)=\mathbf 0.\]

So \(\hat{\boldsymbol\beta}\) is an unbiased estimator of \(\boldsymbol\beta\). Equivalently, \(E(\hat{\boldsymbol\beta})=(\mathbf X'\mathbf X)^{-1}\mathbf X'E(\mathbf y)=(\mathbf X'\mathbf X)^{-1}\mathbf X'\mathbf X\boldsymbol\beta=\boldsymbol\beta\).

(iii) Covariance matrix

\[\begin{aligned} V(\hat{\boldsymbol\beta})&=E\left[(\hat{\boldsymbol\beta}-E(\hat{\boldsymbol\beta}))(\hat{\boldsymbol\beta}-E(\hat{\boldsymbol\beta}))'\right]=E\left[(\hat{\boldsymbol\beta}-\boldsymbol\beta)(\hat{\boldsymbol\beta}-\boldsymbol\beta)'\right]\\ &=E\left[(\mathbf X'\mathbf X)^{-1}\mathbf X'\boldsymbol\varepsilon\boldsymbol\varepsilon'\mathbf X(\mathbf X'\mathbf X)^{-1}\right]\\ &=(\mathbf X'\mathbf X)^{-1}\mathbf X'E(\boldsymbol\varepsilon\boldsymbol\varepsilon')\mathbf X(\mathbf X'\mathbf X)^{-1}\\ &=\sigma^2(\mathbf X'\mathbf X)^{-1}\mathbf X'\mathbf I\mathbf X(\mathbf X'\mathbf X)^{-1}\\ &=\sigma^2(\mathbf X'\mathbf X)^{-1}. \end{aligned} \tag{4.10}\]

The diagonal elements \(\sigma^2C_{jj}\) are the variances of the individual estimates \(\hat\beta_j\), and the off-diagonal elements \(\sigma^2C_{ij}\) are their covariances.

(iv) Variance of fitted values and residuals

\[V(\hat{\mathbf y})=V(\mathbf X\hat{\boldsymbol\beta})=\mathbf XV(\hat{\boldsymbol\beta})\mathbf X'=\sigma^2\mathbf X(\mathbf X'\mathbf X)^{-1}\mathbf X'=\sigma^2\mathbf H, \tag{4.11}\]

\[V(\mathbf e)=(\mathbf I-\mathbf H)V(\mathbf y)(\mathbf I-\mathbf H)'=\sigma^2(\mathbf I-\mathbf H). \tag{4.12}\]

So \(V(\hat y_i)=\sigma^2h_{ii}\) and \(V(e_i)=\sigma^2(1-h_{ii})\): residuals do not have equal variance even when the errors do, a fact used in Chapter 8.

4.6 Estimation of \(\sigma^2\)

The principle of least squares cannot be used to estimate \(\sigma^2\), because \(\sigma^2\) does not appear in \(\boldsymbol\varepsilon'\boldsymbol\varepsilon\). Since \(E(\varepsilon_i^2)=\sigma^2\), we attempt to estimate \(\sigma^2\) from the residuals \(e_i\), which are the observable counterparts of the \(\varepsilon_i\).

From Equation 4.7, and because \(\mathbf I-\mathbf H\) is symmetric and idempotent,

\[SS_{Res}=\mathbf e'\mathbf e=\mathbf y'(\mathbf I-\mathbf H)'(\mathbf I-\mathbf H)\mathbf y=\mathbf y'(\mathbf I-\mathbf H)\mathbf y. \tag{4.13}\]

Substituting \(\mathbf y=\mathbf X\boldsymbol\beta+\boldsymbol\varepsilon\) and using \((\mathbf I-\mathbf H)\mathbf X=\mathbf 0\),

\[\mathbf e'\mathbf e=(\mathbf X\boldsymbol\beta+\boldsymbol\varepsilon)'(\mathbf I-\mathbf H)(\mathbf X\boldsymbol\beta+\boldsymbol\varepsilon)=\boldsymbol\varepsilon'(\mathbf I-\mathbf H)\boldsymbol\varepsilon.\]

For any symmetric matrix \(\mathbf A\), \(E(\boldsymbol\varepsilon'\mathbf A\boldsymbol\varepsilon)=\operatorname{tr}[\mathbf A\,E(\boldsymbol\varepsilon\boldsymbol\varepsilon')]=\sigma^2\operatorname{tr}(\mathbf A)\) when \(E(\boldsymbol\varepsilon)=\mathbf 0\). Therefore

\[E(SS_{Res})=\sigma^2\operatorname{tr}(\mathbf I-\mathbf H)=\sigma^2(n-p),\qquad\text{i.e.}\qquad E\left[\frac{\mathbf y'(\mathbf I-\mathbf H)\mathbf y}{n-p}\right]=E\left[\frac{SS_{Res}}{n-p}\right]=\sigma^2.\]

Further, if \(\boldsymbol\varepsilon\sim N(\mathbf 0,\sigma^2\mathbf I)\), so that \(\mathbf y\sim N(\mathbf X\boldsymbol\beta,\sigma^2\mathbf I)\), a quadratic form in an idempotent matrix of rank \(n-p\) gives

\[\frac{\mathbf y'(\mathbf I-\mathbf H)\mathbf y}{\sigma^2}\sim\chi^2_{n-p}. \tag{4.14}\]

Hence

\[\boxed{\hat\sigma^2=MS_{Res}=\frac{SS_{Res}}{n-p}=s^2} \tag{4.15}\]

the mean sum of squares due to residual, is the unbiased estimator of \(\sigma^2\). A convenient computing formula is

\[SS_{Res}=\mathbf y'\mathbf y-\mathbf y'\mathbf H\mathbf y=\mathbf y'\mathbf y-\mathbf y'\mathbf X(\mathbf X'\mathbf X)^{-1}\mathbf X'\mathbf y=\mathbf y'\mathbf y-\hat{\boldsymbol\beta}'\mathbf X'\mathbf y. \tag{4.16}\]

The estimated covariance matrix of \(\hat{\boldsymbol\beta}\) is then

\[\hat V(\hat{\boldsymbol\beta})=s^2(\mathbf X'\mathbf X)^{-1}. \tag{4.17}\]

4.7 Example for illustration

Example 4.1 The data in Table 4.1 (csv/notes12.csv) give a response \(y\) and two regressors \(x_1\) and \(x_2\) for twelve units. Fit the model \(y=\beta_0+\beta_1x_1+\beta_2x_2+\varepsilon\) using matrices, and estimate \(\sigma^2\) and the covariance matrix of \(\hat{\boldsymbol\beta}\).

Table 4.1: Data for the illustrative example
Sl No. y \(x_1\) \(x_2\)
1 2 0 2
2 3 2 6
3 2 2 7
4 7 2 5
5 6 4 9
6 8 4 8
7 10 4 7
8 7 6 10
9 8 6 11
10 12 6 9
11 11 8 15
12 14 8 13

Solution

Step 1: Form \(\mathbf y\) and \(\mathbf X\). The first column of \(\mathbf X\) is a column of ones for the intercept.

\[\mathbf y=\begin{bmatrix}2 \\ 3 \\ 2 \\ 7 \\ 6 \\ 8 \\ 10 \\ 7 \\ 8 \\ 12 \\ 11 \\ 14\end{bmatrix},\qquad \mathbf X=\begin{bmatrix}1 & 0 & 2 \\ 1 & 2 & 6 \\ 1 & 2 & 7 \\ 1 & 2 & 5 \\ 1 & 4 & 9 \\ 1 & 4 & 8 \\ 1 & 4 & 7 \\ 1 & 6 & 10 \\ 1 & 6 & 11 \\ 1 & 6 & 9 \\ 1 & 8 & 15 \\ 1 & 8 & 13\end{bmatrix}\]

Step 2: Compute \(\mathbf X'\mathbf X\) and \(\mathbf X'\mathbf y\).

\[\mathbf X'\mathbf X=\begin{bmatrix}n&\sum x_1&\sum x_2\\\sum x_1&\sum x_1^2&\sum x_1x_2\\\sum x_2&\sum x_1x_2&\sum x_2^2\end{bmatrix}=\begin{bmatrix}12 & 52 & 102 \\ 52 & 296 & 536 \\ 102 & 536 & 1004\end{bmatrix},\qquad \mathbf X'\mathbf y=\begin{bmatrix}\sum y\\\sum x_1y\\\sum x_2y\end{bmatrix}=\begin{bmatrix}90 \\ 482 \\ 872\end{bmatrix}\]

Step 3: Invert \(\mathbf X'\mathbf X\) (\(|\mathbf X'\mathbf X|=10144\)):

\[(\mathbf X'\mathbf X)^{-1}=\begin{bmatrix}0.9748 & 0.2429 & -0.2287 \\ 0.2429 & 0.1621 & -0.1112 \\ -0.2287 & -0.1112 & 0.0836\end{bmatrix}\]

Step 4: The least squares estimates, by Equation 4.4:

\[\hat{\boldsymbol\beta}=(\mathbf X'\mathbf X)^{-1}\mathbf X'\mathbf y=\begin{bmatrix}0.9748 & 0.2429 & -0.2287 \\ 0.2429 & 0.1621 & -0.1112 \\ -0.2287 & -0.1112 & 0.0836\end{bmatrix}\begin{bmatrix}90 \\ 482 \\ 872\end{bmatrix}=\begin{bmatrix}5.375 \\ 3.012 \\ -1.285\end{bmatrix}\]

The fitted equation is \(\hat y=5.375+3.012x_1-1.285x_2\).

Step 5: Residual sum of squares, by Equation 4.16, with \(\mathbf y'\mathbf y=\sum y_i^2=840\):

\[SS_{Res}=\mathbf y'\mathbf y-\hat{\boldsymbol\beta}'\mathbf X'\mathbf y=840-\begin{bmatrix}5.375&3.012&-1.285\end{bmatrix}\begin{bmatrix}90 \\ 482 \\ 872\end{bmatrix}=840-814.541=25.459.\]

Step 6: Estimate of \(\sigma^2\), by Equation 4.15:

\[s^2=\frac{SS_{Res}}{n-p}=\frac{25.459}{12-3}=2.829.\]

Step 7: Estimated covariance matrix of \(\hat{\boldsymbol\beta}\), by Equation 4.17:

\[\hat V(\hat{\boldsymbol\beta})=s^2(\mathbf X'\mathbf X)^{-1}=2.829\begin{bmatrix}0.9748 & 0.2429 & -0.2287 \\ 0.2429 & 0.1621 & -0.1112 \\ -0.2287 & -0.1112 & 0.0836\end{bmatrix}=\begin{bmatrix}2.7574 & 0.6871 & -0.6470 \\ 0.6871 & 0.4584 & -0.3146 \\ -0.6470 & -0.3146 & 0.2365\end{bmatrix}\]

The standard errors are the square roots of the diagonal: \(SE(\hat\beta_0)=1.6605\), \(SE(\hat\beta_1)=0.6771\) and \(SE(\hat\beta_2)=0.4863\).

As a check, the hat matrix has trace \(\operatorname{tr}(\mathbf H)=3.000=p\), and the residuals \(\mathbf e=(\mathbf I-\mathbf H)\mathbf y\) sum to 0.000.

Note

In the regression of \(y\) on \(x_1\) alone, \(x_1\) has a positive effect, and \(x_2\) is also positively correlated with \(y\) (0.712). Yet in the joint model the coefficient of \(x_2\) is negative. Each coefficient in a multiple regression is a partial effect: the change in mean \(y\) per unit change in \(x_j\) when the other regressors are held fixed. Because \(x_1\) and \(x_2\) are strongly correlated (0.955), the partial and simple effects can differ even in sign.

4.8 R practical

Fitting with lm()

d12 <- read.csv("csv/notes12.csv")
rlg <- lm(y ~ x1 + x2, data = d12)
summary(rlg)
#> 
#> Call:
#> lm(formula = y ~ x1 + x2, data = d12)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -3.5915 -0.7157  0.1349  0.9562  2.0284 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)   
#> (Intercept)   5.3754     1.6605   3.237   0.0102 * 
#> x1            3.0118     0.6771   4.448   0.0016 **
#> x2           -1.2855     0.4863  -2.643   0.0268 * 
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 1.682 on 9 degrees of freedom
#> Multiple R-squared:  0.8457, Adjusted R-squared:  0.8114 
#> F-statistic: 24.66 on 2 and 9 DF,  p-value: 0.0002226

Scatter plots

plot(d12$x1, d12$y, xlab = "x1", ylab = "y", pch = 19)   # one pair
pairs(d12, pch = 19, col = "darkgreen")                    # all pairs
Figure 4.1: Scatter plot matrix of the illustrative data
Figure 4.2: Scatter plot matrix of the illustrative data

The same results by matrix algebra

y <- d12$y
X <- cbind(1, d12$x1, d12$x2)            # or model.matrix(rlg)
XtX  <- t(X) %*% X
XtXi <- solve(XtX)
b    <- XtXi %*% t(X) %*% y              # beta-hat
H    <- X %*% XtXi %*% t(X)              # hat matrix
e    <- (diag(12) - H) %*% y             # residuals
SSres <- sum(e^2); s2 <- SSres / (12 - 3)
round(b, 4); SSres; s2
#>         [,1]
#> [1,]  5.3754
#> [2,]  3.0118
#> [3,] -1.2855
#> [1] 25.45899
#> [1] 2.828777
round(s2 * XtXi, 4)                      # same as vcov(rlg)
#>         [,1]    [,2]    [,3]
#> [1,]  2.7574  0.6871 -0.6470
#> [2,]  0.6871  0.4584 -0.3146
#> [3,] -0.6470 -0.3146  0.2365
sum(diag(H))                             # trace = p = 3
#> [1] 3
Exercise

Write the nitrogen–yield data of Example 3.1 in matrix–vector form (\(\mathbf y\), \(\mathbf X\) with a column of ones), compute \(\mathbf X'\mathbf X\), \(\mathbf X'\mathbf y\), \((\mathbf X'\mathbf X)^{-1}\), \(\hat{\boldsymbol\beta}\), \(SS_{Res}\), \(s^2\) and \(\hat V(\hat{\boldsymbol\beta})\), and verify that they agree with the scalar results of Chapter 3. (Solution: Question 10 of the chapter summary.)

Historical Insights

Matrices enter statistics

Although Gauss solved his normal equations by systematic elimination in 1809, matrix notation was not used in regression until the 1930s. A. C. Aitken, a New Zealand mathematician famous for his prodigious mental arithmetic, wrote the least squares problem as \(\mathbf y=\mathbf X\boldsymbol\beta+\boldsymbol\varepsilon\) and derived the generalized least squares estimator \((\mathbf X'\mathbf V^{-1}\mathbf X)^{-1}\mathbf X'\mathbf V^{-1}\mathbf y\) in 1935 (Aitken 1935). The compact notation made it possible to state results such as \(V(\hat{\boldsymbol\beta})=\sigma^2(\mathbf X'\mathbf X)^{-1}\) once for any number of regressors, and later made regression ideally suited to the digital computer.

Quotes to Inspire

“The purpose of computing is insight, not numbers.”
- Richard Hamming

4.9 Practical exercises

4.9.1 Practical 4.1: Fitting a two-regressor model by matrices

Milk yield (kg/day, \(y\)) of six crossbred cows, with concentrate fed (kg/day, \(x_1\)) and green fodder fed (tens of kg/day, \(x_2\)), is given below. Using matrix methods, find \(\hat{\boldsymbol\beta}\), \(SS_{Res}\), \(s^2\) and the standard errors of the estimates.

Table 4.2: Milk yield, concentrate and green fodder
Cow 1 2 3 4 5 6
\(y\) 10 12 15 18 20 25
\(x_1\) 1 2 3 4 5 6
\(x_2\) 2 1 3 2 4 5

Solution

\[\mathbf X'\mathbf X=\begin{bmatrix}6 & 21 & 17 \\ 21 & 91 & 71 \\ 17 & 71 & 59\end{bmatrix},\qquad \mathbf X'\mathbf y=\begin{bmatrix}100 \\ 401 \\ 318\end{bmatrix},\qquad |\mathbf X'\mathbf X|=344,\]

\[(\mathbf X'\mathbf X)^{-1}=\begin{bmatrix}0.9535 & -0.0930 & -0.1628 \\ -0.0930 & 0.1890 & -0.2006 \\ -0.1628 & -0.2006 & 0.3052\end{bmatrix},\qquad \hat{\boldsymbol\beta}=(\mathbf X'\mathbf X)^{-1}\mathbf X'\mathbf y=\begin{bmatrix}6.2791 \\ 2.6831 \\ 0.3517\end{bmatrix}.\]

So \(\hat y=6.279+2.683x_1+0.352x_2\). With \(\mathbf y'\mathbf y=1818\) and \(\hat{\boldsymbol\beta}'\mathbf X'\mathbf y=1815.7006\), Equation 4.16 gives \(SS_{Res}=2.2994\) and \(s^2=2.2994/3=0.7665\). The standard errors \(\sqrt{s^2C_{jj}}\) are 0.8549, 0.3806 and 0.4837.

Doing it in R

cows <- data.frame(y = c(10, 12, 15, 18, 20, 25), x1 = 1:6, x2 = c(2, 1, 3, 2, 4, 5))
Xc <- model.matrix(~ x1 + x2, data = cows); yc <- cows$y
XtXc <- crossprod(Xc); Xtyc <- crossprod(Xc, yc)
bc <- solve(XtXc, Xtyc)                       # beta-hat
Hc <- Xc %*% solve(XtXc) %*% t(Xc)             # hat matrix
ec <- yc - Xc %*% bc; SSresc <- sum(ec^2); s2c <- SSresc / (nrow(Xc) - ncol(Xc))
cbind(estimate = drop(bc), se = sqrt(diag(s2c * solve(XtXc))))
#>              estimate        se
#> (Intercept) 6.2790698 0.8548818
#> x1          2.6831395 0.3805624
#> x2          0.3517442 0.4836863
sum(diag(Hc))                               # trace = p
#> [1] 3
summary(lm(y ~ x1 + x2, data = cows))$coef # check
#>              Estimate Std. Error   t value    Pr(>|t|)
#> (Intercept) 6.2790698  0.8548818 7.3449563 0.005215019
#> x1          2.6831395  0.3805624 7.0504593 0.005864454
#> x2          0.3517442  0.4836863 0.7272155 0.519705878

4.9.2 Practical 4.2: Properties of the hat matrix

For the data of Practical 4.1, verify numerically that \(\mathbf Hc\) is symmetric and idempotent, that \(\operatorname{tr}(\mathbf Hc)=3\), that \((\mathbf Ic-\mathbf Hc)\mathbf Xc=\mathbf 0\) and that \(\sum e_i=0\).

Solution

The checks in R below return TRUE for symmetry and idempotency (up to rounding), a trace of 3, a zero matrix for \((\mathbf Ic-\mathbf Hc)\mathbf Xc\) and a residual sum of zero, as proved in Section 4.3 and Section 4.4.

Doing it in R

Ic <- diag(6)
all.equal(Hc, t(Hc))                         # symmetric
#> [1] TRUE
all.equal(Hc %*% Hc, Hc)                      # idempotent
#> [1] TRUE
sum(diag(Hc)); sum(diag(Ic - Hc))             # p and n - p
#> [1] 3
#> [1] 3
round((Ic - Hc) %*% Xc, 10)                   # zero matrix
#>   (Intercept) x1 x2
#> 1           0  0  0
#> 2           0  0  0
#> 3           0  0  0
#> 4           0  0  0
#> 5           0  0  0
#> 6           0  0  0
round(sum(ec), 10)                          # residuals sum to zero
#> [1] 0

4.10 Chapter Summary

Fill in the blanks

Answers are given at the end of the chapter.

  1. The normal equations in matrix form are __________.

  2. The ordinary least squares estimator is \(\hat{\boldsymbol\beta}=\) __________.

  3. The matrix of second derivatives of \(\boldsymbol\varepsilon'\boldsymbol\varepsilon\) is \(2\mathbf X'\mathbf X\), which is positive __________.

  4. The hat matrix is \(\mathbf H=\) __________.

  5. A matrix \(\mathbf A\) with \(\mathbf A\mathbf A=\mathbf A\) is called __________.

  6. \(\operatorname{tr}(\mathbf H)=\) __________ and \(\operatorname{tr}(\mathbf I-\mathbf H)=\) __________.

  7. The covariance matrix of \(\hat{\boldsymbol\beta}\) is __________.

  8. \(V(\hat{\mathbf y})=\) __________ and \(V(\mathbf e)=\) __________.

  9. Under normality, \(SS_{Res}/\sigma^2\) follows a __________ distribution with __________ degrees of freedom.

  10. In Example 4.1, \(s^2=\) __________.

Short-answer questions

  1. Derive the normal equations of multiple regression in matrix form.

  2. Why is \(\mathbf y'\mathbf X\boldsymbol\beta=\boldsymbol\beta'\mathbf X'\mathbf y\)?

  3. Show that the least squares solution minimizes \(\boldsymbol\varepsilon'\boldsymbol\varepsilon\).

  4. Define the hat matrix and prove that it is symmetric and idempotent.

  5. Prove that \(\operatorname{tr}(\mathbf H)=p\).

  6. Show that \(\mathbf e=(\mathbf I-\mathbf H)\boldsymbol\varepsilon\).

  7. Prove that \(\hat{\boldsymbol\beta}\) is unbiased and find its covariance matrix.

  8. Why can least squares not be used to estimate \(\sigma^2\)?

  9. Prove that \(MS_{Res}\) is an unbiased estimator of \(\sigma^2\).

  10. Explain why a coefficient in multiple regression may differ in sign from the corresponding simple correlation.

Numerical and conceptual questions

Answers are given at the end of the chapter.

  1. A model with intercept and three regressors is fitted to 25 observations. Give the orders of \(\mathbf X\), \(\mathbf X'\mathbf X\), \(\mathbf H\) and \(\hat{\boldsymbol\beta}\).

  2. For Question 1, find \(\operatorname{tr}(\mathbf H)\) and the average hat value.

  3. Given \(\mathbf X'\mathbf X=\begin{bmatrix}4&8\\8&20\end{bmatrix}\) and \(\mathbf X'\mathbf y=\begin{bmatrix}12\\28\end{bmatrix}\), find \(\hat{\boldsymbol\beta}\).

  4. For Question 3, if \(\mathbf y'\mathbf y=40\) and \(n=4\), find \(SS_{Res}\) and \(s^2\).

  5. For Questions 3 and 4, find \(\hat V(\hat{\boldsymbol\beta})\).

  6. Show that \(\mathbf H\mathbf 1=\mathbf 1\) when the model contains an intercept.

  7. If \(\mathbf A\) is idempotent, show that \(\mathbf I-\mathbf A\) is idempotent.

  8. A \(3\times3\) idempotent matrix has trace 2. What is its rank?

  9. Using Example 4.1, predict \(y\) at \(x_1=5\), \(x_2=8\).

  10. Solve the exercise: express the nitrogen data of Example 3.1 in matrix form and fit the line.

Important formulae

Normal equations and OLS estimator:

\[ \mathbf X'\mathbf X\hat{\boldsymbol\beta}=\mathbf X'\mathbf y,\qquad\hat{\boldsymbol\beta}=(\mathbf X'\mathbf X)^{-1}\mathbf X'\mathbf y \]

Hat matrix and fitted values:

\[ \mathbf H=\mathbf X(\mathbf X'\mathbf X)^{-1}\mathbf X',\qquad\hat{\mathbf y}=\mathbf H\mathbf y \]

Residuals:

\[ \mathbf e=(\mathbf I-\mathbf H)\mathbf y=(\mathbf I-\mathbf H)\boldsymbol\varepsilon \]

Moments of the estimator:

\[ E(\hat{\boldsymbol\beta})=\boldsymbol\beta,\qquad V(\hat{\boldsymbol\beta})=\sigma^2(\mathbf X'\mathbf X)^{-1} \]

Variances of fitted values and residuals:

\[ V(\hat{\mathbf y})=\sigma^2\mathbf H,\qquad V(\mathbf e)=\sigma^2(\mathbf I-\mathbf H) \]

Residual sum of squares and estimate of \(\sigma^2\):

\[ SS_{Res}=\mathbf y'\mathbf y-\hat{\boldsymbol\beta}'\mathbf X'\mathbf y,\qquad s^2=\frac{SS_{Res}}{n-p} \]

Quick revision

  • Minimize \((\mathbf y-\mathbf X\boldsymbol\beta)'(\mathbf y-\mathbf X\boldsymbol\beta)\) → \(\hat{\boldsymbol\beta}=(\mathbf X'\mathbf X)^{-1}\mathbf X'\mathbf y\).
  • \(2\mathbf X'\mathbf X\) positive definite → the solution is a minimum.
  • \(\mathbf H\) → symmetric, idempotent, trace \(p\), projects \(\mathbf y\) onto the column space of \(\mathbf X\).
  • \(\mathbf I-\mathbf H\) → symmetric, idempotent, trace \(n-p\), \((\mathbf I-\mathbf H)\mathbf X=\mathbf 0\).
  • \(\hat{\boldsymbol\beta}-\boldsymbol\beta=(\mathbf X'\mathbf X)^{-1}\mathbf X'\boldsymbol\varepsilon\) → unbiased, covariance \(\sigma^2(\mathbf X'\mathbf X)^{-1}\).
  • \(E(\mathbf e'\mathbf e)=(n-p)\sigma^2\) → \(s^2=SS_{Res}/(n-p)\) is unbiased.
  • Residuals have unequal variances \(\sigma^2(1-h_{ii})\).

Answers to fill in the blanks

1. \(\mathbf X'\mathbf X\hat{\boldsymbol\beta}=\mathbf X'\mathbf y\) 2. \((\mathbf X'\mathbf X)^{-1}\mathbf X'\mathbf y\) 3. Definite 4. \(\mathbf X(\mathbf X'\mathbf X)^{-1}\mathbf X'\) 5. Idempotent 6. \(p\); \(n-p\) 7. \(\sigma^2(\mathbf X'\mathbf X)^{-1}\) 8. \(\sigma^2\mathbf H\); \(\sigma^2(\mathbf I-\mathbf H)\) 9. Chi-square; \(n-p\) 10. 2.829

Solutions to numerical and conceptual questions

  1. \(\mathbf X\): \(25\times4\); \(\mathbf X'\mathbf X\): \(4\times4\); \(\mathbf H\): \(25\times25\); \(\hat{\boldsymbol\beta}\): \(4\times1\).

  2. \(\operatorname{tr}(\mathbf H)=p=4\); average hat value \(=4/25=0.16\).

  3. \(|\mathbf X'\mathbf X|=16\), \((\mathbf X'\mathbf X)^{-1}=\frac1{16}\begin{bmatrix}20&-8\\-8&4\end{bmatrix}\); by Equation 4.4, \(\hat{\boldsymbol\beta}=\frac1{16}\begin{bmatrix}240-224\\-96+112\end{bmatrix}=\begin{bmatrix}1\\1\end{bmatrix}\).

  4. By Equation 4.16, \(SS_{Res}=40-(1\times12+1\times28)=0\), so \(s^2=0\): the fit is exact.

  5. \(\hat V(\hat{\boldsymbol\beta})=s^2(\mathbf X'\mathbf X)^{-1}=\mathbf 0\) since \(s^2=0\).

  6. \(\mathbf 1\) is the first column of \(\mathbf X\), and \(\mathbf H\mathbf X=\mathbf X\), so \(\mathbf H\mathbf 1=\mathbf 1\).

  7. \((\mathbf I-\mathbf A)^2=\mathbf I-2\mathbf A+\mathbf A^2=\mathbf I-2\mathbf A+\mathbf A=\mathbf I-\mathbf A\).

  8. Rank \(=\) trace \(=2\).

  9. \(\hat y=5.375+3.012(5)-1.285(8)=10.151\).

  10. \(\mathbf X'\mathbf X=\begin{bmatrix}10 & 900 \\ 900 & 114000\end{bmatrix}\), \(\mathbf X'\mathbf y=\begin{bmatrix}43.9 \\ 4520.0\end{bmatrix}\), \((\mathbf X'\mathbf X)^{-1}=\begin{bmatrix}0.345455 & -0.002727 \\ -0.002727 & 0.000030\end{bmatrix}\), \(\hat{\boldsymbol\beta}=\begin{bmatrix}2.83818 \\ 0.01724\end{bmatrix}\), \(SS_{Res}=202.57-202.5319=0.0381\), \(s^2=0.00476\) and \(\hat V(\hat{\boldsymbol\beta})=\begin{bmatrix}0.0016435 & -0.0000130 \\ -0.0000130 & 0.0000001\end{bmatrix}\), the same as Example 3.1.