I am following this tutorial on creating a Markov chain for attribution modelling in R.
However when I run it I get a compilation error, a solution was proposed on this page however it is still giving the same error, I have also tried two different workarounds to get past the compilation error, but both result in runtime errors where the output looks nothing like the one in the tutorial. The workarounds are documented at the bottom.
Any help with this would be highly appreciated.
library(dplyr)
library(reshape2)
library(ggplot2)
library(ggthemes)
library(ggrepel)
library(RColorBrewer)
library(ChannelAttribution)
library(markovchain)
##### simple example #####
# creating a data sample
df1 <- data.frame(path = c('c1 > c2 > c3', 'c1', 'c2 > c3'), conv = c(1, 0, 0), conv_null = c(0, 1, 1))
# calculating the model
mod1 <- markov_model(df1,
var_path = 'path',
var_conv = 'conv',
var_null = 'conv_null',
out_more = TRUE)
# extracting the results of attribution
df_res1 <- mod1$result
# extracting a transition matrix
df_trans1 <- mod1$transition_matrix
df_trans1 <- dcast(df_trans1, channel_from ~ channel_to, value.var = 'transition_probability')
### plotting the Markov graph ###
df_trans <- mod1$transition_matrix
# adding dummies in order to plot the graph
df_dummy <- data.frame(channel_from = c('(start)', '(conversion)', '(null)'),
channel_to = c('(start)', '(conversion)', '(null)'),
transition_probability = c(0, 1, 1))
df_trans <- rbind(df_trans, df_dummy)
# ordering channels
df_trans$channel_from <- factor(df_trans$channel_from,
levels = c('(start)', '(conversion)', '(null)', 'c1', 'c2', 'c3'))
df_trans$channel_to <- factor(df_trans$channel_to,
levels = c('(start)', '(conversion)', '(null)', 'c1', 'c2', 'c3'))
df_trans <- dcast(df_trans, channel_from ~ channel_to, value.var = 'transition_probability')
# creating the markovchain object
trans_matrix <- matrix(data = as.matrix(df_trans[, -1]),
nrow = nrow(df_trans[, -1]), ncol = ncol(df_trans[, -1]),
dimnames = list(c(as.character(df_trans[, 1])),
c(colnames(df_trans[, -1]))))
trans_matrix[is.na(trans_matrix)] <- 0
trans_matrix1 <- new("markovchain", transitionMatrix = trans_matrix)
# plotting the graph
plot(trans_matrix1, edge.arrow.size = 0.35)
The error occurs on this line
trans_matrix1 <- new("markovchain", transitionMatrix = trans_matrix)
Error:
Aggregation function missing: defaulting to length
Error in validObject(.Object) :
invalid class “markovchain” object: Error! Rows of transition matrix do not some one
EDIT:
I have tried the following, both give runtime errors:
- user2554330s solution to divide each row by using
trans_matrix <- trans_matrix/rowSums(trans_matrix)
- Manually deleting the last row and column which are both NA, so that all columns equal 1