# Check the vectors
c
## [1] 0 1 2 3 4 5 6 7 8 9 10
e
## [1] 1 2 2 3 4 4 5 5 5 6 7 7 8 9 9
f
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Scale f by two
f*2 # Multiplies f by 2 element-wise
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
# Check the length of the vectors
length(c)
## [1] 11
length(e)
## [1] 15
length(f)
## [1] 15
Can you take the dot product of c and f? Why?
# Take the dot product of c and f
#c %*% f
Take the dot product of two conformable vectors
# Create a Square matrix
M <- matrix(c(0, 2, 1, 0), nrow = 2, ncol = 2)
M
## [,1] [,2]
## [1,] 0 1
## [2,] 2 0
# Create an identity matrix
I <- diag(nrow=2, ncol=2)
I
## [,1] [,2]
## [1,] 1 0
## [2,] 0 1
# Check the matrix
A
## [,1] [,2]
## [1,] 2 1
## [2,] 3 2
## [3,] -2 2
# Scale A by 3
A*3
## [,1] [,2]
## [1,] 6 3
## [2,] 9 6
## [3,] -6 6
# Check Matrices
A
## [,1] [,2]
## [1,] 2 1
## [2,] 3 2
## [3,] -2 2
B
## [,1] [,2]
## [1,] 1 1
## [2,] 4 2
## [3,] -2 1
# Add A & B
A+B # Addition is made element-wise
## [,1] [,2]
## [1,] 3 2
## [2,] 7 4
## [3,] -4 3
# Subtract B from A
A-B # Subtraction is made element-wise
## [,1] [,2]
## [1,] 1 0
## [2,] -1 0
## [3,] 0 1
A*B # This is different than matrix multiplication
## [,1] [,2]
## [1,] 2 1
## [2,] 12 4
## [3,] 4 2
# Check the dimensions of the matrices
dim(A)
## [1] 3 2
dim(B)
## [1] 3 2
dim(D)
## [1] 2 3
Can you multiply A and B? Why?
# Multiply A by D
C <- A%*%D
dim(C) == c(nrow(A), ncol(D)) # The dimensions of the output matrix sould be: [nrow(1st matrix), ncol(2nd matrix)]
## [1] TRUE TRUE
# Check the input and output matrices
A
## [,1] [,2]
## [1,] 2 1
## [2,] 3 2
## [3,] -2 2
D
## [,1] [,2] [,3]
## [1,] 2 1 3
## [2,] -2 2 1
C
## [,1] [,2] [,3]
## [1,] 2 4 7
## [2,] 2 7 11
## [3,] -8 2 -4
What is the value in the first cell of the output matrix when multiplying D by A?
It is not always the case that we can swap matrices.
# Checking the dimensions of the matrices
dim(D)
## [1] 2 3
dim(I)
## [1] 2 2
# Multiplying matrices
I%*%D # ncol(I) == nrow(D)
## [,1] [,2] [,3]
## [1,] 2 1 3
## [2,] -2 2 1
#D%*%I # ncol(D) != nrow(I)
A
## [,1] [,2]
## [1,] 2 1
## [2,] 3 2
## [3,] -2 2
t(A)
## [,1] [,2] [,3]
## [1,] 2 3 -2
## [2,] 1 2 2
What matrices are invertible ?
# Take the inverse of M
solve(M)
## [,1] [,2]
## [1,] 0 0.5
## [2,] 1 0.0
# When we multiple a matrix by its inverse, we get the identity matrix
M %*% solve(M)
## [,1] [,2]
## [1,] 1 0
## [2,] 0 1
You may ask yourself: What’s the point? (Fair…)
Matrix algebra is used in OLS regression to solve complex linear equations. OLS regression is one way to assess the relationship between two variables.
We will only see a simple example of a linear equation here (we will leave OLS for POLI 572). After visiting the Spurious Correlations website, we know that the number of people who died by becoming entangled in their bedsheets increased by about 22 times as per capita cheese consumption increased by one pound for the 2005-2007 period.
We want to create a linear equation to represent this relationship, where the number of people who die by becoming entangled in their bedsheets is a linear function of a scalar, 22, and a vector containing per capita cheese consumption.
# Create a cheese consumption vector
cheese <- seq(from=28, to=33, by=.1) # using the seq() function to be more efficient
cheese
## [1] 28.0 28.1 28.2 28.3 28.4 28.5 28.6 28.7 28.8 28.9 29.0 29.1 29.2 29.3
## [15] 29.4 29.5 29.6 29.7 29.8 29.9 30.0 30.1 30.2 30.3 30.4 30.5 30.6 30.7
## [29] 30.8 30.9 31.0 31.1 31.2 31.3 31.4 31.5 31.6 31.7 31.8 31.9 32.0 32.1
## [43] 32.2 32.3 32.4 32.5 32.6 32.7 32.8 32.9 33.0
# Create a scalar named beta
beta <- 22
# Create the linear function
die_entangled <- beta*cheese
die_entangled
## [1] 616.0 618.2 620.4 622.6 624.8 627.0 629.2 631.4 633.6 635.8 638.0
## [12] 640.2 642.4 644.6 646.8 649.0 651.2 653.4 655.6 657.8 660.0 662.2
## [23] 664.4 666.6 668.8 671.0 673.2 675.4 677.6 679.8 682.0 684.2 686.4
## [34] 688.6 690.8 693.0 695.2 697.4 699.6 701.8 704.0 706.2 708.4 710.6
## [45] 712.8 715.0 717.2 719.4 721.6 723.8 726.0
Here is what the relationship between the number of people who die by becoming entangled in their bedsheets and per capita cheese consumption looks like.
Each point is an observation (here, a time unit), which has a value on the cheese vector/variable and a value on the die_entangled vector/variable.
# Plot the two variables
plot(x=cheese, y=die_entangled ) # x is on the horizontal axis, y on the vertical axis
This work by Sarah Lachance is licensed under CC BY-NC-ND 4.0