转移矩阵的马尔可夫图

时间:2015-09-23 07:20:59

标签: r igraph markov-chains

我正在尝试使用包markovchain创建Markov图。代码如下

library(ChannelAttribution)
library(markovchain)
data(PathData)
m<-markov_model(Dy, "channel_path", "total_conversions", "total_conversion_value",out_more = 1)

transition_matrix<-m$transition_matrix
trans_conversion<-data.frame(channel_from="(conversion)",channel_to=unique(as.vector(transition_matrix$channel_to)),transition_probability=0)
trans_start<-data.frame(channel_from="(start)",channel_to=("start"),transition_probability=0)

final_transition<-rbind(rbind(transition_matrix,trans_conversion),trans_start)

transition_frame<-reshape(transition_matrix,direction = "wide", idvar="channel_from", timevar="channel_to")
transition_frame[is.na(transition_frame)]<-0
colnames(transition_frame)<-c("channel_from",as.vector(transition_frame$channel_from)[-1],"(start)")


finalmatrix<-as.matrix(transition_frame, dimnames = list(transition_frame$channel_from, colnames(transition_frame)[-1]))

plot(finalmatrix)

但是我一直收到以下错误

> plot(finalmatrix)
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

非常感谢这方面的任何帮助。

1 个答案:

答案 0 :(得分:2)

您需要在绘图前创建markovchain对象。我在下面找到了https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.pdf

下面的例子
weatherStates <- c("sunny", "cloudy", "rain")
byRow <- TRUE
weatherMatrix <- matrix(data = c(0.70, 0.2, 0.1,
                                 0.3, 0.4, 0.3,
                                 0.2, 0.45, 0.35), 
                        byrow = byRow, nrow = 3,
                        dimnames = list(weatherStates, weatherStates))
mcWeather <- new("markovchain", states = weatherStates, byrow = byRow,
transitionMatrix = weatherMatrix, name = "Weather")