- #1

user366312

Gold Member

- 71

- 3

## Summary:

- Kindly explain the source code in R which meant for calculating stationary distribution of a transition matrix.

## Main Question or Discussion Point

**Summary:**Kindly explain the source code in R which meant for calculating stationary distribution of a transition matrix.

I am trying to understand the following source code meant for finding stationary distribution of a matrix:

R code:

```
### Stationary distribution of discrete-time Markov chain
### (uses eigenvectors)
stationary <- function(mat)
{
x = eigen(t(mat))$vectors[,1]
as.double(x/sum(x))
}
```

R code:

```
> rm(list=ls())
>
> P <- matrix(c(0.66, 0.34,
+ 0.66, 0.34), nrow=2, ncol=2, byrow = TRUE)
>
> x <- eigen(t(P))
> x
$values
[1] 1 0
$vectors
[,1] [,2]
[1,] 0.8889746 -0.7071068
[2,] 0.4579566 0.7071068
> y <- x$vectors[,1]
> y
[1] 0.8889746 0.4579566
>
```

Code:

`y <- x$vectors[,1]`

**Why wasn't that simply written like the following?**

R code:

```
### Stationary distribution of discrete-time Markov chain
### (uses eigenvectors)
stationary <- function(mat)
{
x = eigen(t(mat))
y = x[,1]
as.double(y/sum(y))
}
```

**dollar sign**and

**vector keyword**?

Last edited: