Oposum
Oposum

Reputation: 1243

Proportions in Markov chain do not add up to 1

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?

Upvotes: 0

Views: 334

Answers (2)

adaien
adaien

Reputation: 1932

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

Upvotes: 1

stark
stark

Reputation: 13189

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.

Upvotes: 1

Related Questions