Matrix
Matrix
1
Chapter 5: Matrix approach to simple linear regression
analysis
5.1 Matrices
What is a matrix?
Example:
Example: 23
Example:
Example:
and
Example:
Suppose . Then
> class(A)
[1] "matrix"
> A+B
[,1] [,2] [,3]
[1,] 0 12 2
[2,] 9 10 14
> A-B
[,1] [,2] [,3]
[1,] 2 -8 4
[2,] -1 0 -2
> x<-c(1,2,3)
> x
[1] 1 2 3
> class(x)
[1] "numeric"
> is.vector(x)
[1] TRUE
where c is a scalar
> C
[,1] [,2]
[1,] 5 7
[2,] 17 16
> D
[,1] [,2] [,3]
[1,] 3 6 9
[2,] 9 12 15
[3,] 4 5 6
Notes:
1.%*% is used for multiplying matrices and/or vectors
2.* means to perform elementwise multiplications. Here
is an example where this can be done:
> E<-A
> A*E
[,1] [,2] [,3]
[1,] 1 4 9
[2,] 16 25 36
, a 31 vector
> x<-c(1,2,3)
> x%*%x
[,1]
[1,] 14
> A%*%x
[,1]
[1,] 14
[2,] 32
and
> X
[,1] [,2]
> Y
[1] 3.1 2.3 3.0 1.9 2.5 3.7 3.4 2.6 2.8 1.6 2.0 2.9 2.3
3.2 1.8 1.4 2.0 3.8 2.2
[20] 1.6
> t(X)%*%X
[,1] [,2]
[1,] 20.00 51.3800
[2,] 51.38 148.4634
> t(X)%*%Y
[,1]
[1,] 50.100
[2,] 140.229
> t(Y)%*%Y
[,1]
[1,] 135.15
Notes:
2008 Christopher R. Bilder
5.15
1.The cbind() function combines items by “c”olumns.
Since 1 is only one element, it will replicate itself for
all elements that you are combining so that one full
matrix is formed. There is also a rbind() function that
combines by rows. Thus, rbind(a,b) forms a matrix
with a above b.
2.
3.
since x11=…=xn1=1
4.XX
Example:
Example:
Example:
Note that “I” (the letter I, not the number one) usually
denotes the identity matrix.
A matrix of 1’s:
Notes:
1.
2.
3.
Vector of 0’s:
rank(A)=2
Example:
Check:
> A%*%solve(A)
[,1] [,2]
[1,] 1 1.110223e-16
[2,] 0 1.000000e+00
> solve(A)%*%A
[,1] [,2]
[1,] 1.000000e+00 0
[2,] 1.110223e-16 1
> round(solve(A)%*%A, 2)
[,1] [,2]
[1,] 1 0
[2,] 0 1
Example:
since we assume
i~N(0,2)
Remember that
Notes:
Cov(Z1,Z1)=E[(Z1-1)(Z1-1)]=E[(Z1-1)2]=Var(Z1)
If Z1 and Z2 are independent, then Cov(Z1,Z2)=0.
Cov(Z) =
= 2I
What is Cov(Y)?
Example:
Let . Then .
2.
3.
Y1=0+1X1+1
Y2=0+1X2+2
Yn=0+1Xn+n
Then Y = X + , which is
and
XX .
Residuals:
Let
Sums of Squares
From Chapters 1 and 2: ,
, and
> anova(mod.fit)
Analysis of Variance Table
Response: College.GPA
Df Sum Sq Mean Sq F value Pr(>F)
HS.GPA 1 8.0615 8.0615 91.38 1.779e-08 ***
Residuals 18 1.5880 0.0882
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' '
1
Covariance matrix of b:
Why?
Cov(b) = Cov[(XX)-1XY]
= (XX)-1XCov(Y)[(XX)-1X]
= (XX)-1XCov(Y)X(XX)-1
= (XX)-1X2IX(XX)-1
= 2(XX)-1XX(XX)-1
= 2(XX)-1
Why?