简体   繁体   中英

Markov Graph from transition matrix

I am trying to create a Markov Graph using the package markovchain. The code is as follows

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)

However I keep getting the following error

> 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

Any help in this regard would be greatly appreciated.

You need to create a markovchain object before plotting. I found the example from below here 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM