Introduction to linear algebra in R

Søren Højsgaard,
Department of Mathematical Sciences,
Aalborg University, Denmark
https://people.math.aau.dk/~sorenh/

Contents

1  Introduction
2  Vectors
    2.1  Vectors
    2.2  Transpose of vectors
    2.3  Multiplying a vector by a number
    2.4  Sum of vectors
    2.5  Inner product of vectors
    2.6  The length (norm) of a vector
    2.7  The 0-vector and 1-vector
    2.8  Orthogonal (perpendicular) vectors
3  Matrices
    3.1  Matrices
    3.2  Multiplying a matrix with a number
    3.3  Transpose of matrices
    3.4  Sum of matrices
    3.5  Multiplication of a matrix and a vector
    3.6  Multiplication of matrices
    3.7  Vectors as matrices
    3.8  Some special matrices
    3.9  Inverse of matrices
    3.10  Solving systems of linear equations
    3.11  Some additional rules for matrix operations
    3.12  Details on inverse matrices*
        3.12.1  Inverse of a 2×2 matrix*
        3.12.2  Inverse of diagonal matrices*
        3.12.3  Generalized inverse*
        3.12.4  Inverting an n×n matrix*
4  Least squares
    4.1  Least squares with generalized inverse
5  A neat little exercise - from a bird's perspective

1  Introduction

The first version of these notes were written in 2005. These notes/slides have two aims: 1) Introducing linear algebra (vectors and matrices) and 2) showing how to work with these concepts in R. They were written in an attempt to give a specific group of students a "feeling" for what matrices, vectors etc. are all about. Hence the notes/slides are not suitable for a course in linear algebra.

2  Vectors

2.1  Vectors

A column vector is a list of numbers stacked on top of each other, e.g. 
a =



2
1
3




A row vector is a list of numbers written one after the other, e.g. 
b = (2, 1, 3)
In both cases, the list is ordered, i.e. 
(2,1,3) ≠ (1,2,3).
We make the following convention:
A general n-vector has the form
a=





a1
a2
:
an






where the ais are numbers, and this vector shall be written a=(a1,...,an).
A graphical representation of 2-vectors is shown Figure 1.
fig/LinAlg-VectorFig01.png
Figure 1: Two 2-vectors
Note that row and column vectors are drawn the same way.

3  Matrices

3.1  Matrices

An r ×c matrix A (reads "an r times c matrix") is a table with r rows and c columns
A =





a11
a12
...
a1c
a21
a22
...
a2c
:
:
···
:
ar1
ar2
...
arc






Note that one can regard A as consisting of c columns vectors put after each other:
A = [ a1: a2 : ... : ac]
Likewise one can regard A as consisting of r row vectors stacked on to of each other.

3.11  Some additional rules for matrix operations

For matrices A, B and C whose dimension match appropriately: the following rules apply
(A+ B)T = AT + BT

(AB)T = BT AT

A(B+C) = AB+AC

AB = AC \not⇒ B=C
In genereal AB ≠ BA
AI = I A = A
If α is a number then αA B = A(αB)

3.12  Details on inverse matrices*

3.12.1  Inverse of a 2×2 matrix*

It is easy find the inverse for a 2 ×2 matrix. When
A =


a
b
c
d



then the inverse is
A−1 = 1

ad−bc



d
−b
−c
a



under the assumption that ab−bc ≠ 0. The number ab−bc is called the determinant of A, sometimes written |A| or det(A). A matrix A has an inverse if and only if |A| ≠ 0.

3.12.2  Inverse of diagonal matrices*

Finding the inverse of a diagonal matrix is easy: Let
A = diag(a1, a2,...,an)
where all ai ≠ 0. Then the inverse is
A−1 = diag( 1

a1
, 1

a2
,..., 1

an
)
If one ai=0 then A−1 does not exist.

3.12.3  Generalized inverse*

Not all square matrices have an inverse. However all square matrices have an infinite number of generalized inverses. A generalized inverse of a square matrix A is a matrix G satisfying that
A G A = A.
For many practical problems it suffice to find a generalized inverse.
> A <- matrix(c(1, 2, 3, 2, 3, 4, 3, 5, 7), ncol=3)
> A # 3rd column is sum of the two first; the inverse does not exist
 
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    3    5
[3,]    3    4    7
 
> det(A) 
 
[1] 0
 
> G <- MASS::ginv(A)
> A %*% G        # Not identity
 
           [,1]      [,2]       [,3]
[1,]  0.8333333 0.3333333 -0.1666667
[2,]  0.3333333 0.3333333  0.3333333
[3,] -0.1666667 0.3333333  0.8333333
 
> A %*% G %*% A  # This is A
 
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    3    5
[3,]    3    4    7
 

3.12.4  Inverting an n×n matrix*

In the following we will illustrate one frequently applied method for matrix inversion. The method is called Gauss-Seidels method and many computer programs use variants of the method for finding the inverse of an n×n matrix.
Consider the matrix A:
> A <- matrix(c(2, 2, 3, 3, 5, 9, 5, 6, 7), ncol=3)
> A
 
     [,1] [,2] [,3]
[1,]    2    3    5
[2,]    2    5    6
[3,]    3    9    7
 
We want to find the matrix B=A−1. To start, we append to A the identity matrix and call the result AB:
> AB <- cbind(A, diag(c(1, 1, 1)))
> AB
 
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    2    3    5    1    0    0
[2,]    2    5    6    0    1    0
[3,]    3    9    7    0    0    1
 
On a matrix we allow ourselves to do the following three operations (sometimes called elementary operations) as often as we want:
  1. Multiply a row by a (non-zero) constant.
  2. Multiply a row by a (non-zero) constant and add the result to another row.
  3. Interchange two rows.
The aim is to perform such operations on AB in a way such that one ends up with a 3 ×6 matrix which has the identity matrix in the three leftmost columns. The three rightmost columns will then contain B=A−1.
Recall that writing e.g. AB[1,] extracts the entire first row of AB.
Now we extract the three rightmost columns of AB into the matrix B. We claim that B is the inverse of A, and this can be verified by a simple matrix multiplication
> B <- AB[,4:6]
> A%*% B
 
              [,1]         [,2]         [,3]
[1,]  1.000000e+00 3.330669e-16 1.110223e-16
[2,] -4.440892e-16 1.000000e+00 2.220446e-16
[3,] -2.220446e-16 9.992007e-16 1.000000e+00
 
So, apart from rounding errors, the product is the identity matrix, and hence B=A−1. This example illustrates that numerical precision and rounding errors is an important issue when making computer programs.

4  Least squares

Consider the table of pairs (xi,yi) below.
x 1.00 2.00 3.00 4.00 5.00
y 3.70 4.20 4.90 5.70 6.00
A plot of yi against xi is shown in Figure 6.
fig/Regfig01.png
Figure 6: Regression
The plot in Figure 6 suggests an approximately linear relationship between y and x, i.e. 
yi = β0 + β1 xi for i=1,...,5
Writing this in matrix form gives
y=





y1
y2
...
y5












1
x1
1
x2
:
:
1
x5









β0
β1



= Xβ
The first question is: Can we find a vector β such that y=Xβ? The answer is clearly no, because that would require the points to lie exactly on a straight line.
A more modest question is: Can we find a vector β such that Xβ is in a sense "as close to y as possible". The answer is yes. The task is to find β such that the length of the vector
e = y−Xβ
is as small as possible. This leads to the so-called system of normal equations
(XT X) β = XT y
If (XT X) is invertible, the (unique) solution is
^
β
 
= (XT X)−1 XT y
> y
 
[1] 3.7 4.2 4.9 5.7 6.0
 
> X
 
       x
[1,] 1 1
[2,] 1 2
[3,] 1 3
[4,] 1 4
[5,] 1 5
 
> beta.hat <- solve(t(X) %*% X) %*% t(X) %*% y
> beta.hat
 
  [,1]
  3.07
x 0.61
 
The fitted values are
> as.numeric(X %*% beta.hat)
 
[1] 3.68 4.29 4.90 5.51 6.12
 

4.1  Least squares with generalized inverse

Expand the setting above: Let X2 be X with an extra column added: The sum of the columns of X.
> X2 <- cbind(X, rowSums(X))
> X2
 
       x  
[1,] 1 1 2
[2,] 1 2 3
[3,] 1 3 4
[4,] 1 4 5
[5,] 1 5 6
 
Then (X2T X2) is not invertible. There are infinitely many solutions to the normal equations. One is:
> G <- MASS::ginv(t(X2) %*% X2)
> G
 
           [,1]       [,2]        [,3]
[1,]  0.6333333 -0.4333333  0.20000000
[2,] -0.4333333  0.3000000 -0.13333333
[3,]  0.2000000 -0.1333333  0.06666667
 
> beta2.hat <- G %*% t(X2) %*% y
> beta2.hat
 
           [,1]
[1,]  1.8433333
[2,] -0.6166667
[3,]  1.2266667
 
Another solution is (why?)
> beta22.hat <- c(beta.hat, 0)
 
The fitted values are the same:
> as.numeric(X2 %*% beta2.hat)
 
[1] 3.68 4.29 4.90 5.51 6.12
 
> as.numeric(X2 %*% beta22.hat)
 
[1] 3.68 4.29 4.90 5.51 6.12
 

5  A neat little exercise - from a bird's perspective

On a sunny day, two tables are standing in an English country garden. On each table birds of unknown species are sitting having the time of their lives.
A bird from the first table says to those on the second table: "Hi - if one of you come to our table then there will be the same number of us on each table". "Yeah, right", says a bird from the second table, "but if one of you comes to our table, then we will be twice as many on our table as on yours".
Question: How many birds are on each table? More specifically,



File translated from TEX by TTH, version 4.12.