3.4 Vectors and Matrices
Vectors
For our purposes, a vector is a list or “array” of numbers. For example, this might be a variable in our data– a list of the ages of all politicians in a country.
Addition
- If we have two vectors and , where
- \(\mathbf{u} \ = \ (u_1, u_2, \dots u_n)\) and
- \(\mathbf{v} \ = \ (v_1, v_2, \dots v_n)\),
- \(\mathbf{u} + \mathbf{v} = (u_1 + v_1, u_2 + v_2, \dots u_n + v_n)\)
- Note: \(\mathbf{u}\) and \(\mathbf{v}\) must be of the same dimensionality - number of elements in each must be the same - for addition.
Scalar multiplication
- If we have a scalar (i.e., a single number) \(\lambda\) and a vector \(\mathbf{u}\)
- \(\lambda \mathbf{u} = (\lambda u_1, \lambda u_2, \dots \lambda u_n)\)
We can implement vector addition and scalar multiplication in R.
Let’s create a vector \(\mathbf{u}\), a vector \(\mathbf{v}\), and a number lambda.
<- c(33, 44, 22, 11)
u <- c(6, 7, 8, 2)
v <- 3 lambda
When you add two vectors in R, it adds each component together.
+ v u
[1] 39 51 30 13
We can multiply each element of a vector, u
by lambda
:
* u lambda
[1] 99 132 66 33
Element-wise Multiplication
Note: When you multiply two vectors together in R, it will take each element of one vector and multiply it by each element of the other vector.
u * v
\(= (u_1 * v_1, u_2 * v_2, \dots u_n * v_n)\)
* v u
[1] 198 308 176 22
3.4.1 Matrix Basics
A matrix represents arrays of numbers in a rectangle, with rows and columns.
- A matrix with \(m\) rows and \(n\) columns is defined as (\(m\) x \(n\)). What is the dimensionality of the matrix A below?
\(A = \begin{pmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \\ a_{41} & a_{42} & a_{43} \end{pmatrix}\)
In R, we can think of a matrix as a set of vectors. For example, we could combine the vectors u
and v
we created above into a matrix defined as W.
## cbind() binds together vectors as columns
<- cbind(u, v)
Wcol Wcol
u v
[1,] 33 6
[2,] 44 7
[3,] 22 8
[4,] 11 2
## rbind() binds together vectors as rows
<- rbind(u, v)
Wrow Wrow
[,1] [,2] [,3] [,4]
u 33 44 22 11
v 6 7 8 2
There are other ways to create matrices in R, but using cbind
and rbind()
are common.
We can find the dimensions of our matrices using dim()
or nrow()
and ncol()
together. For example:
dim(Wcol)
[1] 4 2
nrow(Wcol)
[1] 4
ncol(Wcol)
[1] 2
Note how the dimensions are different from the version created with rbind()
:
dim(Wrow)
[1] 2 4
nrow(Wrow)
[1] 2
ncol(Wrow)
[1] 4
Extracting specific components
The element \(a_{ij}\) signifies the element is in the \(i\)th row and \(j\)th column of matrix A. For example, \(a_{12}\) is in the first row and second column.
- Square matrices have the same number of rows and columns
- Vectors have just one row or one column (e.g., \(x_1\) element of \(\mathbf{x}\) vector)
In R, we can use brackets to extract a specific \(ij\) element of a matrix or vector.
Wcol
u v
[1,] 33 6
[2,] 44 7
[3,] 22 8
[4,] 11 2
2,1] # element in the second row, first column Wcol[
u
44
2,] # all elements in the second row Wcol[
u v
44 7
1] # all elements in the first column Wcol[,
[1] 33 44 22 11
For matrices, to extract a particular entry in R, you have a comma between entries because there are both rows and columns. For vectors, you only have one entry, so no comma is needed.
u
[1] 33 44 22 11
2] # second element in the u vector u[
[1] 44
3.4.2 Matrix Operations
Matrix Addition
- To be able to add matrix A and matrix B, they must have the same dimensions.
- Like vector addition, to add matrices, you add each of the components together.
\(A = \begin{pmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \end{pmatrix}\) and \(B = \begin{pmatrix} b_{11} & b_{12} & b_{13}\\ b_{21} & b_{22} & b_{23} \\ b_{31} & b_{32} & b_{33} \end{pmatrix}\)
\(A + B = \begin{pmatrix} a_{11} + b_{11} & a_{12} + b_{12} & a_{13} + b_{13}\\ a_{21} + b_{21} & a_{22} + b_{22} & a_{23} + b_{23} \\ a_{31} + b_{31} & a_{32} + b_{32} & a_{33} + b_{33} \end{pmatrix}\)
\(Q = \begin{pmatrix} 2 & 4 & 1\\ 6 & 1 & 5 \end{pmatrix}\) \(+\) \(R = \begin{pmatrix} 9 & 4 & 2\\ 11 & 8 & 7 \end{pmatrix} = Q + R = \begin{pmatrix} 11 & 8 & 3\\ 17 & 9 & 12 \end{pmatrix}\)
Scalar Multiplication
Take a scalar \(\nu\). Just like vectors, we multiply each component of a matrix by the scalar.
\(\nu Q = \begin{pmatrix} \nu q_{11} & \nu q_{12} & \dots & \nu q_{1n}\\ \nu q_{21} & \nu q_{22} & \dots & \nu q_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ \nu q_{m1} & \nu q_{m2} & \dots & \nu q_{mn} \end{pmatrix}\)
Example: Take \(c = 2\) and a matrix A.
\(cA = c *\begin{pmatrix} 4 & 6 & 1\\ 3 & 2 & 8 \end{pmatrix}\) = \(\begin{pmatrix} 8 & 12 & 2\\ 6 & 4 & 16 \end{pmatrix}\)
Note the Commutativity/Associativity: For scalar \(c\): \(c(AB) = (cA)B = A(cB) = (AB)c\).
Matrix Multiplication
A matrix A and B must be conformable to multiply AB.
- To be comformable, for \(m_A\) x \(n_A\) matrix A and \(m_B\) x \(n_B\) matrix B, the “inside” dimensions must be equal: \(n_A = m_B\).
- The resulting AB has the “outside” dimensions: \(m_A\) x \(n_B\).
For each \(c_{ij}\) component of \(C = AB\), we take the inner product of the \(i^{th}\) row of matrix A and the \(j^{th}\) column of matrix B.
- Their product C = AB is the \(m\) x \(n\) matrix where:
- \(c_{ij} =a_{i1}b_{1j} + a_{i2}b_{2j} + \dots + a_{ik}b_{kj}\)
Example: This \(2 \times 3\) matrix is multiplied by a \(3 \times 2\) matrix, resulting in the \(2 \times 2\) matrix.
\(\begin{pmatrix} 4 & 6 & 1\\ 3 & 2 & 8 \end{pmatrix}\) \(\times\) \(\begin{pmatrix} 8 & 12 \\ 6 & 4 \\ 7 & 10 \end{pmatrix}\) = \(\begin{pmatrix} (4*8 + 6*6 + 1*7) & (4*12 + 6*4 + 1*10) \\ (3*8 + 2*6 + 8*7) & (3*12 + 2*4 + 8*10) \end{pmatrix}\)
For example, the entry in the first row and second column of the new matrix \(c_{12} = (a_{11} = 4* b_{11} = 12) + (a_{12} = 6*b_{21} = 4) + (a_{13} = 1*b_{31} = 10)\)
We can also do matrix multiplication in R.
## Create a 3 x 2 matrix A
<- cbind(c(3, 4, 6), c(5, 6, 8))
A A
[,1] [,2]
[1,] 3 5
[2,] 4 6
[3,] 6 8
## Create a 2 x 4 matrix B
<- cbind(c(6,8), c(7, 9), c(3, 6), c(1, 11))
B B
[,1] [,2] [,3] [,4]
[1,] 6 7 3 1
[2,] 8 9 6 11
Note that the multiplication AB is conformable because the number of columns in A matches the number of rows in B:
ncol(A)
nrow(B)
[1] 2
[1] 2
To multiply matrices together in R, we need to add symbols around the standard asterisk for multiplication:
%*% B A
[,1] [,2] [,3] [,4]
[1,] 58 66 39 58
[2,] 72 82 48 70
[3,] 100 114 66 94
That is necessary for multiplying matrices together. It is not necessary for scalar multiplication, where we take a single number (e.g., c = 3) and multiply it with a matrix:
<- 3
c *A c
[,1] [,2]
[1,] 9 15
[2,] 12 18
[3,] 18 24
Note the equivalence of the below expressions, which combine scalar and matrix multiplication:
* (A %*% B) c
[,1] [,2] [,3] [,4]
[1,] 174 198 117 174
[2,] 216 246 144 210
[3,] 300 342 198 282
* A) %*% B (c
[,1] [,2] [,3] [,4]
[1,] 174 198 117 174
[2,] 216 246 144 210
[3,] 300 342 198 282
%*% (c * B) A
[,1] [,2] [,3] [,4]
[1,] 174 198 117 174
[2,] 216 246 144 210
[3,] 300 342 198 282
In social science, one matrix of interest is often a rectangular dataset that includes column vectors representing independent variables, as well as another vector that includes your dependent variable. These might have 1000 or more rows and a handful of columns you care about.