0

I have following two-state Markov chain:

pre<-cbind(c(rep("rain",100),rep("sun",100),rep("rain",100)))
post<-cbind(c(rep("rain",50),rep("sun",70),rep("rain",100),rep("sun",80)))
df<-cbind(pre,post)
df<-as.data.frame(df)
colnames(df)<-c("pre","post")

states<-c("rain","sun")

probsCase<-function(i,j){
sum(as.character(df$pre)==states[i] & as.character(df$post)==states[j])/sum(as.character(df$pre)==states[i])
}

transitionMatrix<-outer(1:2,1:2,Vectorize(probsCase))
colnames(transitionMatrix)<-states
rownames(transitionMatrix)<-states

library(diagram)
plotmat(transitionMatrix,relsize=0.75)

Which produces the following plot:

enter image description here

It seems to me that the arrows between "sun" and "rain" should be pointing the opposite directions, otherwise the respective proportions will not add up to 1.

For comparison, you can look at this similar plot online where the proportions from do add up to 1

enter image description here

Any thoughts?

Oposum
  • 1,155
  • 3
  • 22
  • 38

2 Answers2

1

The numbers on the top diagram indicate the amounts entering the target state. The total of all lines entering the state add to 1. On the bottom plot, the total of all lines leaving the prior state add to 1. The values may be calculated either way, but the plots should be labeled to show what is being displayed.

stark
  • 12,615
  • 3
  • 33
  • 50
  • thank you, is there away I can alter my code to change from "entering the state" to "leaving the state", to make it look like the second plot? – Oposum Apr 12 '16 at 19:39
1

You just need to plot the transpose matrix to have what you need.

So this:

plotmat(t(transitionMatrix),relsize=0.75)

will play the trick

adaien
  • 1,932
  • 1
  • 12
  • 26