This week we reviewed matrices. To be honest, when I first started learning R, I thought that this data structure was not very useful, the data frame can be used for a lot more data. However recently in LIS 4761 Intro to Data and Text Mining, I have been learning how to do semantic analysis on Twitter data using R. A a part of the semantic analysis and bag of words methods is using Term Document Matrices and Document Term Matrices to get word frequencies in several documents, or in the case of that class, tweets. Thus having a good understanding of how to work with a matrix and what it can and cannot do will be beneficial.
For the R code I use in this post, you can find it on my GitHub here, or follow the link at the bottom.
To begin we can create two matrices.
#Given matrices
A <- matrix(1:100, nrow = 10)
B <- matrix(1:1000, nrow = 10)
Code language: R (r)
If I print the first matrix to the console, this is how R sees the matrix. I would show the other one, but it is a very wide matrix.
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 11 21 31 41 51 61 71 81 91
[2,] 2 12 22 32 42 52 62 72 82 92
[3,] 3 13 23 33 43 53 63 73 83 93
[4,] 4 14 24 34 44 54 64 74 84 94
[5,] 5 15 25 35 45 55 65 75 85 95
[6,] 6 16 26 36 46 56 66 76 86 96
[7,] 7 17 27 37 47 57 67 77 87 97
[8,] 8 18 28 38 48 58 68 78 88 98
[9,] 9 19 29 39 49 59 69 79 89 99
[10,] 10 20 30 40 50 60 70 80 90 100
Code language: plaintext (plaintext)
In this matrix take note of how the sequence increments by one going down the column, and up by 10 going across the row. By default R fills matrices in 1 column at a time.
We can also transpose the matrices using the following code:
#Transpose the matrices
A_t <- t(A)
B_t <- t(B)
Code language: PHP (php)
Again I will only print the first Matrix because Matrix B is huge. Matrix A_t looks like this:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 2 3 4 5 6 7 8 9 10
[2,] 11 12 13 14 15 16 17 18 19 20
[3,] 21 22 23 24 25 26 27 28 29 30
[4,] 31 32 33 34 35 36 37 38 39 40
[5,] 41 42 43 44 45 46 47 48 49 50
[6,] 51 52 53 54 55 56 57 58 59 60
[7,] 61 62 63 64 65 66 67 68 69 70
[8,] 71 72 73 74 75 76 77 78 79 80
[9,] 81 82 83 84 85 86 87 88 89 90
[10,] 91 92 93 94 95 96 97 98 99 100
Code language: plaintext (plaintext)
All that the transpose function has done is turn the columns into rows, and turned rows into columns. This could be useful if you have a dataset that maybe starts as Document Term Matrix, and you really want a Term Document Matrix. The transpose function can also be used for more advanced matrix algebra.
Now let us create two vectors for each matrix, and then multiply the matrix by its respective vector.
#Create two vectors
A_vector <- c(1:10)
B_vector <- c(1:100)
#Multiply the matrices by the vectors
A_multiplied <- A %*% A_vector
B_multiplied <- B %*% B_vector
Code language: R (r)
Now lets take a look at the Matrix A_multiplied.
[,1]
[1,] 3355
[2,] 3410
[3,] 3465
[4,] 3520
[5,] 3575
[6,] 3630
[7,] 3685
[8,] 3740
[9,] 3795
[10,] 3850
Code language: plaintext (plaintext)
In R, whenever the basic multiplication sign is used, it multiplies by the corresponding element. However that kind of result is not very interesting and is just as easily done in a data frame. If you are looking to do more matrix algebra type functions, then you need to use %*% to specify matrix multiplication.
For now I will reassign those vectors to the row length of their corresponding matrix. They will both now hold the value of 10 since that is the number of rows in each matrix.
#re-assign the vectors a and b to equal the number of rows of the column for the
#corresponding matrix
A_vector <- nrow(A)
B_vector <- nrow(B)
Code language: PHP (php)
So, how about multiplying to matrices together then? It is done just the same as the matrix and vector multiplication above.
#Multiply the matrix by a matrix
C <- A %*% B
Code language: R (r)
C is a giant matrix of 100 columns and 10 rows, and while I would want to print that out, it is too big to put in this blog post.
Then there is the inverse of the matrix. While it is complicated and hard to do by hand, R does it all in a single simple function, solve()
. However to solve a matrix like this it needs to be square, and the determinant cannot be zero. Both matrices used above do not meet the criteria. Matrix B is rectangular with 100 columns and 10 rows. Matrix A on the other hand is square, however if we use the function det()
to find its determinant, it returns the value 0, so it is also no good. But I want to demonstrate it, so lets create a new matrix that does meet the criteria.
#Inverse the matrix
S <- matrix(2:5, nrow = 2)
solve(S)
Code language: R (r)
S starts off as the following matrix:
[,1] [,2]
[1,] 2 4
[2,] 3 5
Code language: plaintext (plaintext)
And becomes the inverse Matrix:
[,1] [,2]
[1,] -2.5 2
[2,] 1.5 -1
Code language: plaintext (plaintext)
Understanding how to use more complicated matrix algebra like this will be useful for estimating your own regression coefficients. Being able to play with matrices has also been good practice for working with them in general.
Links:
GitHub: https://github.com/SimonLiles/LIS4370RProgramming/blob/main/LIS4370Mod5.R