Explaining the Source Code for Calculating Stationary Distribution in R

AI Thread Summary
The discussion centers around understanding a source code snippet in R designed to calculate the stationary distribution of a transition matrix using eigenvectors. The original code utilizes the eigen function to extract the first column of eigenvectors, which represents the stationary distribution. A participant questions why the code uses the dollar sign and vector keyword to access the first column instead of a simpler approach. It is clarified that the dollar sign is necessary because the eigen function returns a list containing both values and vectors, and proper syntax is essential for accessing these components. The importance of consulting R documentation when encountering unfamiliar functions is emphasized, along with suggestions for further learning resources, including online tutorials and the Rosetta Code website.
user366312
Gold Member
Messages
88
Reaction score
3
TL;DR Summary
Kindly explain the source code in R which meant for calculating stationary distribution of a transition matrix.
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:

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

I tested the following source code myself:

[CODE title="R code" highlight="6,16"]> 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]

looks like the command
Code:
y <- x$vectors[,1]
is selecting the 1st column of the matrix.

Why wasn't that simply written like the following?

[CODE title="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))
}
[/CODE]

What was the reason for introduction of a dollar sign and vector keyword?
 
Last edited:
Technology news on Phys.org
My suggestion is to begin reading up on R or checking out some online tutorials

https://www.tutorialspoint.com/r/index.htm
@FactChecker suggestion in your other thread is a great technique for teasing out what these constructs mean.

There’s also the Rosetta Code website with examples of R doing various tasks. The code may or may not be the best implementation of any task but they are instructive

http://rosettacode.org/wiki/Category:R
And lastly the R reference on the eig function

https://www.rdocumentation.org/packages/pracma/versions/1.9.9/topics/eig
 
  • Like
Likes FactChecker
user366312 said:
What was the reason for introduction of a dollar sign and vector keyword?
That is what the documentation of the eigen function says that it returns. (see R documentation of eigen function)
I don't think that your code will work. When you encounter an unfamiliar function, it is always good to check the documentation.
 
  • Like
Likes jedishrfu
FactChecker said:
That is what the documentation of the eigen function says that it returns. (see R documentation of eigen function)
I don't think that your code will work. When you encounter an unfamiliar function, it is always good to check the documentation.

Got it thanks.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top