Section 2: Eigendecomposition.: 1 Selecting Elements of Matrices
Section 2: Eigendecomposition.: 1 Selecting Elements of Matrices
Matrix Eigendecomposition
1 2
2 5
>
Now enter the following command:
> res=eigen(A)
> res
The above assigns the result of the eigendecomposition of A to the name res. Typing
res then calls the result up. Note that it would have been fine to simply enter
eigen(A) to see the result of the function, but assigning it with a name makes it
easier to use in the future. If the code has been correctly entered you should have
been returned with the following:
$values
[1] 5.8284271 0.1715729
$vectors
[,1]
[,2]
[1,] 0.3826834 0.9238795
[2,] 0.9238795 -0.3826834
The numbers following the term $values are the eigenvalues of the matrix A (given in
decreasing order), whilst the columns of the matrix returned after the term $vectors
are the corresponding orthonormal eigenvectors. That is to say the output informs
us that the matrix A has one eigenvalue of 5.83 that corresponds to orthonormal
eigenvector (0.38, 0.92), and another smaller eigenvalue of 0.17 that corresponds to
orthonormal eigenvector (0.92, 0.38).
To request the eigenvalues and eigenvectors of the matrix A directly, now enter the
following:
> res$values
> res$vectors
Task: Write an R code that directly returns only the first eigenvalue of A:
>
Task: Write an R code that directly returns only the second eigenvector of A:
>
2
Matrix Multiplication
In order to double check that the results are the appropriate eigenvalues and eigenvectors of the matrix A we need to know how matrix multiplication works in R.
Remembering that the * character is used for ordinary multiplication in R, the sequence %*% replaces this for when multiplying matrices.
Enter the following:
> A%*%res$vector[,1]
Now check the results agree with the following:
> res$value[1]*res$vector[,1]
Task: Check that the second eigenvalue and eigenvector are indeed correct:
>
>
Sometimes when multiplying a vector by a matrix we need to take the transpose
of one or more of the terms in order to ensure that the matrix or vector dimensions permit multiplication. In R this is performed with the t function. Enter the
following:
> a=c(1,2)
> a
This creates a generic vector in R that will automatically be considered as either a
row vector or column vector as appropriate:
> a%*%A
> A%*%a
Now enter the following:
> a=matrix(c(1,2),nrow=1,ncol=2)
> a
This has told R to specifically treat the vector a as a row vector, hence only a%*%A
will now work. Now try:
> t(a)
> A%*%t(a)
3
You will see the transpose function t changes the row vector a into the column
vector t(a).
Task: The cranial length and cranial breadth of 35 female frogs have expected value
= (23, 24)T and covariance matrix:
17.7 20.3
20.3 24.4
What are the eigenvalues and eigenvectors of the covariance matrix?
>
>
>
Plots
As a final task today we will examine how to create some simple plots in R. Try
and understand how the following code works (remember to use help files if you are
uncertain):
> x=sample(c(-1000:1000), size=200, replace=FALSE)
> x=sort(x)
> y=x^2+3*x+1
> plot(x,y, type="l", col="red")
Task: Use R to produce a plot of the curve y = x3 + x2 2x + 4.
>
>
>
>