# Eigenvalues and Eigenvectors in R

Calculating eigenvalues and eigenvectors for age- and stage-structured populations is made very simple by computers. Here I show how to calculate the eigenvalues and eigenvectors for the right whale population example from class.

The first thing we need to do is to define the transition matrix. Using the function **c()**, let’s put all of the entries in the transition matrix into one long vector - since there are four stages, my vector will have 16 entries in it. We will put the entries in row-wise, so the first four entries will be the top row of the transition matrix, the next four entries will be the second row, and so on. Note that I have put the entries on separate lines to make it clear how they fill in the matrix, but this is not required.

```
entries <- c( 0, 0, 0, 0.45,
0.92, 0.86, 0, 0,
0, 0.08, 0.8, 0.83,
0, 0.02, 0.19, 0)
```

Now I want to put those entries into a matrix. I can use the function **matrix()** to do this. I am going to specify two options to this function: **nrow**, which tells **matrix** how many rows the function will have, and **byrow**, which tells **matrix** to put the first four entries into the first row, rather than the first column.

```
right_whale <- matrix(entries, nrow=4, byrow=TRUE)
right_whale
```

```
## [,1] [,2] [,3] [,4]
## [1,] 0.00 0.00 0.00 0.45
## [2,] 0.92 0.86 0.00 0.00
## [3,] 0.00 0.08 0.80 0.83
## [4,] 0.00 0.02 0.19 0.00
```

Now, to calculate the eigenvalues and eigenvectors, I can simply use the function **eigen()**:

```
## Calculate the eigenvalues and right eigenvectors
whale_eigen <- eigen(right_whale)
whale_eigen
```

```
## $values
## [1] 1.00912 0.80797 -0.15446 -0.00263
##
## $vectors
## [,1] [,2] [,3] [,4]
## [1,] -0.07649 -0.04956 -0.6933 0.681980
## [2,] -0.47194 0.87640 0.6287 -0.727336
## [3,] -0.86139 -0.47069 -0.2596 0.076617
## [4,] -0.17154 -0.08899 0.2380 -0.003986
```

Note that **whale_eigen** is a list with two elements (denoted by the “$”), **values** and **vectors**. As you might expect, **whale_eigen$values** are the four eigenvalues, stored as a vector. The leading eigenvalue is the first entry in the vector. **whale_eigen$vectors** gives the corresponding right eigenvectors, stored as a matrix. The right eigenvector associated with the dominant eigenvalue is stored in the first column of the matrix.

So, for the right whale population, the leading eigenvalue is

`whale_eigen$values[1]`

`## [1] 1.009`

and the corresponding stable stage distribution (after rescaling so the elements of the vector sum to 1) is

```
## dividing by the sum of the elements rescales so the entries sum to 1
stable_stage <- whale_eigen$vectors[,1]/sum(whale_eigen$vectors[,1])
stable_stage
```

`## [1] 0.04837 0.29844 0.54471 0.10848`

To find the reproductive values, we need to find the left eigenvectors. It turns out that the left eigenvectors of any matrix are equal to the right eigenvectors of the transpose matrix. So, if we take the transpose and use **eigen()**, we can easily find the left eigenvector, and then the reproductive values:

```
## use t() to compute the transpose
t(right_whale)
```

```
## [,1] [,2] [,3] [,4]
## [1,] 0.00 0.92 0.00 0.00
## [2,] 0.00 0.86 0.08 0.02
## [3,] 0.00 0.00 0.80 0.19
## [4,] 0.45 0.00 0.83 0.00
```

```
## calculate the eigenvalues and eigenvectors for the transpose
eigen(t(right_whale)) # note that eigenvalues are the same
```

```
## $values
## [1] 1.00912 0.80797 -0.15446 -0.00263
##
## $vectors
## [,1] [,2] [,3] [,4]
## [1,] 0.3561 -0.6915 0.023457 0.386598
## [2,] 0.3905 -0.6073 -0.003938 -0.001105
## [3,] 0.5709 0.3909 -0.195180 -0.212445
## [4,] 0.6283 0.0164 0.980479 0.897445
```

```
## left eigenvector
eigen(t(right_whale))$vectors[,1]
```

`## [1] 0.3561 0.3905 0.5709 0.6283`

```
## divide by the first non-zero entry to calculate *relative* reproductive values
repro_value <- eigen(t(right_whale))$vectors[,1]/eigen(t(right_whale))$vectors[1,1]
repro_value
```

`## [1] 1.000 1.097 1.603 1.765`

To find the mean reproductive value, we need to take the inner product of the reproductive values and the stable stage distribution.

```
## inner products in R are calculated using %*%
repro_value %*% stable_stage
```

```
## [,1]
## [1,] 1.441
```