Explaining the Source Code for Calculating Stationary Distribution in R

In summary, the source code in R uses eigenvectors to find the stationary distribution of a transition matrix.
  • #1
user366312
Gold Member
89
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:

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))
}

I tested the following source code myself:

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
>

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?

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))
}

What was the reason for introduction of a dollar sign and vector keyword?
 
Last edited:
Technology news on Phys.org
  • #2
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
  • #3
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
  • #4
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.
 

1. What is source code in R?

Source code in R refers to the sequence of instructions or commands written in the R programming language that are used to create a program or perform a specific task. It is the set of instructions that the computer interprets and executes to produce the desired output.

2. How do I view the source code of an R function?

To view the source code of an R function, you can use the built-in function "body()", which will display the source code in the console. You can also use the "edit()" function to open the source code in a separate window for easier viewing and editing.

3. What is the purpose of commenting in R source code?

Commenting in R source code is used to add notes or explanations to the code, making it easier for other programmers to understand and modify the code. It also helps to document the code and provide context for future reference.

4. Can I modify the source code of an R package?

Yes, you can modify the source code of an R package, but it is generally not recommended as it can cause conflicts with future updates or other packages that rely on the original code. It is best to create a separate modified version of the package if you need to make changes.

5. How can I improve my understanding of R source code?

To improve your understanding of R source code, you can read through the code line by line and try to understand what each command is doing. You can also refer to documentation or online resources for explanations and examples. Practice writing your own code and analyzing existing code can also help improve your understanding.

Similar threads

  • Programming and Computer Science
Replies
1
Views
751
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
12
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
2K
  • Advanced Physics Homework Help
Replies
1
Views
2K
  • General Math
Replies
11
Views
1K
Back
Top