All fourteen are worked below, each set out as 1. Problem, 2. Aim, 3. Formula, 4. Calculation, 5. Result, with the R program (written without packages) and its real output inside the Calculation.
| # | Practical | Worked here | Also in |
|---|---|---|---|
| 1 | Inverse of a matrix by the partition method | Practical 1 | — |
| 2 | Solutions of linear equations by the sweep-out method | Practical 2 | — |
| 3 | Solutions of linear equations by the Doolittle method | Practical 3 | — |
| 4 | Moore–Penrose inverse by the Penrose method | Practical 4 | Unit 1, Example 1.3 |
| 5 | Generalized inverse of a matrix | Practical 5 | — |
| 6 | Characteristic equation from traces of successive powers | Practical 6 | Unit 2, Example 2.1 |
| 7 | Spectral decomposition of a third-order square matrix | Practical 7 | Unit 2, Example 2.4 |
| 8 | Simultaneous reduction of a pair of quadratic forms | Practical 8 | Unit 3, Example 3.3 |
| 9 | Orthonormal basis by the Gram–Schmidt process | Practical 9 | Unit 1, Example 1.1 |
| 10 | Variance–covariance matrix and its characteristics | Practical 10 | — |
| 11 | Simple linear regression, lack of fit, R², adjusted R², pure error, CI | Practical 11 | — |
| 12 | Multiple linear regression, the same quantities | Practical 12 | — |
| 13 | Simple, partial and multiple correlation coefficients | Practical 13 | — |
| 14 | Testing multicollinearity | Practical 14 | Unit 4, Example 4.2 |
All five use
\[ A = \begin{pmatrix} 2 & 1 & 1 \\ 1 & 2 & 1 \\ 1 & 1 & 2 \end{pmatrix}, \qquad \mathbf{b} = \begin{pmatrix} 7 \\ 8 \\ 9 \end{pmatrix}, \]so five different methods can be checked against one another instead of each being taken on trust. The answers they must all reproduce are \(\det A = 4\), \(\mathbf{x} = (1, 2, 3)'\), and
\[ A^{-1} = \frac{1}{4}\begin{pmatrix} 3 & -1 & -1 \\ -1 & 3 & -1 \\ -1 & -1 & 3 \end{pmatrix}. \]Find the inverse of \(A = \begin{pmatrix} 2 & 1 & 1 \\ 1 & 2 & 1 \\ 1 & 1 & 2 \end{pmatrix}\) by the partition method.
To invert a matrix by partitioning it into blocks and inverting the smaller pieces.
\(S\) is the Schur complement of \(A_{11}\).
Applying it:
since \(\tfrac23 + \tfrac1{12} = \tfrac34\) and \(-\tfrac13 + \tfrac1{12} = -\tfrac14\).
A <- matrix(c(2,1,1, 1,2,1, 1,1,2), 3, 3, byrow = TRUE)
A11 <- A[1:2, 1:2]; A12 <- A[1:2, 3, drop = FALSE]; A21 <- A[3, 1:2, drop = FALSE]; A22 <- A[3, 3, drop = FALSE]
A11i <- solve(A11); S <- A22 - A21 %*% A11i %*% A12; Si <- solve(S)
B12 <- -A11i %*% A12 %*% Si; B21 <- -Si %*% A21 %*% A11i
B11 <- A11i + A11i %*% A12 %*% Si %*% A21 %*% A11i
Ainv <- rbind(cbind(B11, B12), cbind(B21, Si)); Ainv * 4
round(max(abs(A %*% Ainv - diag(3))), 10) # 0 -- A times its inverse is I
[,1] [,2] [,3]
[1,] 3 -1 -1
[2,] -1 3 -1
[3,] -1 -1 3
[1] 0
the matrix obtained in Unit 2 by Cayley–Hamilton and again by spectral decomposition (Practical 7): three independent routes, one answer. \(\checkmark\)
Solve \(A\mathbf{x} = \mathbf{b}\) by the sweep-out (Gauss–Jordan) method, where \(A = \begin{pmatrix} 2 & 1 & 1 \\ 1 & 2 & 1 \\ 1 & 1 & 2 \end{pmatrix}\) and \(\mathbf{b} = (7, 8, 9)'\).
To solve a system of linear equations by sweeping the augmented matrix to the identity.
At each step, divide the pivot row by the pivot, then subtract multiples of it from every other row so that its column becomes a unit vector. Augmenting with \(I\) instead of \(\mathbf{b}\) produces \(A^{-1}\) with identical work.
Applying it:
Substituting back: \(2(1)+2+3 = 7\), \(1+2(2)+3 = 8\), \(1+2+2(3) = 9\). \(\checkmark\)
A <- matrix(c(2,1,1, 1,2,1, 1,1,2), 3, 3, byrow = TRUE)
sweep <- function(M) { # Gauss-Jordan on an augmented matrix
for (k in seq_len(nrow(M))) {
M[k, ] <- M[k, ] / M[k, k]
for (i in seq_len(nrow(M))[-k]) M[i, ] <- M[i, ] - M[i, k] * M[k, ]
}
M
}
sweep(cbind(A, c(7, 8, 9)))[, 4] # the solution
sweep(cbind(A, diag(3)))[, 4:6] * 4 # and, augmenting with I, 4 times the inverse
[1] 1 2 3
[,1] [,2] [,3]
[1,] 3 -1 -1
[2,] -1 3 -1
[3,] -1 -1 3
\(\mathbf{x} = (1, 2, 3)'\). The same sweep, applied to \((A \mid I)\), gives \(A^{-1}\) of Practical 1.
Solve \(A\mathbf{x} = \mathbf{b}\) with \(A = \begin{pmatrix} 2 & 1 & 1 \\ 1 & 2 & 1 \\ 1 & 1 & 2 \end{pmatrix}\) and \(\mathbf{b} = (7, 8, 9)'\) by the Doolittle (LU) method, and find \(\det A\).
To factorise a matrix into unit lower and upper triangular factors once, and solve by forward and back substitution.
Applying it:
\(u_{1j} = a_{1j}\): first row of \(U\) is \((2, 1, 1)\); \(\ell_{21} = \ell_{31} = \tfrac12\).
\[ u_{22} = 2 - \tfrac12 = \tfrac32, \quad u_{23} = 1 - \tfrac12 = \tfrac12, \quad \ell_{32} = \frac{1 - \tfrac12}{\tfrac32} = \frac13, \quad u_{33} = 2 - \tfrac12 - \tfrac13\left(\tfrac12\right) = \tfrac43. \] \[ L = \begin{pmatrix} 1 & 0 & 0 \\ \tfrac12 & 1 & 0 \\ \tfrac12 & \tfrac13 & 1 \end{pmatrix}, \qquad U = \begin{pmatrix} 2 & 1 & 1 \\ 0 & \tfrac32 & \tfrac12 \\ 0 & 0 & \tfrac43 \end{pmatrix}. \]Check: row 3 of \(L\) against column 3 of \(U\): \(\tfrac12 + \tfrac16 + \tfrac43 = 2 = a_{33}\). \(\checkmark\)
\[ z_1 = 7, \quad z_2 = 8 - \tfrac72 = \tfrac92, \quad z_3 = 9 - \tfrac72 - \tfrac32 = 4; \qquad x_3 = \frac{4}{\tfrac43} = 3, \quad x_2 = \frac{\tfrac92 - \tfrac32}{\tfrac32} = 2, \quad x_1 = \frac{7 - 2 - 3}{2} = 1. \]A <- matrix(c(2,1,1, 1,2,1, 1,1,2), 3, 3, byrow = TRUE)
doolittle <- function(A) { # Doolittle LU, written out rather than calling a library
n <- nrow(A); L <- diag(n); U <- matrix(0, n, n)
for (i in 1:n) {
for (j in i:n) U[i, j] <- A[i, j] - sum(L[i, ] * U[, j])
if (i < n) for (j in (i+1):n)
L[j, i] <- (A[j, i] - sum(L[j, ] * U[, i])) / U[i, i]
}
list(L = L, U = U)
}
lu <- doolittle(A); lu$L; lu$U
max(abs(lu$L %*% lu$U - A)) # 0 -- the factorisation is exact
z <- forwardsolve(lu$L, c(7, 8, 9)); backsolve(lu$U, z)
prod(diag(lu$U)) # det(A)
[,1] [,2] [,3]
[1,] 1.0 0.0000000 0
[2,] 0.5 1.0000000 0
[3,] 0.5 0.3333333 1
[,1] [,2] [,3]
[1,] 2 1.0 1.000000
[2,] 0 1.5 0.500000
[3,] 0 0.0 1.333333
[1] 0
[1] 1 2 3
[1] 4
\(\mathbf{x} = (1, 2, 3)'\) and \(\det A = 1 \times 2 \times \tfrac32 \times \tfrac43 = 4\). Factorise once and any number of right-hand sides cost only the two substitutions — which is why this, and not Gauss–Jordan, is what numerical libraries do.
Find the Moore–Penrose inverse of \(A = \begin{pmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{pmatrix}\) and verify the four Penrose conditions.
To find the Moore–Penrose inverse of a matrix of full column rank and verify that it satisfies all four Penrose conditions.
Applying it:
The two columns are not multiples of one another, so \(\operatorname{rank}(A) = 2\), full column rank.
\[ A'A = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}, \quad (A'A)^{-1} = \frac13\begin{pmatrix} 2 & -1 \\ -1 & 2 \end{pmatrix}, \quad A^{+} = \frac{1}{3}\begin{pmatrix} 2 & -1 & 1 \\ -1 & 2 & 1 \end{pmatrix}. \] \[ A^{+}A = I_2, \qquad AA^{+} = \frac{1}{3}\begin{pmatrix} 2 & -1 & 1 \\ -1 & 2 & 1 \\ 1 & 1 & 2 \end{pmatrix}, \quad \operatorname{trace}(AA^{+}) = 2. \]| condition | check | holds? |
|---|---|---|
| (i) \(AA^{+}A = A\) | \(A(A^{+}A) = A I_2 = A\) | yes |
| (ii) \(A^{+}AA^{+} = A^{+}\) | \(I_2 A^{+} = A^{+}\) | yes |
| (iii) \((AA^{+})' = AA^{+}\) | \(AA^{+}\) is symmetric | yes |
| (iv) \((A^{+}A)' = A^{+}A\) | \(I_2\) is symmetric | yes |
A <- matrix(c(1,0, 0,1, 1,1), 3, 2, byrow = TRUE)
Ap <- solve(t(A) %*% A) %*% t(A); Ap * 3
c(i = max(abs(A %*% Ap %*% A - A)), ii = max(abs(Ap %*% A %*% Ap - Ap)),
iii = max(abs(t(A %*% Ap) - A %*% Ap)), iv = max(abs(t(Ap %*% A) - Ap %*% A))) # all 0
sum(diag(A %*% Ap)) # the trace of the projection = rank
[,1] [,2] [,3]
[1,] 2 -1 1
[2,] -1 2 1
i ii iii iv
0 0 0 0
[1] 2
\(A^{+} = \frac13\begin{pmatrix} 2 & -1 & 1 \\ -1 & 2 & 1 \end{pmatrix}\), satisfying all four conditions. \(AA^{+}\) is not \(I_3\) and could not be (its rank is 2): it is the projection onto the column space of \(A\), with trace 2 = rank. Applied to \(\mathbf{y}\), \(A^{+}\) returns the least-squares coefficients (worked also in Unit 1, Example 1.3).
Find a generalized inverse of the singular matrix \(S = \begin{pmatrix} 1 & 1 & 0 \\ 1 & 1 & 0 \\ 0 & 0 & 2 \end{pmatrix}\), and check whether it is the Moore–Penrose inverse.
To construct a generalized inverse of a singular matrix from a non-singular submatrix, and to see that it need not be the Moore–Penrose inverse.
Here \(\det S = 0\) and \(\operatorname{rank}(S) = 2\).
Applying it:
Rows 1 and 3, columns 1 and 3: \(\begin{pmatrix} 1 & 0 \\ 0 & 2 \end{pmatrix}\), \(\det = 2\), inverse \(\begin{pmatrix} 1 & 0 \\ 0 & \tfrac12 \end{pmatrix}\). Placing it back:
\[ G = \begin{pmatrix} 1 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & \tfrac12 \end{pmatrix}, \qquad SG = \begin{pmatrix} 1 & 0 & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1 \end{pmatrix}, \qquad (SG)S = S. \checkmark \]\(GSG = G\) also holds, so \(G\) is reflexive. But \((SG)' = \begin{pmatrix} 1 & 1 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 1 \end{pmatrix} \ne SG\): condition (iii) fails.
ginv_sub <- function(S, rows, cols) { # a g-inverse from a non-singular submatrix
G <- matrix(0, ncol(S), nrow(S))
G[cols, rows] <- solve(S[rows, cols])
G
}
S <- matrix(c(1,1,0, 1,1,0, 0,0,2), 3, 3, byrow = TRUE)
G <- ginv_sub(S, c(1, 3), c(1, 3)); G
max(abs(S %*% G %*% S - S)) # 0 -- condition (i) holds
max(abs(G %*% S %*% G - G)) # 0 -- and (ii): G is reflexive
isTRUE(all.equal(t(S %*% G), S %*% G)) # FALSE -- so NOT Moore-Penrose
[,1] [,2] [,3]
[1,] 1 0 0.0
[2,] 0 0 0.0
[3,] 0 0 0.5
[1] 0
[1] 0
[1] FALSE
\(G\) is a (reflexive) generalized inverse of \(S\) but not its Moore–Penrose inverse. There are many g-inverses — a different submatrix gives a different \(G\), and every one satisfies \(SGS = S\); the Moore–Penrose inverse is the single one that is also symmetric in both products. In Unit 4 that non-uniqueness is why \(\hat{\boldsymbol\beta}\) is not unique in a rank-deficient model while \(\boldsymbol\ell'\hat{\boldsymbol\beta}\) for estimable \(\boldsymbol\ell\) is.
Find the characteristic equation of \(A = \begin{pmatrix} 2 & 1 & 1 \\ 1 & 2 & 1 \\ 1 & 1 & 2 \end{pmatrix}\) from the traces of its powers, its roots, and its eigenvectors.
To obtain the characteristic equation of a matrix from the traces of its successive powers, and its roots and vectors.
Checks: \(c_3 = \det A\); the roots sum to the trace and multiply to the determinant.
Applying it:
\(c_3 = 4 = \det A\); \(4 + 1 + 1 = 6\) and \(4 \times 1 \times 1 = 4\). \(\checkmark\) For \(\lambda = 4\): \(A - 4I\) has rank 2 and \(\mathbf{x} = (1, 1, 1)'\). For \(\lambda = 1\): \(A - I\) has rank 1, the single equation \(x_1 + x_2 + x_3 = 0\), and a basis \((-1, 1, 0)'\), \((-1, 0, 1)'\), both orthogonal to \((1, 1, 1)'\).
A <- matrix(c(2,1,1, 1,2,1, 1,1,2), 3, 3, byrow = TRUE)
tr <- function(M) sum(diag(M)) # characteristic equation from traces of powers
A2 <- A %*% A; A3 <- A2 %*% A
t1 <- tr(A); t2 <- tr(A2); t3 <- tr(A3)
c1 <- t1; c2 <- (c1*t1 - t2)/2; c3 <- (c2*t1 - c1*t2 + t3)/3
c(t1, t2, t3); c(c1, c2, c3)
Re(polyroot(c(-c3, c2, -c1, 1))) # the roots
[1] 6 18 66
[1] 6 9 4
[1] 1 1 4
The characteristic equation is \(\lambda^{3} - 6\lambda^{2} + 9\lambda - 4 = 0\), with roots 4 (vector \((1,1,1)'\)) and 1 twice (any vector with \(x_1 + x_2 + x_3 = 0\)) (worked also in Unit 2, Example 2.1).
Find the spectral decomposition of \(A = \begin{pmatrix} 2 & 1 & 1 \\ 1 & 2 & 1 \\ 1 & 1 & 2 \end{pmatrix}\) (roots 4 and 1, twice, from Practical 6), and from it \(A^{-1}\) and \(A^{1/2}\).
To write a symmetric matrix as a sum of its eigenvalues times the projections onto its eigenspaces, and use it for functions of the matrix.
Applying it:
\((I + J/3)^{2} = I + 2J/3 + J/3 = I + J = A\). \(\checkmark\)
A <- matrix(c(2,1,1, 1,2,1, 1,1,2), 3, 3, byrow = TRUE)
J <- matrix(1, 3, 3); P1 <- J / 3; P2 <- diag(3) - J / 3
round(max(abs(4 * P1 + P2 - A)), 10) # 0 -- the decomposition reproduces A
round(c(max(abs(P1 %*% P1 - P1)), max(abs(P1 %*% P2))), 10) # idempotent, orthogonal
round(max(abs((P1 / 4 + P2) - solve(A))), 10) # 0 -- the inverse
R <- 2 * P1 + P2; R; round(max(abs(R %*% R - A)), 10) # the square root, and its square
[1] 0
[1] 0 0
[1] 0
[,1] [,2] [,3]
[1,] 1.3333333 0.3333333 0.3333333
[2,] 0.3333333 1.3333333 0.3333333
[3,] 0.3333333 0.3333333 1.3333333
[1] 0
\(A = 4\cdot\frac{J}{3} + 1\cdot\left(I - \frac{J}{3}\right)\); hence \(A^{-1} = I - J/4\) (diagonal \(\tfrac34\), off-diagonal \(-\tfrac14\), as in Practical 1) and \(A^{1/2} = I + J/3\). Every function of \(A\) comes from one decomposition (worked also in Unit 2, Example 2.4).
Reduce the quadratic forms \(\mathbf{x}'A\mathbf{x}\) and \(\mathbf{x}'B\mathbf{x}\) simultaneously, where \(A = \begin{pmatrix} 5 & 2 \\ 2 & 2 \end{pmatrix}\) and \(B = \begin{pmatrix} 2 & 1 \\ 1 & 1 \end{pmatrix}\).
To reduce two quadratic forms, one positive definite, to sums of squares by one transformation.
\(B\) must be positive definite.
Applying it:
\(B\): \(2 > 0\), \(\det B = 1 > 0\); \(A\): \(5 > 0\), \(\det A = 6 > 0\). \(\checkmark\)
\[ |A - \lambda B| = (5 - 2\lambda)(2 - \lambda) - (2 - \lambda)^{2} = (2 - \lambda)\left[(5 - 2\lambda) - (2 - \lambda)\right] = (2 - \lambda)(3 - \lambda). \] \[ A - 2B = \begin{pmatrix} 1 & 0 \\ 0 & 0 \end{pmatrix}, \ \det = 0; \qquad A - 3B = \begin{pmatrix} -1 & -1 \\ -1 & -1 \end{pmatrix}, \ \det = 0. \checkmark \]A <- matrix(c(5, 2, 2, 2), 2); B <- matrix(c(2, 1, 1, 1), 2)
c(det(B), det(A)) # both positive definite
eigen(solve(B) %*% A)$values # the roots of |A - lambda B| = 0
c(det(A - 2 * B), det(A - 3 * B)) # both 0
[1] 1 6
[1] 3 2
[1] 0 0
The roots are \(\lambda = 2, 3\), and the pair reduces to \(\mathbf{x}'B\mathbf{x} = y_1^{2} + y_2^{2}\), \(\mathbf{x}'A\mathbf{x} = 2y_1^{2} + 3y_2^{2}\). The ratio \(\mathbf{x}'A\mathbf{x}/\mathbf{x}'B\mathbf{x}\) therefore ranges over \([2, 3]\) and no further — in a discriminant problem, the smallest and largest achievable separation (worked also in Unit 3, Example 3.3).
Construct an orthonormal basis of \(\mathbb{R}^{3}\) from \(\mathbf{v}_1 = (1,1,0)'\), \(\mathbf{v}_2 = (1,0,1)'\), \(\mathbf{v}_3 = (0,1,1)'\).
To turn a set of independent vectors into an orthonormal basis of the same space.
Applying it:
gram_schmidt <- function(V) { # columns of V in, orthonormal columns out
E <- V
for (k in seq_len(ncol(V))) {
u <- V[, k]
for (j in seq_len(k - 1)) u <- u - sum(V[, k] * E[, j]) * E[, j]
E[, k] <- u / sqrt(sum(u^2))
}
E
}
V <- cbind(c(1, 1, 0), c(1, 0, 1), c(0, 1, 1))
E <- gram_schmidt(V); round(E, 6)
round(t(E) %*% E, 10) # the identity: orthonormal
[,1] [,2] [,3]
[1,] 0.707107 0.408248 -0.57735
[2,] 0.707107 -0.408248 0.57735
[3,] 0.000000 0.816497 0.57735
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
\(\{\mathbf{e}_1, \mathbf{e}_2, \mathbf{e}_3\}\) above is an orthonormal basis: \(E'E = I\). The starting vectors were independent but at \(60^{\circ}\) to one another; the output is a genuine set of coordinate axes for the same space — the construction that, as the Helmert matrix, makes the sample mean and variance independent (worked also in Unit 1, Example 1.1).
Five units are measured on three variables:
| unit | 1 | 2 | 3 | 4 | 5 | mean |
|---|---|---|---|---|---|---|
| \(x_1\) | 2 | 4 | 6 | 8 | 10 | 6.0 |
| \(x_2\) | 1 | 3 | 2 | 5 | 4 | 3.0 |
| \(x_3\) | 5 | 4 | 6 | 5 | 8 | 5.6 |
Find the variance–covariance matrix, its trace and determinant, and the correlation matrix, and interpret them.
To compute the variance–covariance matrix of a data set and read the data's spread from its trace, determinant and correlations.
Applying it:
X <- cbind(c(2,4,6,8,10), c(1,3,2,5,4), c(5,4,6,5,8))
S <- cov(X); S
c(trace = sum(diag(S)), det = det(S), prod_diag = prod(diag(S)))
round(cov2cor(S), 6)
[,1] [,2] [,3]
[1,] 10.0 4.0 3.5
[2,] 4.0 2.5 0.5
[3,] 3.5 0.5 2.3
trace det prod_diag
14.800 1.575 57.500
[,1] [,2] [,3]
[1,] 1.0000 0.800000 0.729800
[2,] 0.8000 1.000000 0.208514
[3,] 0.7298 0.208514 1.000000
Total variance 14.8; generalized variance 1.575, against 57.5 for uncorrelated variables with these variances (Hadamard's inequality, \(1.575 \le 57.5\)): the correlations have collapsed the cloud to under 3% of that volume. \(x_1\) is strongly related to both others, but \(x_2\) and \(x_3\) are almost unrelated to each other (\(r = 0.2085\)): their apparent connection runs through \(x_1\), which is what the partial correlations of Practical 13 are designed to expose.
Eight observations at four distinct \(x\) values, two replicates each:
| \(x\) | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 |
|---|---|---|---|---|---|---|---|---|
| \(y\) | 3 | 4 | 6 | 7 | 8 | 10 | 11 | 13 |
Fit the simple linear regression; find \(R^{2}\), adjusted \(R^{2}\), a 95% confidence interval for the slope, and test for lack of fit using the pure error.
To fit a simple linear regression, measure its fit, and test whether a straight line is adequate by separating pure error from lack of fit.
Applying it:
| \(x\) | values | mean | contribution |
|---|---|---|---|
| 1 | 3, 4 | 3.5 | 0.5 |
| 2 | 6, 7 | 6.5 | 0.5 |
| 3 | 8, 10 | 9.0 | 2.0 |
| 4 | 11, 13 | 12.0 | 2.0 |
| SS pure error (4 d.f.) | 5.0 | ||
x <- c(1,1,2,2,3,3,4,4); y <- c(3,4,6,7,8,10,11,13)
fit <- lm(y ~ x); coef(fit)
c(R2 = summary(fit)$r.squared, adjR2 = summary(fit)$adj.r.squared)
confint(fit, "x")
anova(fit, lm(y ~ factor(x))) # lack of fit: the line against the group means
(Intercept) x
0.75 2.80
R2 adjR2
0.9389222 0.9287425
2.5 % 97.5 %
x 2.086609 3.513391
Analysis of Variance Table
Model 1: y ~ x
Model 2: y ~ factor(x)
Res.Df RSS Df Sum of Sq F Pr(>F)
1 6 5.1
2 4 5.0 2 0.1 0.04 0.9612
\(\hat y = 0.75 + 2.8x\), \(R^{2} = 0.939\), \(\bar R^{2} = 0.929\), and with 95% confidence the slope lies between 2.09 and 3.51. The lack-of-fit \(F = 0.04\) (\(p = 0.96\)) gives no evidence against a straight line. Without replication \(SSE\) could not have been split, and the residual would confound curvature with noise.
Regress \(y = 12, 18, 20, 28, 32\) on \(x_1\) and \(x_2\) of Practical 10 (\(x_1 = 2, 4, 6, 8, 10\); \(x_2 = 1, 3, 2, 5, 4\)). Find the estimates, \(R^{2}\), adjusted \(R^{2}\), the overall \(F\) test and the standard errors.
To fit a multiple linear regression by solving the normal equations in matrix form, and to test the model and each coefficient.
Applying it:
| coefficient | estimate | \(c_{jj}\) | \(SE\) | \(t\) | \(P(|t_2| > |t|)\) |
|---|---|---|---|---|---|
| \(b_1\) | 2.055556 | 5/72 | 0.232406 | 8.844692 | 0.012543 |
| \(b_2\) | 1.111111 | 5/18 | 0.464811 | 2.390457 | 0.139337 |
x1 <- c(2,4,6,8,10); x2 <- c(1,3,2,5,4); y <- c(12,18,20,28,32)
X <- cbind(1, x1, x2); XtX <- t(X) %*% X; XtX; det(XtX)
b <- solve(XtX, t(X) %*% y); round(drop(b), 6)
fit <- lm(y ~ x1 + x2); s <- summary(fit)
round(c(R2 = s$r.squared, adjR2 = s$adj.r.squared, F = unname(s$fstatistic[1])), 6)
round(coef(s)[-1, ], 6)
x1 x2
5 30 15
x1 30 220 106
x2 15 106 55
[1] 720
x1 x2
6.333333 2.055556 1.111111
R2 adjR2 F
0.993924 0.987847 163.571429
Estimate Std. Error t value Pr(>|t|)
x1 2.055556 0.232406 8.844692 0.012543
x2 1.111111 0.464811 2.390457 0.139337
\(\hat y = 6.333 + 2.056x_1 + 1.111x_2\), \(R^{2} = 0.994\), \(\bar R^{2} = 0.988\). The model as a whole is highly significant (\(p = 0.006\)) and \(x_1\) is individually significant (\(p = 0.013\)), but \(x_2\) is not (\(p = 0.139\)) — with only two error degrees of freedom there is very little power, the classic sign of a model over-fitted to its sample size, and why \(\bar R^{2}\) is reported beside \(R^{2}\).
For the data of Practical 12, find the simple correlations of \(y\), \(x_1\), \(x_2\), the partial correlations \(r_{01.2}\) and \(r_{02.1}\), and the multiple correlation \(R_{0.12}\).
To measure the relation between two variables with a third held fixed, and of one variable with two others jointly.
Subscript 0 is \(y\). Check: \(R_{0.12}^{2}\) equals the \(R^{2}\) of the regression of Practical 12.
Applying it:
Numerator: \(0.988212 - 0.869626 \times 0.8 = 0.292511\). Denominator: \(\sqrt{(1 - 0.756249)(1 - 0.64)} = \sqrt{0.087750} = 0.296226\).
\[ r_{01.2} = \frac{0.292511}{0.296226} = 0.987457, \qquad r_{02.1} = 0.860663, \qquad R_{0.12} = 0.996957. \]Check: \(\sqrt{R^{2}} = \sqrt{0.993924} = 0.996957\) (Practical 12). \(\checkmark\)
x1 <- c(2,4,6,8,10); x2 <- c(1,3,2,5,4); y <- c(12,18,20,28,32)
r01 <- cor(y, x1); r02 <- cor(y, x2); r12 <- cor(x1, x2)
round(c(r01 = r01, r02 = r02, r12 = r12), 6)
p012 <- (r01 - r02 * r12) / sqrt((1 - r02^2) * (1 - r12^2))
p021 <- (r02 - r01 * r12) / sqrt((1 - r01^2) * (1 - r12^2))
R012 <- sqrt((r01^2 + r02^2 - 2 * r01 * r02 * r12) / (1 - r12^2))
round(c(r01.2 = p012, r02.1 = p021, R0.12 = R012, sqrtR2 = sqrt(summary(lm(y ~ x1 + x2))$r.squared)), 6)
r01 r02 r12
0.988212 0.869626 0.800000
r01.2 r02.1 R0.12 sqrtR2
0.987457 0.860663 0.996957 0.996957
Holding \(x_2\) fixed barely changes the relation between \(y\) and \(x_1\) (0.9882 → 0.9875), so \(x_1\) carries information of its own; holding \(x_1\) fixed also leaves \(r(y, x_2)\) high (0.8696 → 0.8607). The multiple correlation is 0.997. Contrast Practical 10, where \(x_2\) and \(x_3\) appeared related only through \(x_1\) — the case where a partial correlation collapses towards zero, and what the technique is for.
Three regressors are observed on five units:
| unit | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| \(x_1\) | 1 | 2 | 3 | 4 | 5 |
| \(x_2\) | 2 | 4 | 6 | 8 | 11 |
| \(x_3\) | 1 | 3 | 2 | 5 | 4 |
Test the design for multicollinearity.
To detect multicollinearity among regressors by the correlation matrix, the variance inflation factors and the condition number.
\(R_j^{2}\) comes from regressing \(x_j\) on the other regressors. The usual warning lines are \(VIF > 10\) and \(\kappa\) above about 30.
Applying it:
| regressor | \(R_j^{2}\) | \(VIF_j\) | reading |
|---|---|---|---|
| \(x_1\) | 0.99457286 | 184.2593 | severe |
| \(x_2\) | 0.99385246 | 162.6667 | severe |
| \(x_3\) | 0.73000000 | 3.7037 | acceptable |
The eigenvalues of the correlation matrix are 2.714426, 0.282689 and 0.002884, so \(\kappa = \sqrt{2.714426/0.002884} = 30.68\).
Z <- cbind(x1 = c(1,2,3,4,5), x2 = c(2,4,6,8,11), x3 = c(1,3,2,5,4))
round(cor(Z), 6)
vif_manual <- function(X) { # VIF without the car package
sapply(seq_len(ncol(X)), function(j)
1 / (1 - summary(lm(X[, j] ~ X[, -j]))$r.squared))
}
round(vif_manual(Z), 4)
ev <- eigen(cor(Z))$values; round(ev, 6)
sqrt(max(ev) / min(ev)) # the condition number
x1 x2 x3
x1 1.000000 0.995893 0.800000
x2 0.995893 1.000000 0.769554
x3 0.800000 0.769554 1.000000
[1] 184.2593 162.6667 3.7037
[1] 2.714426 0.282689 0.002884
[1] 30.67827
\(x_1\) and \(x_2\) are severely collinear (\(VIF\) 184 and 163, far above 10); \(x_3\) is not part of the problem (\(VIF = 3.7\)). The condition number of the correlation matrix, 30.7, is just above the warning line of 30: one eigenvalue, 0.0029, is nearly zero — the near-exact relation \(x_2 \approx 2x_1\). The standard error of \(\hat\beta_1\) is \(\sqrt{184.26} = 13.6\) times what orthogonal regressors would give. The estimates stay unbiased and the fit stays good inside the data's range: multicollinearity damages the separation of effects, not the fit. The remedy is in the design — drop or combine one of the pair, or collect data that breaks the pattern — not a larger sample of the same shape (worked also in Unit 4, Example 4.2).