R Lab 2 (II): 2023-04-17

Review of matrix algebra

Vectors

x <- c(3, 4, 10)
x
[1]  3  4 10
length(x)
[1] 3

Converting a vector into a matrix object

xmat <- as.matrix(x)
xmat
     [,1]
[1,]    3
[2,]    4
[3,]   10
dim(xmat)
[1] 3 1

Transpose of a vector

t(x)
     [,1] [,2] [,3]
[1,]    3    4   10
length(t(x))
[1] 3

Transpose of a matrix

t(xmat)
     [,1] [,2] [,3]
[1,]    3    4   10
dim(t(xmat))
[1] 1 3

Vector multiplication

x <- c(4, 1, 3, 2)
y <- c(1, -1, 3, 0)

The inner product

t(x)%*%y
     [,1]
[1,]   12
x*y
[1]  4 -1  9  0

A normalized vector: a vector of unit length

z <- x / sqrt(c(t(x)%*%x))
z
[1] 0.7302967 0.1825742 0.5477226 0.3651484
t(z)%*%z # Check the length of the normalized vector
     [,1]
[1,]    1

Cosine of the angle between two vectors (in radians/rad)

cor(x,y)
[1] 0.6803361

Matrices

data <- c(1:6)
data
[1] 1 2 3 4 5 6
A <- matrix(data, nrow=3, ncol=2)
A
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6
B <- matrix(data, nrow = 3, ncol=2, byrow = TRUE)
B
     [,1] [,2]
[1,]    1    2
[2,]    3    4
[3,]    5    6

Some special matrices

Symmetric matrices: correlation matrix/var-cov matrix

data("USArrests")
View(USArrests)

colMeans(USArrests)
  Murder  Assault UrbanPop     Rape 
   7.788  170.760   65.540   21.232 
cov(USArrests)
             Murder   Assault   UrbanPop      Rape
Murder    18.970465  291.0624   4.386204  22.99141
Assault  291.062367 6945.1657 312.275102 519.26906
UrbanPop   4.386204  312.2751 209.518776  55.76808
Rape      22.991412  519.2691  55.768082  87.72916
cor(USArrests)
             Murder   Assault   UrbanPop      Rape
Murder   1.00000000 0.8018733 0.06957262 0.5635788
Assault  0.80187331 1.0000000 0.25887170 0.6652412
UrbanPop 0.06957262 0.2588717 1.00000000 0.4113412
Rape     0.56357883 0.6652412 0.41134124 1.0000000

Diagonal matrices

ddata <- diag(data)
ddata
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    0    0    0    0    0
[2,]    0    2    0    0    0    0
[3,]    0    0    3    0    0    0
[4,]    0    0    0    4    0    0
[5,]    0    0    0    0    5    0
[6,]    0    0    0    0    0    6
diag(ddata)
[1] 1 2 3 4 5 6

Identity matrix

diag(rep(1,4))
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    1    0    0
[3,]    0    0    1    0
[4,]    0    0    0    1

Matrix addition and subtraction

A <- matrix(c(1:6), 2, 3)
A
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
B <- matrix(c(20:25), 2, 3)
B
     [,1] [,2] [,3]
[1,]   20   22   24
[2,]   21   23   25
A + B
     [,1] [,2] [,3]
[1,]   21   25   29
[2,]   23   27   31
A - B
     [,1] [,2] [,3]
[1,]  -19  -19  -19
[2,]  -19  -19  -19

Matrix multiplication

A%*%t(B)
     [,1] [,2]
[1,]  206  215
[2,]  272  284
t(B)%*%A
     [,1] [,2] [,3]
[1,]   62  144  226
[2,]   68  158  248
[3,]   74  172  270

Multiplication by a scalar

3*A
     [,1] [,2] [,3]
[1,]    3    9   15
[2,]    6   12   18

Trace of a matrix: sum of its diagonal elements

sum(diag(A))
[1] 5

Cross-product matrix

str(USArrests)
'data.frame':  50 obs. of  4 variables:
 $ Murder  : num  13.2 10 8.1 8.8 9 7.9 3.3 5.9 15.4 17.4 ...
 $ Assault : int  236 263 294 190 276 204 110 238 335 211 ...
 $ UrbanPop: int  58 48 80 50 91 78 77 72 80 60 ...
 $ Rape    : num  21.2 44.5 31 19.5 40.6 38.7 11.1 15.8 31.9 25.8 ...
datamat <- as.matrix(USArrests)

t(datamat)%*%datamat
           Murder Assault UrbanPop      Rape
Murder    3962.20   80756  25736.2   9394.32
Assault  80756.00 1798262 574882.0 206723.00
UrbanPop 25736.20  574882 225041.0  72309.90
Rape      9394.32  206723  72309.9  26838.62
crossprod(datamat)
           Murder Assault UrbanPop      Rape
Murder    3962.20   80756  25736.2   9394.32
Assault  80756.00 1798262 574882.0 206723.00
UrbanPop 25736.20  574882 225041.0  72309.90
Rape      9394.32  206723  72309.9  26838.62

Determinant of a square matrix

det(crossprod(datamat))
[1] 5.203407e+16