Introduction: Matrices in R
While dealing with matrices in R, all columns in the matrix must have the same mode (numeric, character, etc.), and the same length. A matrix is a two-dimensional rectangular data set. It can be created using a vector input to the matrix( )
functions.
The general syntax of creating matrices in R is:
matrix_name <- matrix(vector, nrow = r, ncol = c, byrow = FALSE, dimnames = list(char_vector_rownames, char_vector_colnames) )
byrow = TRUE
indicates that the matrix should be filled by rows-wise.
dimnames
provides optional labels for the column and rows.
Creating Matrices in R
Following the general syntax of the matrix( )
function, let us create a matrix from a vector of the first 20 numbers.
Example 1:
# Generate matrix having 5 rows and 4 columns y1 <- matrix (1 : 20, nrow = 5, ncol = 4) ; y1 # Output > y1 [,1] [,2] [,3] [,4] [1,] 1 6 11 16 [2,] 2 7 12 17 [3,] 3 8 13 18 [4,] 4 9 14 19 [5,] 5 10 15 20
y2 <- matrix (1 : 20, nrow = 5, ncol = 4, byrow = FALSE); y2 # Output > y2 [,1] [,2] [,3] [,4] [1,] 1 6 11 16 [2,] 2 7 12 17 [3,] 3 8 13 18 [4,] 4 9 14 19 [5,] 5 10 15 20
y3 <- matrix (1 : 20, nrow = 5, ncol = 4, byrow = TRUE) ; y3 # Output > y3 [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12 [4,] 13 14 15 16 [5,] 17 18 19 20
Example 2:
elements <- c(11, 23, 29, 67) rownames <- c("R1", "R2") colnames <- c("C1", "C2") m1 <- matrix(elements, nrow = 2, ncol = 2, byrow = TRUE, dimnames = list(rownames, colnames) ) # Output > m1 C1 C2 R1 11 23 R2 29 67
Try
nrow = 4 and ncol = 1, byrow = FALSE
Note the difference. You may also have some errors related to the number of rows or columns. Therefore, if you change the number of rows or columns then ensure that you have the same number of row names and column names too.
Matrix Operations in R
In the R language, there are some operators and functions that can be used to perform computation on one or more matrices. Some basic matrix operations in R are:
Matrix Operation | Operator/ Function |
Add/ Subtract | +, − |
Multiply | %*% |
Transpose | t( ) |
Inverse | solve ( ) |
Extract Diagonal | diag( ) It is described at the end too |
Determinant | det( ) |
The following are some examples related to these operators and matrix functions.
m1 <- matrix(c(11, 23, 9, 35), nrow = 2) m2 <- matrix(c(5, 19, 11, 20), nrow =2) m3 <- m1 + m2 m4 <- m1 - m2 m5 <- m1 %*% m2 m6 <- m1 / m2 m1t <- t(m1) m1tminv <- solve(m1t %*% m1) diag(m1tminv) # Output > m1 [,1] [,2] [1,] 11 9 [2,] 23 35 > m2 [,1] [,2] [1,] 5 11 [2,] 19 20 > m3 [,1] [,2] [1,] 16 20 [2,] 42 55 > m4 [,1] [,2] [1,] 6 -2 [2,] 4 15 > m5 [,1] [,2] [1,] 226 301 [2,] 780 953 > m6 [,1] [,2] [1,] 2.200000 0.8181818 [2,] 1.210526 1.7500000 > m1t [,1] [,2] [1,] 11 23 [2,] 9 35 > m1tminv [,1] [,2] [1,] 0.04121954 -0.02853175 [2,] -0.02853175 0.02051509 > diag(m1tminv) [1] 0.04121954 0.02051509
There some other important functions that can be used to perform some required computation on matrices in R. These matrix operations in R are described below for matrix X
. You can use your own matrix.
Consider we have a matrix X
with elements.
X <- matrix(1:20, nrow = 4, ncol = 5) X
Function | Description |
rowSums(X) | Compute the total of each row of Matrix X |
colSums(X) | Compute the total of each column of Matrix X |
rowMeans(X) | Compute the average value of each row of Matrix X |
colMeans(X) | Compute the average value of each column of Matrix X |
diag(X) | Extract diagonal elements of Matrix X , orCreate a Matrix that has required diagonal elements such as diag(1:5) , diag(5) , |
crossprod(X,X) | Compute X’X. It is a shortcut of t(X)%*%X |
Obtaining $ \beta $’s using Matrices in R
Consider we have a dataset that has a response variable and few regressors. There are many ways to create data (or variables), such as one can create a vector for each variable, a data frame for all of the variables, matrices, or can read data stored in a file.
Here we try it using vectors, then bind the vectors where required.
y <- c(5, 6, 7, 9, 8, 4, 3, 2, 1, 6, 0, 7) x1 <- c(4, 5, 6, 7, 8, 3, 4, 9, 9, 8, 7, 5) x2 <- c(10, 22, 23, 10, 11, 14, 15, 16, 17, 12, 11, 17) x <- cbind(1, x1, x2)
The cbind( )
function is used to create a matrix x
. Note that 1 is also bounded to get the intercept term (the model with the intercept term). Let us compute $\beta$’s from OLS using matrix functions and operators.
xt <- t(x) xtx <- xt %*% x xtxinv <- solve(xtx) xty <- xt %*% y b <- xtxinv %*% xty
The output is
> x x1 x2 [1,] 1 4 10 [2,] 1 5 22 [3,] 1 6 23 [4,] 1 7 10 [5,] 1 8 11 [6,] 1 3 14 [7,] 1 4 15 [8,] 1 9 16 [9,] 1 9 17 [10,] 1 8 12 [11,] 1 7 11 [12,] 1 5 17 > xt [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] 1 1 1 1 1 1 1 1 1 1 1 1 x1 4 5 6 7 8 3 4 9 9 8 7 5 x2 10 22 23 10 11 14 15 16 17 12 11 17 > xtx x1 x2 12 75 178 x1 75 515 1103 x2 178 1103 2854 > xtxinv