1. Chapter 5 Problems 2

Set matrix A, B, and C

A <- matrix(c(2,1,3,5,5,7,4,8), ncol=2, byrow = TRUE)
A
##      [,1] [,2]
## [1,]    2    1
## [2,]    3    5
## [3,]    5    7
## [4,]    4    8
B <- matrix(c(6,9,3,1), ncol=1, byrow = TRUE)
B
##      [,1]
## [1,]    6
## [2,]    9
## [3,]    3
## [4,]    1
C <- matrix(c(3,8,8,6,5,1,2,4), ncol=2, byrow = TRUE)
C
##      [,1] [,2]
## [1,]    3    8
## [2,]    8    6
## [3,]    5    1
## [4,]    2    4

(1) \(A + C\)

A + C
##      [,1] [,2]
## [1,]    5    9
## [2,]   11   11
## [3,]   10    8
## [4,]    6   12

(2) \(A - C\)

A - C
##      [,1] [,2]
## [1,]   -1   -7
## [2,]   -5   -1
## [3,]    0    6
## [4,]    2    4

(3) \(B'A\)

t(B) %*% A
##      [,1] [,2]
## [1,]   58   80

(4) \(AC'\)

A %*% t(C)
##      [,1] [,2] [,3] [,4]
## [1,]   14   22   11    8
## [2,]   49   54   20   26
## [3,]   71   82   32   38
## [4,]   76   80   28   40

(5) \(C'A\)

t(C) %*% A
##      [,1] [,2]
## [1,]   63   94
## [2,]   55   77

2. Chapter 5 Problems 5

set data

X = matrix(c(4,1,2,3,3,4), ncol=1,byrow = TRUE)
aX = cbind(1, X)
X
##      [,1]
## [1,]    4
## [2,]    1
## [3,]    2
## [4,]    3
## [5,]    3
## [6,]    4
aX
##      [,1] [,2]
## [1,]    1    4
## [2,]    1    1
## [3,]    1    2
## [4,]    1    3
## [5,]    1    3
## [6,]    1    4
Y = matrix(c(16,5,10,15,13,22), ncol=1, byrow = TRUE)
Y
##      [,1]
## [1,]   16
## [2,]    5
## [3,]   10
## [4,]   15
## [5,]   13
## [6,]   22

(1) \(Y'Y\)

t(Y) %*% Y
##      [,1]
## [1,] 1259

(2) \(X'X\)

t(aX) %*% aX
##      [,1] [,2]
## [1,]    6   17
## [2,]   17   55

(3) \(X'Y\)

t(aX) %*% Y
##      [,1]
## [1,]   81
## [2,]  261

3. Chapter 5 Problems 10

set data

A = matrix(c(2,4,3,1), ncol=2, byrow = TRUE)
A
##      [,1] [,2]
## [1,]    2    4
## [2,]    3    1
B = matrix(c(4,3,2,6,5,10,10,1,6), ncol=3, byrow = TRUE)
B
##      [,1] [,2] [,3]
## [1,]    4    3    2
## [2,]    6    5   10
## [3,]   10    1    6

(1) Inverse of matrices

  • Inverse of matrix A: \(A^{-1}\)
A_inv = solve(A)
A_inv
##      [,1] [,2]
## [1,] -0.1  0.4
## [2,]  0.3 -0.2
  • Inverse of matrix B: \(B^{-1}\)
B_inv = solve(B)
B_inv
##            [,1]        [,2]        [,3]
## [1,]  0.1086957 -0.08695652  0.10869565
## [2,]  0.3478261  0.02173913 -0.15217391
## [3,] -0.2391304  0.14130435  0.01086957

(2) Checking

  • checking for A
A %*% A_inv
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1
  • checking for B
B %*% B_inv
##      [,1] [,2]          [,3]
## [1,]    1    0  5.204170e-17
## [2,]    0    1 -1.387779e-17
## [3,]    0    0  1.000000e+00
round(B %*% B_inv,digits=15)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1

4. Chapter 5 Problems 11

set data

A = matrix(c(5,4,1,1,0,9,3,5,6), ncol=3)
A
##      [,1] [,2] [,3]
## [1,]    5    1    3
## [2,]    4    0    5
## [3,]    1    9    6

(1) Inverse of A

A_inv = solve(A)
A_inv
##            [,1]       [,2]        [,3]
## [1,]  0.3308824 -0.1544118 -0.03676471
## [2,]  0.1397059 -0.1985294  0.09558824
## [3,] -0.2647059  0.3235294  0.02941176

(2) checking

A %*% A_inv
##              [,1] [,2]          [,3]
## [1,] 1.000000e+00    0 -1.387779e-17
## [2,] 2.220446e-16    1  0.000000e+00
## [3,] 0.000000e+00    0  1.000000e+00
round(A %*% A_inv,digits=15)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1

4. Chapter 5 Problems 24

set data

X = matrix(c(4,1,2,3,3,4), ncol=1,byrow = TRUE)
aX = cbind(1, X)
Y = matrix(c(16,5,10,15,13,22), ncol=1, byrow = TRUE)

a (1)

betahat = solve(t(aX)%*%aX)%*%t(aX)%*%Y
betahat
##           [,1]
## [1,] 0.4390244
## [2,] 4.6097561

a (2)

Yhat = aX%*%betahat
residuals = Y - Yhat
residuals
##             [,1]
## [1,] -2.87804878
## [2,] -0.04878049
## [3,]  0.34146341
## [4,]  0.73170732
## [5,] -1.26829268
## [6,]  3.12195122

a (3) \(SSR = \sum (\hat{Y} - \bar{Y})^2\)

SSR = sum((Yhat - mean(Y))^2)
SSR
## [1] 145.2073

a (4) \(SSE = \sum (Y - \hat{Y})^2\)

SSE = sum((Y-Yhat)^2)
SSE
## [1] 20.29268

a (5) estimated variance-coveraiance matrix of b

sigma_squared = SSE/(length(X)- 1 - 1)
sigma_squared
## [1] 5.073171
sigma_squared * solve(t(aX)%*%aX)
##           [,1]       [,2]
## [1,]  6.805473 -2.1035098
## [2,] -2.103510  0.7424152

a (6)

betahat
##           [,1]
## [1,] 0.4390244
## [2,] 4.6097561
Y = 0.439 + 4.610 * X
X4 = 4
Yhat4 = 0.439 + 4.610 * X4
Yhat4
## [1] 18.879

a (7) \(s^2\) {pred} when Xh = 4

MSE = SSE/(length(X)-2)
Xh = c(1, 4)
MSE * (1 + t(Xh)%*%solve(t(aX)%*%aX)%*%Xh)
##          [,1]
## [1,] 6.929209

b calculation

MSE = SSE/(length(X) - 2)
s_squared_b = MSE * solve(t(aX)%*%aX)
s_squared_b
##           [,1]       [,2]
## [1,]  6.805473 -2.1035098
## [2,] -2.103510  0.7424152

b (1) \(s\{b_0,b_1\}\)

s_squared_b[1,2]
## [1] -2.10351

b.(2) \(s^2\{b_0\}\)

s_squared_b[1,1]
## [1] 6.805473

b.(3) \(s\{b_1\}\)

(s_squared_b[2,2])^0.5
## [1] 0.8616352

c. matrix H

H = aX%*%solve(t(aX)%*%aX)%*%t(aX)
H
##             [,1]       [,2]       [,3]      [,4]      [,5]        [,6]
## [1,]  0.36585366 -0.1463415 0.02439024 0.1951220 0.1951220  0.36585366
## [2,] -0.14634146  0.6585366 0.39024390 0.1219512 0.1219512 -0.14634146
## [3,]  0.02439024  0.3902439 0.26829268 0.1463415 0.1463415  0.02439024
## [4,]  0.19512195  0.1219512 0.14634146 0.1707317 0.1707317  0.19512195
## [5,]  0.19512195  0.1219512 0.14634146 0.1707317 0.1707317  0.19512195
## [6,]  0.36585366 -0.1463415 0.02439024 0.1951220 0.1951220  0.36585366

d. \(s^2\{e\}\)

I = diag(1, length(X))
MSE = SSE/(length(X)- 1 - 1)
MSE * (I-H)
##            [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
## [1,]  3.2171327  0.7424152 -0.1237359 -0.9898870 -0.9898870 -1.8560381
## [2,]  0.7424152  1.7323022 -1.9797739 -0.6186794 -0.6186794  0.7424152
## [3,] -0.1237359 -1.9797739  3.7120761 -0.7424152 -0.7424152 -0.1237359
## [4,] -0.9898870 -0.6186794 -0.7424152  4.2070196 -0.8661511 -0.9898870
## [5,] -0.9898870 -0.6186794 -0.7424152 -0.8661511  4.2070196 -0.9898870
## [6,] -1.8560381  0.7424152 -0.1237359 -0.9898870 -0.9898870  3.2171327
Copyright © 2017 Ming Chen & Wenqiang Feng. All rights reserved.