SlideShare a Scribd company logo
1 of 52
R Matrix Math Quick Reference
Mark Niemann-Ross
2021-05-14
About
About This Document
This document provides brief explanations of the matrix functions available in Base
R and the matrix package.
RelatedMaterial
This Quick Reference is supplemental to courses on LinkedIn Learning. A quick
reference to plotting functions in R is here. A quick reference to clustering functions
in R is here. An index to all R functions covered at LinkedIn Learning is found here.
The latest version of this quick reference is found here. The source to this document
is found on github/mnr. This document is available as Free Software under the
terms of the Free Software Foundation’s GNU General Public License.
About Mark Niemann-Ross
Mark is an author for LinkedIn Learning and writes Science Fiction.
Quick Reference to Matrix Math Functions for the R
programming language
This Quick Reference is supplemental to courses on LinkedIn Learning.
The latest version of this quick reference is found here
A quick reference to plotting functions in base R is here
A quick reference to clustering functions in R is here
An index to all R functions covered at LinkedIn Learning is found here
The Matrix package is recommended for advanced operations.
Create a matrix
Instructional video about Matrix
matrix( c(1:9), nrow = 3)
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
Addition
A <- matrix( c(1:9), nrow = 3)
B <- matrix( c(11:19), nrow = 3)
A
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
B
## [,1] [,2] [,3]
## [1,] 11 14 17
## [2,] 12 15 18
## [3,] 13 16 19
A + B
## [,1] [,2] [,3]
## [1,] 12 18 24
## [2,] 14 20 26
## [3,] 16 22 28
Subtraction
A - B
## [,1] [,2] [,3]
## [1,] -10 -10 -10
## [2,] -10 -10 -10
## [3,] -10 -10 -10
B - A
## [,1] [,2] [,3]
## [1,] 10 10 10
## [2,] 10 10 10
## [3,] 10 10 10
Multiplication - simple
A * B
## [,1] [,2] [,3]
## [1,] 11 56 119
## [2,] 24 75 144
## [3,] 39 96 171
Multiplication - “dot product”
Instructional video about %*%
A %*% B
## [,1] [,2] [,3]
## [1,] 150 186 222
## [2,] 186 231 276
## [3,] 222 276 330
Division
HA - just kidding. There is no matrix division. Instead, use the inverse.
𝐴𝑋 = 𝐵 is solved for X with 𝑋 = 𝐴−1
𝐵
…lots more on inverse below…
The determinant of a matrix
Instructional video on det() and determinant()
The determinant of $begin{pmatrix} a & b c & d end{pmatrix}$ is 𝑎𝑑 − 𝑏𝑐
sampleMatrix <- matrix(c(10,12,5,30), nrow = 2)
sampleMatrix
## [,1] [,2]
## [1,] 10 5
## [2,] 12 30
det(sampleMatrix)
## [1] 240
…which is equivalent to 𝑎𝑑 − 𝑏𝑐 … which is written as…
sampleMatrix[1,1]*sampleMatrix[2,2] - sampleMatrix[1,2] *
sampleMatrix[2,1]
## [1] 240
determinant vs det
Instructional video on det() and determinant()
determinant() produces a list with $modulus and $sign
determinant(sampleMatrix)
## $modulus
## [1] 5.480639
## attr(,"logarithm")
## [1] TRUE
##
## $sign
## [1] 1
##
## attr(,"class")
## [1] "det"
determinant(sampleMatrix, logarithm = FALSE)
## $modulus
## [1] 240
## attr(,"logarithm")
## [1] FALSE
##
## $sign
## [1] 1
##
## attr(,"class")
## [1] "det"
Zero Matrix
matrix(0, nrow = 3, ncol = 3)
## [,1] [,2] [,3]
## [1,] 0 0 0
## [2,] 0 0 0
## [3,] 0 0 0
Identity Matrix
Instructional video on diag()
I <- diag(3)
I
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
Diagonal Matrix
Instructional video on diag()
diag(5, nrow = 3)
## [,1] [,2] [,3]
## [1,] 5 0 0
## [2,] 0 5 0
## [3,] 0 0 5
myMatrix <- A
diag(myMatrix) <- 8
myMatrix
## [,1] [,2] [,3]
## [1,] 8 4 7
## [2,] 2 8 8
## [3,] 3 6 8
Upper and Lower Triangular
Instructional video on lower.tri() and upper.tri()
upper.tri(A)
## [,1] [,2] [,3]
## [1,] FALSE TRUE TRUE
## [2,] FALSE FALSE TRUE
## [3,] FALSE FALSE FALSE
upper.tri(A, diag = TRUE)
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] FALSE TRUE TRUE
## [3,] FALSE FALSE TRUE
lower.tri(A)
## [,1] [,2] [,3]
## [1,] FALSE FALSE FALSE
## [2,] TRUE FALSE FALSE
## [3,] TRUE TRUE FALSE
myMatrix <- A
myMatrix[upper.tri(myMatrix)] <- NA
myMatrix
## [,1] [,2] [,3]
## [1,] 1 NA NA
## [2,] 2 5 NA
## [3,] 3 6 9
Matrix Comparison
There are two ways to compare matrices. First, create two matrices for example
comparison.
partiallyA <- A
partiallyA[upper.tri(partiallyA)] <- 1 # upper triangle becomes
different
simple matrice comparison, using equality
A == partiallyA # returns logical value for each element
## [,1] [,2] [,3]
## [1,] TRUE FALSE FALSE
## [2,] TRUE TRUE FALSE
## [3,] TRUE TRUE TRUE
object comparisonfor exact equality
identical(partiallyA, A) # returns logical value for entire matrice
comparison
## [1] FALSE
identical(A,A) # compare two identical objects
## [1] TRUE
Matrix transposition
Instructional video on t()
NonSymmetricMatrix <- matrix(c(1:10), nrow = 5)
NonSymmetricMatrix # show what it looks like
## [,1] [,2]
## [1,] 1 6
## [2,] 2 7
## [3,] 3 8
## [4,] 4 9
## [5,] 5 10
t(NonSymmetricMatrix) # t() for transpose...show what transpose looks
like
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 3 4 5
## [2,] 6 7 8 9 10
Distance between points
Options to choose how distance is calculated, what type of matrix is produced. See
the instructional video for details.
mymatrix <- matrix( c(1:6), nrow = 3)
mymatrix
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
dist(mymatrix, method = "euclidean" )
## 1 2
## 2 1.414214
## 3 2.828427 1.414214
Build a symmetric matrix
There is a package for matrix tools. Here’s how to do it with baseR
A + t(A)
## [,1] [,2] [,3]
## [1,] 2 6 10
## [2,] 6 10 14
## [3,] 10 14 18
Build a skew-symmetrix matrix
A - t(A)
## [,1] [,2] [,3]
## [1,] 0 2 4
## [2,] -2 0 2
## [3,] -4 -2 0
Test for symmetric matrix
Instructional video on isSymmetric()
isSymmetric(A) # not symmetric
## [1] FALSE
isSymmetric(A + t(A)) # symmetric
## [1] TRUE
isSymmetric(A - t(A)) # skew symmetric
## [1] FALSE
Inner product of two vectors
Simple dot-product …aka 𝑣𝑒𝑐1𝑇
𝑣𝑒𝑐2
vec1 <- c(1:3)
vec2 <- c(1:3)
vec1 # what do these look like?
## [1] 1 2 3
vec2
## [1] 1 2 3
t(vec1) %*% vec2 # inner product
## [,1]
## [1,] 14
Outer product of two vectors
Instructional video on outer() and %o%
Transpose the second vector … 𝑣𝑒𝑐1𝑣𝑒𝑐2𝑇
These three versions produce the same result…
vec1 %*% t(vec2) # the actual formula
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 2 4 6
## [3,] 3 6 9
outer(vec1, vec2) # the R function
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 2 4 6
## [3,] 3 6 9
vec1 %o% vec2 # %o% is a wrapper for outer()
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 2 4 6
## [3,] 3 6 9
Outer product of two matrices
First…a reminder of the contents of matrix A and B
A
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
B
## [,1] [,2] [,3]
## [1,] 11 14 17
## [2,] 12 15 18
## [3,] 13 16 19
Then…here’s the outer product. This multiplies the first matrix by individual values
from the second matrix.
Think of this as…
A * B[1,1]
A * B[2,1]
…and so on
outer(A,B) # ... A %o% B will produce the same result
## , , 1, 1
##
## [,1] [,2] [,3]
## [1,] 11 44 77
## [2,] 22 55 88
## [3,] 33 66 99
##
## , , 2, 1
##
## [,1] [,2] [,3]
## [1,] 12 48 84
## [2,] 24 60 96
## [3,] 36 72 108
##
## , , 3, 1
##
## [,1] [,2] [,3]
## [1,] 13 52 91
## [2,] 26 65 104
## [3,] 39 78 117
##
## , , 1, 2
##
## [,1] [,2] [,3]
## [1,] 14 56 98
## [2,] 28 70 112
## [3,] 42 84 126
##
## , , 2, 2
##
## [,1] [,2] [,3]
## [1,] 15 60 105
## [2,] 30 75 120
## [3,] 45 90 135
##
## , , 3, 2
##
## [,1] [,2] [,3]
## [1,] 16 64 112
## [2,] 32 80 128
## [3,] 48 96 144
##
## , , 1, 3
##
## [,1] [,2] [,3]
## [1,] 17 68 119
## [2,] 34 85 136
## [3,] 51 102 153
##
## , , 2, 3
##
## [,1] [,2] [,3]
## [1,] 18 72 126
## [2,] 36 90 144
## [3,] 54 108 162
##
## , , 3, 3
##
## [,1] [,2] [,3]
## [1,] 19 76 133
## [2,] 38 95 152
## [3,] 57 114 171
Solve a system of equations
Instructional video about solve()
As an example, start with this system of equations:
$$ 2x_1 - 3x_2 - 1x_3 = 2 1x_1 + 2x_2 + 3x_3 = 15 5x_1 + 1x_2 - 1x_3 = 4 $$
…when converted to a matrix…
$$begin{pmatrix} 2 & -3 & -1 1 & 2 & 3 5 & 1 & -1 end{pmatrix}
begin{pmatrix} x_1 x_2 x_3 end{pmatrix} = begin{pmatrix} 2 15
4 end{pmatrix}$$
…then, to solve with R…
coef_A <- matrix(c(2,1,5,-3,2,1,-1,3,-1), nrow = 3)
RHS_system <- matrix(c(2,15,4), nrow = 3)
solve(coef_A, RHS_system)
## [,1]
## [1,] 2
## [2,] -1
## [3,] 5
Inverse matrix
Instructional video about solving for an inverse matrix
𝐴𝐴−
1 = 𝐼 … -1 is the inverse of a matrix. I is the identity matrix
solve(partiallyA) # solve() finds the inverse of a matrix
## [,1] [,2] [,3]
## [1,] 1.8571429 -0.1428571 -0.19047619
## [2,] -0.7142857 0.2857143 0.04761905
## [3,] -0.1428571 -0.1428571 0.14285714
Note: Some matrices return and error of Lapack routine dgesv: system is
exactly singular: U[3,3] = 0
Also note: solve() can be used in other ways. Refer to documentation.
Permutations
n <- 3 # for an n x n matrix
factorial(n) # provides the number of permutations
## [1] 6
BaseR doesn’t have a function to generate all possible permutations, but there are
packages that will do this.
backsolve
Instructional video about forward and backward solve for a matrix
Backsolve solves a triangular system of linear equations. This can come from a
gaussian elimination (for example).
Start with… begin{align} -3x_1 + 2x_2 - x_3 &= -1 -2x_2 + 5x_3 &= -9 -2x_3 &= 2
end{align}
# r holds a matrix of coefficients for the system to be solved
r <- matrix(c(-3, 2, -1,
0, -2, 5,
0, 0, -2),
nrow = 3, byrow = TRUE)
# x is a column matrix with the solutions
x <- matrix(c(-1, -9, 2), ncol = 1)
# backsolve produces the values of x.
backsolve(r, x)
## [,1]
## [1,] 2
## [2,] 2
## [3,] -1
Backsolve produces a column matrix where 𝑚𝑎𝑡𝑟𝑖𝑥(𝑐(𝑥3,𝑥2,𝑥1),𝑛𝑐𝑜𝑙 = 1)
forwardsolve
Instructional video about forward and backward solve for a matrix
forwardsolve uses the lower triangular matrix. (backsolve() uses the upper
triangular matrix.) For example…
begin{align} 2 &= -2x_3 -9 &= 5x_3 -2x_2  -1 &= -x_3+ 2x_2 -3x_1 end{align}
# r holds a matrix of coefficients for the system to be solved
l <- matrix(c(-2, 0, 0,
5, -2, 0,
-1, 2, -3),
nrow = 3, byrow = TRUE)
# x is a column matrix with the solutions
x <- matrix(c(2, -9, -1), ncol = 1)
# forwardsolve produces the values of x.
forwardsolve(l, x)
## [,1]
## [1,] -1
## [2,] 2
## [3,] 2
Singular Value Decomposition
Instructional video about SVD and QR decomposition
svd(A)
## $d
## [1] 1.684810e+01 1.068370e+00 5.543107e-16
##
## $u
## [,1] [,2] [,3]
## [1,] -0.4796712 0.77669099 0.4082483
## [2,] -0.5723678 0.07568647 -0.8164966
## [3,] -0.6650644 -0.62531805 0.4082483
##
## $v
## [,1] [,2] [,3]
## [1,] -0.2148372 -0.8872307 0.4082483
## [2,] -0.5205874 -0.2496440 -0.8164966
## [3,] -0.8263375 0.3879428 0.4082483
QR Decomposition
Instructional video about SVD and QR decomposition
Useful for solving 𝐴𝑥 = 𝑏 (𝐴 being a matrix, 𝑏 being a vector)
qr(A)
## $qr
## [,1] [,2] [,3]
## [1,] -3.7416574 -8.552360 -1.336306e+01
## [2,] 0.5345225 1.963961 3.927922e+00
## [3,] 0.8017837 0.988693 1.776357e-15
##
## $rank
## [1] 2
##
## $qraux
## [1] 1.267261e+00 1.149954e+00 1.776357e-15
##
## $pivot
## [1] 1 2 3
##
## attr(,"class")
## [1] "qr"
crossproduct
Instructional video about crossproduct()
Not to be confused with a dot product (which is done with %*%). Regarding when
to use crossprod() vs t(A) %*% B … one may be faster than another based on the
matrix being sparse. But this will only make a difference on massive matrices.
crossprod(A,B)
## [,1] [,2] [,3]
## [1,] 74 92 110
## [2,] 182 227 272
## [3,] 290 362 434
…which is equivalent to…
t(A) %*% B
## [,1] [,2] [,3]
## [1,] 74 92 110
## [2,] 182 227 272
## [3,] 290 362 434
tcrossproduct
tcrossprod(A,B)
## [,1] [,2] [,3]
## [1,] 186 198 210
## [2,] 228 243 258
## [3,] 270 288 306
…which is equivalent to…
A %*% t(B)
## [,1] [,2] [,3]
## [1,] 186 198 210
## [2,] 228 243 258
## [3,] 270 288 306
eigenvalues, eigenvectors
Instructional video about Eigenvalues and Eigenvectors()
eigen() returns both eigen values and eigen vectors
Here’s a matrix…
A
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
Here’s the eigen value and vector
eigen(A)
## eigen() decomposition
## $values
## [1] 1.611684e+01 -1.116844e+00 -5.700691e-16
##
## $vectors
## [,1] [,2] [,3]
## [1,] -0.4645473 -0.8829060 0.4082483
## [2,] -0.5707955 -0.2395204 -0.8164966
## [3,] -0.6770438 0.4038651 0.4082483
Choleski Decomposition
symmetricalMatrix <- matrix(c(20,12,5,
12,15,2,
5,2,25), nrow = 3)
isSymmetric(symmetricalMatrix)
## [1] TRUE
chol(symmetricalMatrix)
## [,1] [,2] [,3]
## [1,] 4.472136 2.683282 1.1180340
## [2,] 0.000000 2.792848 -0.3580574
## [3,] 0.000000 0.000000 4.8602258
finally - look at package::matrix
#install.packages("Matrix")
library(Matrix)

More Related Content

What's hot

Potencias resueltas 1eso (1)
Potencias resueltas 1eso (1)Potencias resueltas 1eso (1)
Potencias resueltas 1eso (1)Lina Manriquez
 
الجلسة الأولى
الجلسة الأولىالجلسة الأولى
الجلسة الأولىYaman Rajab
 
ゲーム理論NEXT コア第4回(最終回) -平衡ゲームとコア-
ゲーム理論NEXT コア第4回(最終回) -平衡ゲームとコア-ゲーム理論NEXT コア第4回(最終回) -平衡ゲームとコア-
ゲーム理論NEXT コア第4回(最終回) -平衡ゲームとコア-ssusere0a682
 
Finite Triple Integral Representation For The Polynomial Set Tn(x1 ,x2 ,x3 ,x4 )
Finite Triple Integral Representation For The Polynomial Set Tn(x1 ,x2 ,x3 ,x4 )Finite Triple Integral Representation For The Polynomial Set Tn(x1 ,x2 ,x3 ,x4 )
Finite Triple Integral Representation For The Polynomial Set Tn(x1 ,x2 ,x3 ,x4 )iosrjce
 
A Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter twoA Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter twoChung Hua Universit
 
Trigonometry 10th edition larson solutions manual
Trigonometry 10th edition larson solutions manualTrigonometry 10th edition larson solutions manual
Trigonometry 10th edition larson solutions manualLarson2017
 

What's hot (10)

Potencias resueltas 1eso (1)
Potencias resueltas 1eso (1)Potencias resueltas 1eso (1)
Potencias resueltas 1eso (1)
 
الجلسة الأولى
الجلسة الأولىالجلسة الأولى
الجلسة الأولى
 
presentazione
presentazionepresentazione
presentazione
 
Capitulo 4 Soluciones Purcell 9na Edicion
Capitulo 4 Soluciones Purcell 9na EdicionCapitulo 4 Soluciones Purcell 9na Edicion
Capitulo 4 Soluciones Purcell 9na Edicion
 
Capitulo 5 Soluciones Purcell 9na Edicion
Capitulo 5 Soluciones Purcell 9na EdicionCapitulo 5 Soluciones Purcell 9na Edicion
Capitulo 5 Soluciones Purcell 9na Edicion
 
ゲーム理論NEXT コア第4回(最終回) -平衡ゲームとコア-
ゲーム理論NEXT コア第4回(最終回) -平衡ゲームとコア-ゲーム理論NEXT コア第4回(最終回) -平衡ゲームとコア-
ゲーム理論NEXT コア第4回(最終回) -平衡ゲームとコア-
 
Finite Triple Integral Representation For The Polynomial Set Tn(x1 ,x2 ,x3 ,x4 )
Finite Triple Integral Representation For The Polynomial Set Tn(x1 ,x2 ,x3 ,x4 )Finite Triple Integral Representation For The Polynomial Set Tn(x1 ,x2 ,x3 ,x4 )
Finite Triple Integral Representation For The Polynomial Set Tn(x1 ,x2 ,x3 ,x4 )
 
E024033041
E024033041E024033041
E024033041
 
A Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter twoA Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter two
 
Trigonometry 10th edition larson solutions manual
Trigonometry 10th edition larson solutions manualTrigonometry 10th edition larson solutions manual
Trigonometry 10th edition larson solutions manual
 

Similar to R Matrix Math Quick Reference

Writing Readable Code with Pipes
Writing Readable Code with PipesWriting Readable Code with Pipes
Writing Readable Code with PipesRsquared Academy
 
Breaking a Stick to form a Pentagon with Positive Integers using Programming ...
Breaking a Stick to form a Pentagon with Positive Integers using Programming ...Breaking a Stick to form a Pentagon with Positive Integers using Programming ...
Breaking a Stick to form a Pentagon with Positive Integers using Programming ...IRJET Journal
 
Basic operations by novi reandy sasmita
Basic operations by novi reandy sasmitaBasic operations by novi reandy sasmita
Basic operations by novi reandy sasmitabeasiswa
 
Datamining r 1st
Datamining r 1stDatamining r 1st
Datamining r 1stsesejun
 
PRE: Datamining 2nd R
PRE: Datamining 2nd RPRE: Datamining 2nd R
PRE: Datamining 2nd Rsesejun
 
Datamining R 1st
Datamining R 1stDatamining R 1st
Datamining R 1stsesejun
 
Introduction to R - Lab slides for UGA course FANR 6750
Introduction to R - Lab slides for UGA course FANR 6750Introduction to R - Lab slides for UGA course FANR 6750
Introduction to R - Lab slides for UGA course FANR 6750richardchandler
 
Data manipulation and visualization in r 20190711 myanmarucsy
Data manipulation and visualization in r 20190711 myanmarucsyData manipulation and visualization in r 20190711 myanmarucsy
Data manipulation and visualization in r 20190711 myanmarucsySmartHinJ
 
A practical work of matlab
A practical work of matlabA practical work of matlab
A practical work of matlabSalanSD
 
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)Wataru Shito
 
Palestra sobre Collections com Python
Palestra sobre Collections com PythonPalestra sobre Collections com Python
Palestra sobre Collections com Pythonpugpe
 
第2回 基本演算,データ型の基礎,ベクトルの操作方法
第2回 基本演算,データ型の基礎,ベクトルの操作方法第2回 基本演算,データ型の基礎,ベクトルの操作方法
第2回 基本演算,データ型の基礎,ベクトルの操作方法Wataru Shito
 
The Ring programming language version 1.7 book - Part 64 of 196
The Ring programming language version 1.7 book - Part 64 of 196The Ring programming language version 1.7 book - Part 64 of 196
The Ring programming language version 1.7 book - Part 64 of 196Mahmoud Samir Fayed
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environmentYogendra Chaubey
 

Similar to R Matrix Math Quick Reference (20)

Writing Readable Code with Pipes
Writing Readable Code with PipesWriting Readable Code with Pipes
Writing Readable Code with Pipes
 
Breaking a Stick to form a Pentagon with Positive Integers using Programming ...
Breaking a Stick to form a Pentagon with Positive Integers using Programming ...Breaking a Stick to form a Pentagon with Positive Integers using Programming ...
Breaking a Stick to form a Pentagon with Positive Integers using Programming ...
 
Basic operations by novi reandy sasmita
Basic operations by novi reandy sasmitaBasic operations by novi reandy sasmita
Basic operations by novi reandy sasmita
 
Datamining r 1st
Datamining r 1stDatamining r 1st
Datamining r 1st
 
PRE: Datamining 2nd R
PRE: Datamining 2nd RPRE: Datamining 2nd R
PRE: Datamining 2nd R
 
Datamining R 1st
Datamining R 1stDatamining R 1st
Datamining R 1st
 
Learning R
Learning RLearning R
Learning R
 
Introduction to R - Lab slides for UGA course FANR 6750
Introduction to R - Lab slides for UGA course FANR 6750Introduction to R - Lab slides for UGA course FANR 6750
Introduction to R - Lab slides for UGA course FANR 6750
 
QMC: Undergraduate Workshop, Tutorial on 'R' Software - Yawen Guan, Feb 26, 2...
QMC: Undergraduate Workshop, Tutorial on 'R' Software - Yawen Guan, Feb 26, 2...QMC: Undergraduate Workshop, Tutorial on 'R' Software - Yawen Guan, Feb 26, 2...
QMC: Undergraduate Workshop, Tutorial on 'R' Software - Yawen Guan, Feb 26, 2...
 
Data manipulation and visualization in r 20190711 myanmarucsy
Data manipulation and visualization in r 20190711 myanmarucsyData manipulation and visualization in r 20190711 myanmarucsy
Data manipulation and visualization in r 20190711 myanmarucsy
 
A practical work of matlab
A practical work of matlabA practical work of matlab
A practical work of matlab
 
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop - Xi...
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop  - Xi...PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop  - Xi...
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop - Xi...
 
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)
 
Palestra sobre Collections com Python
Palestra sobre Collections com PythonPalestra sobre Collections com Python
Palestra sobre Collections com Python
 
Programming meeting #8
Programming meeting #8Programming meeting #8
Programming meeting #8
 
Image Processing in R
Image Processing in R Image Processing in R
Image Processing in R
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
第2回 基本演算,データ型の基礎,ベクトルの操作方法
第2回 基本演算,データ型の基礎,ベクトルの操作方法第2回 基本演算,データ型の基礎,ベクトルの操作方法
第2回 基本演算,データ型の基礎,ベクトルの操作方法
 
The Ring programming language version 1.7 book - Part 64 of 196
The Ring programming language version 1.7 book - Part 64 of 196The Ring programming language version 1.7 book - Part 64 of 196
The Ring programming language version 1.7 book - Part 64 of 196
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
 

Recently uploaded

Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...nirzagarg
 
Vastral Call Girls Book Now 7737669865 Top Class Escort Service Available
Vastral Call Girls Book Now 7737669865 Top Class Escort Service AvailableVastral Call Girls Book Now 7737669865 Top Class Escort Service Available
Vastral Call Girls Book Now 7737669865 Top Class Escort Service Availablegargpaaro
 
👉 Bhilai Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Girl Ser...
👉 Bhilai Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Girl Ser...👉 Bhilai Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Girl Ser...
👉 Bhilai Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Girl Ser...vershagrag
 
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...kumargunjan9515
 
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...HyderabadDolls
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNKTimothy Spann
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareGraham Ware
 
Statistics notes ,it includes mean to index numbers
Statistics notes ,it includes mean to index numbersStatistics notes ,it includes mean to index numbers
Statistics notes ,it includes mean to index numberssuginr1
 
Belur $ Female Escorts Service in Kolkata (Adult Only) 8005736733 Escort Serv...
Belur $ Female Escorts Service in Kolkata (Adult Only) 8005736733 Escort Serv...Belur $ Female Escorts Service in Kolkata (Adult Only) 8005736733 Escort Serv...
Belur $ Female Escorts Service in Kolkata (Adult Only) 8005736733 Escort Serv...HyderabadDolls
 
Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubaikojalkojal131
 
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...HyderabadDolls
 
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...HyderabadDolls
 
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...gajnagarg
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...nirzagarg
 
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...vershagrag
 
Oral Sex Call Girls Kashmiri Gate Delhi Just Call 👉👉 📞 8448380779 Top Class C...
Oral Sex Call Girls Kashmiri Gate Delhi Just Call 👉👉 📞 8448380779 Top Class C...Oral Sex Call Girls Kashmiri Gate Delhi Just Call 👉👉 📞 8448380779 Top Class C...
Oral Sex Call Girls Kashmiri Gate Delhi Just Call 👉👉 📞 8448380779 Top Class C...Delhi Call girls
 
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...gajnagarg
 
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabiaahmedjiabur940
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRajesh Mondal
 

Recently uploaded (20)

Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
 
Vastral Call Girls Book Now 7737669865 Top Class Escort Service Available
Vastral Call Girls Book Now 7737669865 Top Class Escort Service AvailableVastral Call Girls Book Now 7737669865 Top Class Escort Service Available
Vastral Call Girls Book Now 7737669865 Top Class Escort Service Available
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
 
👉 Bhilai Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Girl Ser...
👉 Bhilai Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Girl Ser...👉 Bhilai Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Girl Ser...
👉 Bhilai Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Girl Ser...
 
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
 
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham Ware
 
Statistics notes ,it includes mean to index numbers
Statistics notes ,it includes mean to index numbersStatistics notes ,it includes mean to index numbers
Statistics notes ,it includes mean to index numbers
 
Belur $ Female Escorts Service in Kolkata (Adult Only) 8005736733 Escort Serv...
Belur $ Female Escorts Service in Kolkata (Adult Only) 8005736733 Escort Serv...Belur $ Female Escorts Service in Kolkata (Adult Only) 8005736733 Escort Serv...
Belur $ Female Escorts Service in Kolkata (Adult Only) 8005736733 Escort Serv...
 
Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubai
 
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
 
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
 
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
 
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...
 
Oral Sex Call Girls Kashmiri Gate Delhi Just Call 👉👉 📞 8448380779 Top Class C...
Oral Sex Call Girls Kashmiri Gate Delhi Just Call 👉👉 📞 8448380779 Top Class C...Oral Sex Call Girls Kashmiri Gate Delhi Just Call 👉👉 📞 8448380779 Top Class C...
Oral Sex Call Girls Kashmiri Gate Delhi Just Call 👉👉 📞 8448380779 Top Class C...
 
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...
 
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for Research
 

R Matrix Math Quick Reference

  • 1. R Matrix Math Quick Reference Mark Niemann-Ross 2021-05-14
  • 2. About About This Document This document provides brief explanations of the matrix functions available in Base R and the matrix package. RelatedMaterial This Quick Reference is supplemental to courses on LinkedIn Learning. A quick reference to plotting functions in R is here. A quick reference to clustering functions in R is here. An index to all R functions covered at LinkedIn Learning is found here. The latest version of this quick reference is found here. The source to this document is found on github/mnr. This document is available as Free Software under the terms of the Free Software Foundation’s GNU General Public License. About Mark Niemann-Ross Mark is an author for LinkedIn Learning and writes Science Fiction.
  • 3. Quick Reference to Matrix Math Functions for the R programming language This Quick Reference is supplemental to courses on LinkedIn Learning. The latest version of this quick reference is found here A quick reference to plotting functions in base R is here A quick reference to clustering functions in R is here An index to all R functions covered at LinkedIn Learning is found here The Matrix package is recommended for advanced operations.
  • 4. Create a matrix Instructional video about Matrix matrix( c(1:9), nrow = 3) ## [,1] [,2] [,3] ## [1,] 1 4 7 ## [2,] 2 5 8 ## [3,] 3 6 9
  • 5. Addition A <- matrix( c(1:9), nrow = 3) B <- matrix( c(11:19), nrow = 3) A ## [,1] [,2] [,3] ## [1,] 1 4 7 ## [2,] 2 5 8 ## [3,] 3 6 9 B ## [,1] [,2] [,3] ## [1,] 11 14 17 ## [2,] 12 15 18 ## [3,] 13 16 19 A + B ## [,1] [,2] [,3] ## [1,] 12 18 24
  • 6. ## [2,] 14 20 26 ## [3,] 16 22 28
  • 7. Subtraction A - B ## [,1] [,2] [,3] ## [1,] -10 -10 -10 ## [2,] -10 -10 -10 ## [3,] -10 -10 -10 B - A ## [,1] [,2] [,3] ## [1,] 10 10 10 ## [2,] 10 10 10 ## [3,] 10 10 10
  • 8. Multiplication - simple A * B ## [,1] [,2] [,3] ## [1,] 11 56 119 ## [2,] 24 75 144 ## [3,] 39 96 171
  • 9. Multiplication - “dot product” Instructional video about %*% A %*% B ## [,1] [,2] [,3] ## [1,] 150 186 222 ## [2,] 186 231 276 ## [3,] 222 276 330
  • 10. Division HA - just kidding. There is no matrix division. Instead, use the inverse. 𝐴𝑋 = 𝐵 is solved for X with 𝑋 = 𝐴−1 𝐵 …lots more on inverse below…
  • 11. The determinant of a matrix Instructional video on det() and determinant() The determinant of $begin{pmatrix} a & b c & d end{pmatrix}$ is 𝑎𝑑 − 𝑏𝑐 sampleMatrix <- matrix(c(10,12,5,30), nrow = 2) sampleMatrix ## [,1] [,2] ## [1,] 10 5 ## [2,] 12 30 det(sampleMatrix) ## [1] 240 …which is equivalent to 𝑎𝑑 − 𝑏𝑐 … which is written as… sampleMatrix[1,1]*sampleMatrix[2,2] - sampleMatrix[1,2] * sampleMatrix[2,1]
  • 13. determinant vs det Instructional video on det() and determinant() determinant() produces a list with $modulus and $sign determinant(sampleMatrix) ## $modulus ## [1] 5.480639 ## attr(,"logarithm") ## [1] TRUE ## ## $sign ## [1] 1 ## ## attr(,"class") ## [1] "det" determinant(sampleMatrix, logarithm = FALSE)
  • 14. ## $modulus ## [1] 240 ## attr(,"logarithm") ## [1] FALSE ## ## $sign ## [1] 1 ## ## attr(,"class") ## [1] "det"
  • 15. Zero Matrix matrix(0, nrow = 3, ncol = 3) ## [,1] [,2] [,3] ## [1,] 0 0 0 ## [2,] 0 0 0 ## [3,] 0 0 0
  • 16. Identity Matrix Instructional video on diag() I <- diag(3) I ## [,1] [,2] [,3] ## [1,] 1 0 0 ## [2,] 0 1 0 ## [3,] 0 0 1
  • 17. Diagonal Matrix Instructional video on diag() diag(5, nrow = 3) ## [,1] [,2] [,3] ## [1,] 5 0 0 ## [2,] 0 5 0 ## [3,] 0 0 5 myMatrix <- A diag(myMatrix) <- 8 myMatrix ## [,1] [,2] [,3] ## [1,] 8 4 7 ## [2,] 2 8 8 ## [3,] 3 6 8
  • 18. Upper and Lower Triangular Instructional video on lower.tri() and upper.tri() upper.tri(A) ## [,1] [,2] [,3] ## [1,] FALSE TRUE TRUE ## [2,] FALSE FALSE TRUE ## [3,] FALSE FALSE FALSE upper.tri(A, diag = TRUE) ## [,1] [,2] [,3] ## [1,] TRUE TRUE TRUE ## [2,] FALSE TRUE TRUE ## [3,] FALSE FALSE TRUE lower.tri(A) ## [,1] [,2] [,3] ## [1,] FALSE FALSE FALSE
  • 19. ## [2,] TRUE FALSE FALSE ## [3,] TRUE TRUE FALSE myMatrix <- A myMatrix[upper.tri(myMatrix)] <- NA myMatrix ## [,1] [,2] [,3] ## [1,] 1 NA NA ## [2,] 2 5 NA ## [3,] 3 6 9
  • 20. Matrix Comparison There are two ways to compare matrices. First, create two matrices for example comparison. partiallyA <- A partiallyA[upper.tri(partiallyA)] <- 1 # upper triangle becomes different simple matrice comparison, using equality A == partiallyA # returns logical value for each element ## [,1] [,2] [,3] ## [1,] TRUE FALSE FALSE ## [2,] TRUE TRUE FALSE ## [3,] TRUE TRUE TRUE object comparisonfor exact equality identical(partiallyA, A) # returns logical value for entire matrice comparison
  • 21. ## [1] FALSE identical(A,A) # compare two identical objects ## [1] TRUE
  • 22. Matrix transposition Instructional video on t() NonSymmetricMatrix <- matrix(c(1:10), nrow = 5) NonSymmetricMatrix # show what it looks like ## [,1] [,2] ## [1,] 1 6 ## [2,] 2 7 ## [3,] 3 8 ## [4,] 4 9 ## [5,] 5 10 t(NonSymmetricMatrix) # t() for transpose...show what transpose looks like ## [,1] [,2] [,3] [,4] [,5] ## [1,] 1 2 3 4 5 ## [2,] 6 7 8 9 10
  • 23. Distance between points Options to choose how distance is calculated, what type of matrix is produced. See the instructional video for details. mymatrix <- matrix( c(1:6), nrow = 3) mymatrix ## [,1] [,2] ## [1,] 1 4 ## [2,] 2 5 ## [3,] 3 6 dist(mymatrix, method = "euclidean" ) ## 1 2 ## 2 1.414214 ## 3 2.828427 1.414214
  • 24. Build a symmetric matrix There is a package for matrix tools. Here’s how to do it with baseR A + t(A) ## [,1] [,2] [,3] ## [1,] 2 6 10 ## [2,] 6 10 14 ## [3,] 10 14 18
  • 25. Build a skew-symmetrix matrix A - t(A) ## [,1] [,2] [,3] ## [1,] 0 2 4 ## [2,] -2 0 2 ## [3,] -4 -2 0
  • 26. Test for symmetric matrix Instructional video on isSymmetric() isSymmetric(A) # not symmetric ## [1] FALSE isSymmetric(A + t(A)) # symmetric ## [1] TRUE isSymmetric(A - t(A)) # skew symmetric ## [1] FALSE
  • 27. Inner product of two vectors Simple dot-product …aka 𝑣𝑒𝑐1𝑇 𝑣𝑒𝑐2 vec1 <- c(1:3) vec2 <- c(1:3) vec1 # what do these look like? ## [1] 1 2 3 vec2 ## [1] 1 2 3 t(vec1) %*% vec2 # inner product ## [,1] ## [1,] 14
  • 28. Outer product of two vectors Instructional video on outer() and %o% Transpose the second vector … 𝑣𝑒𝑐1𝑣𝑒𝑐2𝑇 These three versions produce the same result… vec1 %*% t(vec2) # the actual formula ## [,1] [,2] [,3] ## [1,] 1 2 3 ## [2,] 2 4 6 ## [3,] 3 6 9 outer(vec1, vec2) # the R function ## [,1] [,2] [,3] ## [1,] 1 2 3 ## [2,] 2 4 6 ## [3,] 3 6 9
  • 29. vec1 %o% vec2 # %o% is a wrapper for outer() ## [,1] [,2] [,3] ## [1,] 1 2 3 ## [2,] 2 4 6 ## [3,] 3 6 9
  • 30. Outer product of two matrices First…a reminder of the contents of matrix A and B A ## [,1] [,2] [,3] ## [1,] 1 4 7 ## [2,] 2 5 8 ## [3,] 3 6 9 B ## [,1] [,2] [,3] ## [1,] 11 14 17 ## [2,] 12 15 18 ## [3,] 13 16 19 Then…here’s the outer product. This multiplies the first matrix by individual values from the second matrix. Think of this as…
  • 31. A * B[1,1] A * B[2,1] …and so on outer(A,B) # ... A %o% B will produce the same result ## , , 1, 1 ## ## [,1] [,2] [,3] ## [1,] 11 44 77 ## [2,] 22 55 88 ## [3,] 33 66 99 ## ## , , 2, 1 ## ## [,1] [,2] [,3] ## [1,] 12 48 84 ## [2,] 24 60 96 ## [3,] 36 72 108 ## ## , , 3, 1
  • 32. ## ## [,1] [,2] [,3] ## [1,] 13 52 91 ## [2,] 26 65 104 ## [3,] 39 78 117 ## ## , , 1, 2 ## ## [,1] [,2] [,3] ## [1,] 14 56 98 ## [2,] 28 70 112 ## [3,] 42 84 126 ## ## , , 2, 2 ## ## [,1] [,2] [,3] ## [1,] 15 60 105 ## [2,] 30 75 120 ## [3,] 45 90 135 ## ## , , 3, 2 ##
  • 33. ## [,1] [,2] [,3] ## [1,] 16 64 112 ## [2,] 32 80 128 ## [3,] 48 96 144 ## ## , , 1, 3 ## ## [,1] [,2] [,3] ## [1,] 17 68 119 ## [2,] 34 85 136 ## [3,] 51 102 153 ## ## , , 2, 3 ## ## [,1] [,2] [,3] ## [1,] 18 72 126 ## [2,] 36 90 144 ## [3,] 54 108 162 ## ## , , 3, 3 ## ## [,1] [,2] [,3]
  • 34. ## [1,] 19 76 133 ## [2,] 38 95 152 ## [3,] 57 114 171
  • 35. Solve a system of equations Instructional video about solve() As an example, start with this system of equations: $$ 2x_1 - 3x_2 - 1x_3 = 2 1x_1 + 2x_2 + 3x_3 = 15 5x_1 + 1x_2 - 1x_3 = 4 $$ …when converted to a matrix… $$begin{pmatrix} 2 & -3 & -1 1 & 2 & 3 5 & 1 & -1 end{pmatrix} begin{pmatrix} x_1 x_2 x_3 end{pmatrix} = begin{pmatrix} 2 15 4 end{pmatrix}$$ …then, to solve with R… coef_A <- matrix(c(2,1,5,-3,2,1,-1,3,-1), nrow = 3) RHS_system <- matrix(c(2,15,4), nrow = 3) solve(coef_A, RHS_system)
  • 36. ## [,1] ## [1,] 2 ## [2,] -1 ## [3,] 5
  • 37. Inverse matrix Instructional video about solving for an inverse matrix 𝐴𝐴− 1 = 𝐼 … -1 is the inverse of a matrix. I is the identity matrix solve(partiallyA) # solve() finds the inverse of a matrix ## [,1] [,2] [,3] ## [1,] 1.8571429 -0.1428571 -0.19047619 ## [2,] -0.7142857 0.2857143 0.04761905 ## [3,] -0.1428571 -0.1428571 0.14285714 Note: Some matrices return and error of Lapack routine dgesv: system is exactly singular: U[3,3] = 0 Also note: solve() can be used in other ways. Refer to documentation.
  • 38. Permutations n <- 3 # for an n x n matrix factorial(n) # provides the number of permutations ## [1] 6 BaseR doesn’t have a function to generate all possible permutations, but there are packages that will do this.
  • 39. backsolve Instructional video about forward and backward solve for a matrix Backsolve solves a triangular system of linear equations. This can come from a gaussian elimination (for example). Start with… begin{align} -3x_1 + 2x_2 - x_3 &= -1 -2x_2 + 5x_3 &= -9 -2x_3 &= 2 end{align} # r holds a matrix of coefficients for the system to be solved r <- matrix(c(-3, 2, -1, 0, -2, 5, 0, 0, -2), nrow = 3, byrow = TRUE) # x is a column matrix with the solutions x <- matrix(c(-1, -9, 2), ncol = 1) # backsolve produces the values of x. backsolve(r, x)
  • 40. ## [,1] ## [1,] 2 ## [2,] 2 ## [3,] -1 Backsolve produces a column matrix where 𝑚𝑎𝑡𝑟𝑖𝑥(𝑐(𝑥3,𝑥2,𝑥1),𝑛𝑐𝑜𝑙 = 1)
  • 41. forwardsolve Instructional video about forward and backward solve for a matrix forwardsolve uses the lower triangular matrix. (backsolve() uses the upper triangular matrix.) For example… begin{align} 2 &= -2x_3 -9 &= 5x_3 -2x_2 -1 &= -x_3+ 2x_2 -3x_1 end{align} # r holds a matrix of coefficients for the system to be solved l <- matrix(c(-2, 0, 0, 5, -2, 0, -1, 2, -3), nrow = 3, byrow = TRUE) # x is a column matrix with the solutions x <- matrix(c(2, -9, -1), ncol = 1) # forwardsolve produces the values of x. forwardsolve(l, x)
  • 42. ## [,1] ## [1,] -1 ## [2,] 2 ## [3,] 2
  • 43. Singular Value Decomposition Instructional video about SVD and QR decomposition svd(A) ## $d ## [1] 1.684810e+01 1.068370e+00 5.543107e-16 ## ## $u ## [,1] [,2] [,3] ## [1,] -0.4796712 0.77669099 0.4082483 ## [2,] -0.5723678 0.07568647 -0.8164966 ## [3,] -0.6650644 -0.62531805 0.4082483 ## ## $v ## [,1] [,2] [,3] ## [1,] -0.2148372 -0.8872307 0.4082483 ## [2,] -0.5205874 -0.2496440 -0.8164966 ## [3,] -0.8263375 0.3879428 0.4082483
  • 44. QR Decomposition Instructional video about SVD and QR decomposition Useful for solving 𝐴𝑥 = 𝑏 (𝐴 being a matrix, 𝑏 being a vector) qr(A) ## $qr ## [,1] [,2] [,3] ## [1,] -3.7416574 -8.552360 -1.336306e+01 ## [2,] 0.5345225 1.963961 3.927922e+00 ## [3,] 0.8017837 0.988693 1.776357e-15 ## ## $rank ## [1] 2 ## ## $qraux ## [1] 1.267261e+00 1.149954e+00 1.776357e-15 ## ## $pivot
  • 45. ## [1] 1 2 3 ## ## attr(,"class") ## [1] "qr"
  • 46. crossproduct Instructional video about crossproduct() Not to be confused with a dot product (which is done with %*%). Regarding when to use crossprod() vs t(A) %*% B … one may be faster than another based on the matrix being sparse. But this will only make a difference on massive matrices. crossprod(A,B) ## [,1] [,2] [,3] ## [1,] 74 92 110 ## [2,] 182 227 272 ## [3,] 290 362 434 …which is equivalent to… t(A) %*% B ## [,1] [,2] [,3] ## [1,] 74 92 110
  • 47. ## [2,] 182 227 272 ## [3,] 290 362 434
  • 48. tcrossproduct tcrossprod(A,B) ## [,1] [,2] [,3] ## [1,] 186 198 210 ## [2,] 228 243 258 ## [3,] 270 288 306 …which is equivalent to… A %*% t(B) ## [,1] [,2] [,3] ## [1,] 186 198 210 ## [2,] 228 243 258 ## [3,] 270 288 306
  • 49. eigenvalues, eigenvectors Instructional video about Eigenvalues and Eigenvectors() eigen() returns both eigen values and eigen vectors Here’s a matrix… A ## [,1] [,2] [,3] ## [1,] 1 4 7 ## [2,] 2 5 8 ## [3,] 3 6 9 Here’s the eigen value and vector eigen(A) ## eigen() decomposition ## $values ## [1] 1.611684e+01 -1.116844e+00 -5.700691e-16
  • 50. ## ## $vectors ## [,1] [,2] [,3] ## [1,] -0.4645473 -0.8829060 0.4082483 ## [2,] -0.5707955 -0.2395204 -0.8164966 ## [3,] -0.6770438 0.4038651 0.4082483
  • 51. Choleski Decomposition symmetricalMatrix <- matrix(c(20,12,5, 12,15,2, 5,2,25), nrow = 3) isSymmetric(symmetricalMatrix) ## [1] TRUE chol(symmetricalMatrix) ## [,1] [,2] [,3] ## [1,] 4.472136 2.683282 1.1180340 ## [2,] 0.000000 2.792848 -0.3580574 ## [3,] 0.000000 0.000000 4.8602258
  • 52. finally - look at package::matrix #install.packages("Matrix") library(Matrix)