MASM22/FMSN30(40) Linear and Logistic Regression (with Data Gathering)

Basic matrix manipulation

Create some matrices:

A <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, byrow = TRUE)
A
#>      [,1] [,2]
#> [1,]    1    2
#> [2,]    3    4
#> [3,]    5    6

B <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE)
B
#>      [,1] [,2] [,3]
#> [1,]    1    2    3
#> [2,]    4    5    6

C <- matrix(c(1, 2, 3, 4), nrow = 2, byrow = TRUE)
C
#>      [,1] [,2]
#> [1,]    1    2
#> [2,]    3    4

Transpose them:

A
#>      [,1] [,2]
#> [1,]    1    2
#> [2,]    3    4
#> [3,]    5    6
t(A)
#>      [,1] [,2] [,3]
#> [1,]    1    3    5
#> [2,]    2    4    6

B
#>      [,1] [,2] [,3]
#> [1,]    1    2    3
#> [2,]    4    5    6
t(B)
#>      [,1] [,2]
#> [1,]    1    4
#> [2,]    2    5
#> [3,]    3    6

Matrix multiplication:

AB <- A %*% B
AB
#>      [,1] [,2] [,3]
#> [1,]    9   12   15
#> [2,]   19   26   33
#> [3,]   29   40   51

BA <- B %*% A
BA
#>      [,1] [,2]
#> [1,]   22   28
#> [2,]   49   64

# This won't work due to wrong dimension:
 C %*% A
#> Error in C %*% A: non-conformable arguments

Matrix multiplication vs element by element:

BA
#>      [,1] [,2]
#> [1,]   22   28
#> [2,]   49   64
C
#>      [,1] [,2]
#> [1,]    1    2
#> [2,]    3    4

BA %*% C
#>      [,1] [,2]
#> [1,]  106  156
#> [2,]  241  354
BA * C
#>      [,1] [,2]
#> [1,]   22   56
#> [2,]  147  256

Matrix inverse: \(A^{-1}\):

solve(A)
#> Error in solve.default(A): 'a' (3 x 2) must be square
solve(AB)
#> Error in solve.default(AB): system is computationally singular: reciprocal condition number = 1.7943e-17
solve(BA)
#>           [,1]       [,2]
#> [1,]  1.777778 -0.7777778
#> [2,] -1.361111  0.6111111

Whatโ€™s wrong with solve(AB)?

# AB is a 3 x 3 matrix but only has rank 2:
qr(AB)$rank
#> [1] 2

# One of the eigen values is 0:
eigen(AB)$values
#> [1] 8.557934e+01 4.206623e-01 1.490034e-15

# The first and second column are perfectly linear:
plot(AB[,2] ~ AB[,1])
abline(lm(AB[,2] ~ AB[,1]))

Identity matrix:

I3 <- diag(3)
I3
#>      [,1] [,2] [,3]
#> [1,]    1    0    0
#> [2,]    0    1    0
#> [3,]    0    0    1
I2 <- diag(2)
I2
#>      [,1] [,2]
#> [1,]    1    0
#> [2,]    0    1

AB
#>      [,1] [,2] [,3]
#> [1,]    9   12   15
#> [2,]   19   26   33
#> [3,]   29   40   51
AB %*% I3
#>      [,1] [,2] [,3]
#> [1,]    9   12   15
#> [2,]   19   26   33
#> [3,]   29   40   51

4 * I2
#>      [,1] [,2]
#> [1,]    4    0
#> [2,]    0    4

4 + I2
#>      [,1] [,2]
#> [1,]    5    4
#> [2,]    4    5