- first create a transition matrix
tm
, i.e. the rows represent start state the columns represent end state, so tm[1,1]
is the number of subjects state in state 1 and end in state 1, so tm[1,3]
is the number of subjects state in state 1 and end in state 3
tm <- matrix(NA,nrow=3,ncol=3)
tm[1,] <- 200*c(.5,.25,.25)
tm[2,] <- 800*c(.75,.1,.15)
tm[3,] <- 600*c(0,.2,.8)
tm
[,1] [,2] [,3]
[1,] 100 50 50
[2,] 600 80 120
[3,] 0 120 480
- a more convenient way is to tabulate start state and end state
xx <- data.frame(id=1:1000,
start.state=sample(1:4,size=1000,replace=T,prob=c(0.2,0.5,0.1,0.2)),
end.state=sample(1:4,size=1000,replace=T,prob=c(0.1,0.3,0.2,0.4)))
tm2 <- table(xx$start.state,xx$end.state)
tm2
1 2 3 4
1 24 67 37 89
2 45 143 87 196
3 13 30 24 44
4 25 59 39 78
- than a simple call to
transitionPlot()
does the magic
box_txt
provides the states' names
plot.new() ## call plot.new because transitionPlot does not
transitionPlot(tm,box_txt = 1:3)
plot.new() ## call plot.new because transitionPlot does not
transitionPlot(tm2,box_txt = paste("state",1:4))
- of course you can change the appearance of the plot, e.g. color of the boxes, appearance of the arrows
- in a similar way text color can be changed
plot.new() ## call plot.new because transitionPlot does not
transitionPlot(tm2,box_txt = c(paste("state",1:4)),
type_of_arrow="gradient",
fill_start_box=c("midnightblue","darkred","darkgreen","maroon4"),
fill_end_box=c("midnightblue","darkred","darkgreen","maroon4"))
No comments :
Post a Comment