Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Markov Chain in R (visualization)

i'd like to do a simple first order markov chain in R. I know there are packages like MCMC, but couldn't found one to display it graphically. Is this even possible? It would be nice if given a transition matrix and an initial state, one can visually see the path through the markov chain (maybe i've to do this by hand...).

Thanks.

like image 268
user1028531 Avatar asked Nov 06 '11 11:11

user1028531


1 Answers

This shows how to apply a random transition matrix to a particular starting vector: c(1,0,0,0):

set.seed(123)
tmat <- matrix(rnorm(16)^2,ncol=4) 
   # need entries to be positive, could have used abs()
tmat <- tmat/rowSums(tmat) # need the rows to sum to 1
tmat
            [,1]       [,2]       [,3]        [,4]
[1,] 0.326123580 0.01735335 0.48977444 0.166748625
[2,] 0.016529424 0.91768404 0.06196453 0.003822008
[3,] 0.546050789 0.04774713 0.33676288 0.069439199
[4,] 0.001008839 0.32476060 0.02627217 0.647958394
require(expm)   # for the %^% function
matplot( t(         # need to transpose to get arguments to matplot correctly
       sapply(1:20, function(x) matrix(c(1,0,0,0), ncol=4) %*% (tmat %^% x) ) ) )

You can see it approaching equilibrium: enter image description here

like image 120
IRTFM Avatar answered Sep 21 '22 22:09

IRTFM