Linear Algebra Using R
Linear Algebra Using R
Sren Hjsgaard
February 15, 2005
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 0vector and 1vector . . . . .
2.8 Orthogonal (perpendicular) vectors
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
1
2
3
3
4
5
5
5
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 Trace . . . . . . . . . . . . . . . . . . . . .
3.12 Determinant . . . . . . . . . . . . . . . . . .
3.13 Some additional rules for matrix operations
3.14 Details on inverse matrices* . . . . . . . . .
3.14.1 Inverse of a 2 2 matrix* . . . . . .
3.14.2 Inverse of diagonal matrices* . . . .
3.14.3 Generalized inverse* . . . . . . . . .
3.14.4 Inverting an n n matrix* . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
6
6
6
7
7
7
8
9
9
10
11
12
12
12
12
12
13
13
13
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
4 Least squares
15
16
Introduction
This note has two goal: 1) Introducing linear algebra (vectors and matrices) and 2)
showing how to work with these concepts in R.
Vectors
2.1
Vectors
a=
a1
a2
..
.
an
where the ai s are numbers, and this vector shall be written a = (a1 , . . . , an ).
A graphical representation of 2vectors is shown Figure 1. Note that row and
a1 = (2,2)
a2 = (1,0.5)
[1] 1 3 2
2.2
Transpose of vectors
Transposing a vector means turning a column (row) vector into a row (column)
vector. The transpose is denoted by > .
Example 1
2
4
1
3
2
3>
5 = [1, 3, 2]
og
2
[1, 3, 2]> = 4
1
3
2
3
5
[1,]
2.3
a1
a2
a = .
..
an
See Figure 2.
Example 2
1
7
7 3 = 21
2
14
> 7 * a
[1]
7 21 14
3
1
a1 = (2,2)
a2 = (1,0.5)
2a2 = (2,1)
a2 = (1,0.5)
2.4
Sum of vectors
a1
b1
a1 + b1
a2 b2 a2 + b2
a+b= . + . =
=b+a
..
.. ..
.
an
bn
an + bn
See Figure 3 and 4. Only vectors of the same dimension can be added.
2
4
1
3
2
3 2
5+4
2
8
9
3 2
5=4
1+2
3+8
2+9
3 2
5=4
3
11
11
3
5
a1 = (2,2)
a1 + a2 = (3,1.5)
a2 = (1,0.5)
Example 3
a1 + ( a2) = (1,2.5)
a1 = (2,2)
a1 + a2 = (3,1.5)
a2 = (1,0.5)
a2 = (1,0.5)
[1]
2.5
3 11 11
[1] 44
2.6
||a|| =
v
u n
uX
aa=t
a2
i
i=1
[1] 3.741657
2.7
The 0-vector (1vector) is a vector with 0 (1) on all entries. The 0vector
(1vector) is frequently written simply as 0 (1) or as 0n (1n ) to emphasize
that its length n.
> rep(0, 5)
[1] 0 0 0 0 0
> rep(1, 5)
[1] 1 1 1 1 1
2.8
Two vectors v1 and v2 are orthogonal if their inner product is zero, written
v1 v2 v1 v2 = 0
> v1 <- c(1, 1)
> v2 <- c(-1, 1)
> sum(v1 * v2)
[1] 0
3
3.1
Matrices
Matrices
A= .
..
..
..
..
.
.
.
ar1 ar2 . . . arc
Note that one can regard A as consisting of c columns vectors put after each other:
A = [a1 : a2 : : ac ]
> A <- matrix(c(1, 3, 2, 2, 8, 9), ncol = 3)
> A
[1,]
[2,]
Note that the numbers 1, 3, 2, 2, 8, 9 are read into the matrix columnby
column. To get the numbers read in rowbyrow do
> A2 <- matrix(c(1, 3, 2, 2, 8, 9), ncol = 3, byrow = T)
> A2
[1,]
[2,]
3.2
Example 4
1
7 3
2
7 14
2
8 = 21 56
14 63
9
> 7 * A
[1,]
[2,]
3.3
Transpose of matrices
Example 5
1
3
2
>
2
1
8
=
2
9
3
8
2
9
Note that if A is an r c matrix then A> is a c r matrix.
> t(A)
[1,]
[2,]
[3,]
[,1] [,2]
1
3
2
2
8
9
3.4
Sum of matrices
1
3
2
2
5
8 + 8
9
3
4
6
6
2 = 11 10
7
5 16
[1,]
[2,]
3.5
Ab= .
..
..
.
.
.
.
.
.
.
ar1
ar2
...
arc
Example 7
1
3
2
2
21
15+28
5
8
= 3 5 + 8 8 = 79
8
9
25+98
82
> A %*% a
[1,]
[2,]
[,1]
23
27
[1,]
[2,]
3.6
Multiplication of matrices
2
8
9
5
8
4
2
1 2
1 2
5
4
= 3 8
: 3 8
8
2
2 9
2 9
15+28
14+22
21
3 4 + 8 2 = 79
= 35+88
25+98
24+92
82
8
28
26
Note that the product AB can only be formed if the number of rows in B and
the number of columns in A are the same. In that case, A and B are said to
be conforme.
In general AB and BA are not identical.
A mnemonic for matrix multiplication is :
1
3
2
2
5
8
8
9
4
2
=
1
3
2
5
8
2 15+28
8 35+88
9 25+98
2
21
1 4 + 2 2 = 79
82
34+82
24+92
8
28
26
[1,]
[2,]
[3,]
3.7
[,1] [,2]
21
8
79
28
82
26
Vectors as matrices
3.8
[1,]
[2,]
[1,]
[2,]
[1,]
[2,]
[3,]
> diag(1, 3)
[1,]
[2,]
[3,]
[1] 1 2 3
> diag(A)
[1] 1 8
10
3.9
Inverse of matrices
1
2
3
4
B=
2
1.5
1 0.5
Now AB = BA = I so B = A1 .
[1,]
[2,]
[,1] [,2]
1
3
2
4
[1,]
[2,]
[,1] [,2]
-2 1.5
1 -0.5
> A %*% B
[1,]
[2,]
[,1] [,2]
1
0
0
1
11
3.10
Example 11 Matrices are closely related to systems of linear equations. Consider the two equations
x1 + 3x2
2x1 + 4x2
= 7
= 10
These two lines are shown in Figure 5 from which it can be seen that the
solution is x1 = 1, x2 = 2.
x1
[1,]
[2,]
[,1]
1
2
12
3.11
Trace
Missing
3.12
Determinant
Missing
3.13
For matrices A, B and C whose dimension match appropriately: the following rules
apply
(A + B)> = A> + B >
(AB)> = B > A>
A(B + C) = AB + AC
AB = AC 6 B = C
In genereal AB 6= BA
AI = IA = A
If is a number then AB = A(B)
3.14
3.14.1
1
ad bc
d b
c
a
under the assumption that abbc 6= 0. The number abbc is called the determinant
of A, sometimes written |A|. If |A| = 0, then A has no inverse.
3.14.2
1
1 1
, ,..., )
a1 a2
an
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 A satisfying that
AA A = A.
For many practical problems it suffice to find a generalized inverse.
13
3.14.4
Inverting an n n matrix*
In the following we will illustrate one frequently applied methopd for matrix inversion. The method is called GaussSeidels method and many computer programs,
including solve() 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,]
[1,]
[2,]
[3,]
14
[1,]
[2,]
[3,]
[1,]
[2,]
[3,]
[1,]
[2,]
[3,]
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 =
A1 . This example illustrates that numerical precision and rounding errors is an
important issue when making computer programs.
15
Least squares
1.00
3.70
2.00
4.20
3.00
4.90
4.00
5.70
5.00
6.00
5.0
4.0
6.0
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
y1
y2
y=
...
y5
1 x1
1 x2
0
= X
..
..
1
.
.
1 x5
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. The solution is
= (X > X)1 X > y
> y
> X
[1,]
[2,]
[3,]
[4,]
[5,]
1
1
1
1
1
x
1
2
3
4
5
16
[,1]
3.07
x 0.61
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,
Write up two equations with two unknowns.
Solve these equations using the methods you have learned from linear algebra.
Simply finding the solution by trialanderror is considered cheating.
17