|
|
|
|
> a <- c(1, 3, 2) > a
[1] 1 3 2The vector a is in R printed "in row format" but can really be regarded as a column vector, cfr. the convention above.
|
|
> t(a)
[,1] [,2] [,3] [1,] 1 3 2
|
|
> 7 * a
[1] 7 21 14
|
|
> a <- c(1, 3, 2) > b <- c(2, 8, 9) > a+b
[1] 3 11 11
|
> sum(a * b)
[1] 44
|
> sqrt(sum(a * a))
[1] 3.741657
> rep(0, 5)
[1] 0 0 0 0 0
> rep(1, 5)
[1] 1 1 1 1 1
|
> v1 <- c(1, 1) > v2 <- c(-1, 1) > sum(v1 * v2)
[1] 0
|
|
> A <- matrix(c(1, 3, 2, 2, 8, 9), ncol=3) > A
[,1] [,2] [,3] [1,] 1 2 8 [2,] 3 2 9Note that the numbers 1,3,2,2,8,9 are read into the matrix column-by-column. To get the numbers read in row-by-row do
> A2 <- matrix(c(1, 3, 2, 2, 8, 9), ncol=3, byrow=T) > A2
[,1] [,2] [,3] [1,] 1 3 2 [2,] 2 8 9
|
> 7 * A
[,1] [,2] [,3] [1,] 7 14 56 [2,] 21 14 63
|
> t(A)
[,1] [,2] [1,] 1 3 [2,] 2 2 [3,] 8 9
|
> B <- matrix(c(5, 8, 3, 4, 2, 7), ncol=3, byrow=T) > A + B
[,1] [,2] [,3] [1,] 6 10 11 [2,] 7 4 16
|
|
> A %*% a
[,1] [1,] 23 [2,] 27Note the difference to
> A * a
[,1] [,2] [,3] [1,] 1 4 24 [2,] 9 2 18Please figure out yourself what goes on!
|
|
|
> A <- matrix(c(1, 3, 2, 2, 8, 9), ncol=2) > B <- matrix(c(5, 8, 4, 2), ncol=2) > A %*% B
[,1] [,2] [1,] 21 8 [2,] 79 28 [3,] 82 26
> matrix(0, nrow=2, ncol=3)
[,1] [,2] [,3] [1,] 0 0 0 [2,] 0 0 0
> matrix(1, nrow=2, ncol=3)
[,1] [,2] [,3] [1,] 1 1 1 [2,] 1 1 1
> diag(c(1, 2, 3))
[,1] [,2] [,3] [1,] 1 0 0 [2,] 0 2 0 [3,] 0 0 3
> diag(1, 3)
[,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1Note what happens when diag is applied to a matrix:
> diag(diag(c(1, 2, 3)))
[1] 1 2 3
> diag(A)
[1] 1 8
|
|
> A <- matrix(c(1, 3, 2, 4), ncol=2,byrow=T) > A
[,1] [,2] [1,] 1 3 [2,] 2 4
> #M2 <- matrix(c(-2,1.5,1,-0.5),ncol=2,byrow=T) > B <- solve(A) > B
[,1] [,2] [1,] -2 1.5 [2,] 1 -0.5
> A %*% B
[,1] [,2] [1,] 1 0 [2,] 0 1
|
|
|
|
> A <- matrix(c(1, 2, 3, 4), ncol=2) > b <- c(7, 10) > x <- solve(A) %*% b > x
[,1] [1,] 1 [2,] 2
|
|
|
|
|
|
|
|
|
|
> A <- matrix(c(1, 2, 3, 2, 3, 4, 3, 5, 7), ncol=3) > A # 3rd column is sum of the two first; the inverse does not exist
[,1] [,2] [,3] [1,] 1 2 3 [2,] 2 3 5 [3,] 3 4 7
> det(A)
[1] 0
> G <- MASS::ginv(A) > A %*% G # Not identity
[,1] [,2] [,3] [1,] 0.8333333 0.3333333 -0.1666667 [2,] 0.3333333 0.3333333 0.3333333 [3,] -0.1666667 0.3333333 0.8333333
> A %*% G %*% A # This is A
[,1] [,2] [,3] [1,] 1 2 3 [2,] 2 3 5 [3,] 3 4 7
> A <- matrix(c(2, 2, 3, 3, 5, 9, 5, 6, 7), ncol=3) > A
[,1] [,2] [,3] [1,] 2 3 5 [2,] 2 5 6 [3,] 3 9 7We want to find the matrix B=A−1. To start, we append to A the identity matrix and call the result AB:
> AB <- cbind(A, diag(c(1, 1, 1))) > AB
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 2 3 5 1 0 0 [2,] 2 5 6 0 1 0 [3,] 3 9 7 0 0 1On a matrix we allow ourselves to do the following three operations (sometimes called elementary operations) as often as we want:
> AB[1,] <- AB[1,] / AB[1,1] > AB[2,] <- AB[2,] - 2 * AB[1,] > AB[3,] <- AB[3,] - 3 * AB[1,] > AB
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 1.5 2.5 0.5 0 0 [2,] 0 2.0 1.0 -1.0 1 0 [3,] 0 4.5 -0.5 -1.5 0 1
> AB[2,] <- AB[2,] / AB[2,2] > AB[3,] <- AB[3,] - 4.5 * AB[2,]
> AB[3,] <- AB[3,] / AB[3,3] > AB
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 1.5 2.5 0.5000000 0.0000000 0.0000000 [2,] 0 1.0 0.5 -0.5000000 0.5000000 0.0000000 [3,] 0 0.0 1.0 -0.2727273 0.8181818 -0.3636364Then AB has zeros below the main diagonal.
> AB[2,] <- AB[2,] - 0.5 * AB[3,] > AB[1,] <- AB[1,] - 2.5 * AB[3,] > AB
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 1.5 0 1.1818182 -2.04545455 0.9090909 [2,] 0 1.0 0 -0.3636364 0.09090909 0.1818182 [3,] 0 0.0 1 -0.2727273 0.81818182 -0.3636364
> AB[1,] <- AB[1,] - 1.5 * AB[2,] > AB
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 0 0 1.7272727 -2.18181818 0.6363636 [2,] 0 1 0 -0.3636364 0.09090909 0.1818182 [3,] 0 0 1 -0.2727273 0.81818182 -0.3636364
> B <- AB[,4:6] > A%*% B
[,1] [,2] [,3] [1,] 1.000000e+00 3.330669e-16 1.110223e-16 [2,] -4.440892e-16 1.000000e+00 2.220446e-16 [3,] -2.220446e-16 9.992007e-16 1.000000e+00So, apart from rounding errors, the product is the identity matrix, and hence B=A−1. This example illustrates that numerical precision and rounding errors is an important issue when making computer programs.
x | 1.00 | 2.00 | 3.00 | 4.00 | 5.00 |
y | 3.70 | 4.20 | 4.90 | 5.70 | 6.00 |
|
|
|
|
|
> y
[1] 3.7 4.2 4.9 5.7 6.0
> X
x [1,] 1 1 [2,] 1 2 [3,] 1 3 [4,] 1 4 [5,] 1 5
> beta.hat <- solve(t(X) %*% X) %*% t(X) %*% y > beta.hat
[,1] 3.07 x 0.61The fitted values are
> as.numeric(X %*% beta.hat)
[1] 3.68 4.29 4.90 5.51 6.12
> X2 <- cbind(X, rowSums(X)) > X2
x [1,] 1 1 2 [2,] 1 2 3 [3,] 1 3 4 [4,] 1 4 5 [5,] 1 5 6Then (X2T X2) is not invertible. There are infinitely many solutions to the normal equations. One is:
> G <- MASS::ginv(t(X2) %*% X2) > G
[,1] [,2] [,3] [1,] 0.6333333 -0.4333333 0.20000000 [2,] -0.4333333 0.3000000 -0.13333333 [3,] 0.2000000 -0.1333333 0.06666667
> beta2.hat <- G %*% t(X2) %*% y > beta2.hat
[,1] [1,] 1.8433333 [2,] -0.6166667 [3,] 1.2266667Another solution is (why?)
> beta22.hat <- c(beta.hat, 0)The fitted values are the same:
> as.numeric(X2 %*% beta2.hat)
[1] 3.68 4.29 4.90 5.51 6.12
> as.numeric(X2 %*% beta22.hat)
[1] 3.68 4.29 4.90 5.51 6.12