R过渡图

时间:2015-02-04 19:17:11

标签: r matrix graph transition

我想绘制一个转换矩阵,但我希望每个状态都有2列。

我的矩阵是:

> R
      0   30   60   90 <NA>
0  0.75 0.37 0.17 0.07 0.97
30 0.15 0.40 0.32 0.02 0.02
60 0.00 0.20 0.19 0.05 0.01
90 0.00 0.00 0.03 0.52 0.00
NA 0.10 0.03 0.29 0.35 0.00

因此,从0州开始,75%仍然存在,15%变为30,依此类推。

问题是我不想要以下情节:

library(diagram)
plotmat(R)

enter image description here

相反,我希望每个状态有2列...根据 this answer我需要创建一个10x10的表....有没有其他方法可以做同样的事情而不必创建这样的表?

Mi想法是在不必修改原始表的情况下访问此图:

为此,我使用以下代码转换了原始矩阵:

L<-matrix(nrow = 10, ncol = 10, byrow = TRUE, data = 0)

for (i in 1:(nrow(R))){
    for (j in 1: ncol(R))
{L[i*2,j*2-1]<-R[i,j]
}}

rownames(L)<-c('0','0', '30','30','60','60','90','90','NA','NA')

plotmat(L[1:6,1:6])

enter image description here

由于

1 个答案:

答案 0 :(得分:1)

这是一个可以满足您需求的图表(示例代码,矩阵和产生图的包):Transition Matrix Plot

install.packages("MmgraphR", repos="http://R-Forge.R-project.org")
library(MmgraphR)
##########################################
# Plotting a probability transition matrix
##########################################

trmat<-matrix( c (0.1, 0.05, 0.05, 0.80,
              0.06, 0.02, 0.03, 0.89,
              0.03, 0.01, 0.01, 0.95,
              0, 0, 0, 1), nrow = 4, ncol = 4, byrow = TRUE)

trmatplot(trmat)