Proportions in Markov chain do not add up to 1

帅比萌擦擦* 提交于 2020-01-07 01:49:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!