3

I'm trying to emulate a random walk/ Markov chain using R. As you can see I've set up a transition matrix, and then I'm trying to run a random walker on this. The thing here is that when the random walker meets an absorbing state (like 2 and 5) does not stop but continue running.

Am I using the wrong functions for something like this or there is somewhere else the problem? Actually what I want to achieve is to print out al the vertices that the walker visited.

library(igraph)

# Produce a transition matrix.
tm <- read.table(row.names=1, header=FALSE, text="
1 0.2 0.3 0.1 0.2 0.1 0.1 
6 0.3 0.2 0.4 0.1 0 0 
3 0 0.2 0.4 0.1 0.2 0.1 
4 0.2 0.1 0.2 0.3 0.1 0.1
5 0 0 0 0 1 0
2 0 0 0 0 0 1")

tm<-as.matrix(tm)
row.names(tm) <- c(1,6,3,4,5,2)
colnames(tm) <- c(1,6,3,4,5,2)

g1 <- graph.adjacency(tm, mode="undirected", weighted=TRUE)

random_walk( graph = g1, start = '4', steps = 100, stuck = "error" )

An output example :

 [1] 4 5 3 4 4 4 3 3 2 2 4 4 2 2 3 4 4 5 5 3 5 5 3 6 3 3 3 4 4 4 6 4 4 4 4 2 2 4 3 6 3 2 2 2 4 1
 [47] 4 3 4 1 1 4 2 3 6 6 6 6 4 3 6 6 6 3 5 5 3 5 5 5 3 1 1 3 2 4 4 2 1 1 1 2 3 1 2 1 1 2 2 1 5 3
 [93] 5 4 4 2 4 3 4 4

And as you can see it doesn't stop, neither at 2 nor at 5.

J. Doe
  • 619
  • 4
  • 16

1 Answers1

0

If you think these states are absorbing, then you are interpreting your adjacency graph as directed, not undirected. Use mode="directed". Then when you plot the graph, you will see the loops better indicate where you can move (notice how there are no longer any lines leading "out" of node 2).

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thank for helping me. It actually changes the behavior of random_walk, but if you tell it to take 100 steps, and at the 4th is an absorbing state, it doesn't stop there but continues with the same number until it reaches the 100. Maybe I have to built a function to remove the extra absorbing states. – J. Doe Feb 07 '18 at 20:10