问题
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:
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
Any thoughts?
回答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
回答2:
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.
来源:https://stackoverflow.com/questions/36581441/proportions-in-markov-chain-do-not-add-up-to-1