Matrix Algebra in R
September 16, 2017
howto notes R review studyMatrices in R
Making a matrix
Convert a vector to a matrix, or add a vector to a matrix using:
cbindrbindmatrix
X <- matrix(1:60, 20, 3)
head(X)
## [,1] [,2] [,3]
## [1,] 1 21 41
## [2,] 2 22 42
## [3,] 3 23 43
## [4,] 4 24 44
## [5,] 5 25 45
## [6,] 6 26 46
As you can see, using the matrix function, we have just created a matrix consisting of 20 rows and 3 columns. By default, the sequence will go by columns, but we can use byrow = TRUE to have the sequence go along the rows.
x <- matrix(1:60, 20, 3, byrow=TRUE)
head(x)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
## [4,] 10 11 12
## [5,] 13 14 15
## [6,] 16 17 18
Matrix Operations
Professor Gilbert Strang has an excellent Linear Algebra class available online here. If ever confused, check it out. You can also find the accompanying recitations on YouTube.
Scalar Multiplication
#Make a matrix
X <- matrix(1:15, 5, 3)
print(X)
## [,1] [,2] [,3]
## [1,] 1 6 11
## [2,] 2 7 12
## [3,] 3 8 13
## [4,] 4 9 14
## [5,] 5 10 15
#Multiple matrix by scalar, e.g. 2
a <- 2
print(a*X)
## [,1] [,2] [,3]
## [1,] 2 12 22
## [2,] 4 14 24
## [3,] 6 16 26
## [4,] 8 18 28
## [5,] 10 20 30
Transpose
Converting rows to columns, and vice versa. This is done very simply in R using the t function on a matrix.
#Make a matrix
X <- matrix(1:15, 5, 3)
print(X)
## [,1] [,2] [,3]
## [1,] 1 6 11
## [2,] 2 7 12
## [3,] 3 8 13
## [4,] 4 9 14
## [5,] 5 10 15
#Transpose matrix, X
t(X)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 3 4 5
## [2,] 6 7 8 9 10
## [3,] 11 12 13 14 15
Identity Matrix
An identity matrix of \(N \times N\) dimensions can easily be constructed in R using the diag function.
diag(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
This gives a 4x4 identity matrix.
Inverse
Some, but not all, squared matrices have a corresponding inverse matrix. The inverse matrix (\(X^{-1}\)) is that matrix, which when multiplied by the original matrix (\(X\)), gives the identity matrix, \(I\).
\[X^{-1} \cdot X = I\]
In R, the inverse matrix can be obtained using the solve() function.
Matrix Multiplication
Generally, works so long as the number of columns of the first matrix has the same number of rows as the second matrix in our multiplication equation.
\[m \times n \cdot n \times p = m \times p\]
This highlights the dimensions of the matrices being multiplied and that the \(n\) of each matrix needs to be the same in order for this to work.
Matrix multiplication in R is done using %*%, e.g. x %*% y.
a <- matrix(1:12, nrow=4)
b <- matrix(1:15, nrow=3)
a %*% b
## [,1] [,2] [,3] [,4] [,5]
## [1,] 38 83 128 173 218
## [2,] 44 98 152 206 260
## [3,] 50 113 176 239 302
## [4,] 56 128 200 272 344
Let’s use the following example to illustrate how this computation can be done in R:
\[ \begin{equation} 2x - y = 0 \\ -x + 2y - z = -1 \\ -3y + 4z = 4 \end{equation} \]
Solving this matrix multiplication involves taking the inverse matrix as seen below: \[ \begin{bmatrix} 2 & -1 & 0 \\ -1 & 2 & -1 \\ 0 & -3 & 4 \end{bmatrix}^{-1} \begin{bmatrix} 0 \\ -1 \\ 4 \end{bmatrix} \]
The solution gives us the values of \(x, y,\) and \(z\).
A <- matrix(c(2, -1, 0, -1, 2, -3, 0, -1, 4), 3, 3)
# can also be created using:
# cbind(c(2, -1, 0), c(-1, 2, -3), c(0, -1, 4))
x <- matrix(c(0, -1, 4), 3, 1)
solve(A) %*% x
## [,1]
## [1,] 0
## [2,] 0
## [3,] 1
# can also be written as:
# solve(A, x)
Just to prove that solve(A) gives the inverse matrix, \(I\), and that \(I^{-1} \times I\) gives the identity matrix, let’s try it below.
solve(A) # gives the inverse matrix of A
## [,1] [,2] [,3]
## [1,] 0.8333333 0.6666667 0.1666667
## [2,] 0.6666667 1.3333333 0.3333333
## [3,] 0.5000000 1.0000000 0.5000000
solve(A) %*% A #should give the identiy matrix
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
These notes were compiled from the Rafael Irizarry’s MOOC on the subject.