September 19, 2021

Please can someone help me get this R code to display the output properly?

Please can someone help me get this R code to display the output properly?

This is for a really important project but I cant get it to run, can anyone test this on RStudio and help me understand why its not working?

I am following a tutorial here which seems to be able to run it fine. But when I do it it says there is an error on line 49: the rows in the transition matrix should equal to one.

I have tried two things to make the rows equal one:
1) delete the last row and the last column of the transition matrix

2) manually force the output to equal one.

Whilst both allow the code to run, the output looks nothing like the one in the tutorial! I am struggling to understand the code but I feel if I can at least get it to run and understand why its not working, I'm in a good place to learn.

The code makes and plots a Markov model, I'm familiar with how a Markov model works I just Don't understand why its not plotting! Please try and run it as I have dealt with the odd rude person who has suggested advice without running it themselves and it simply doesn't work. Any help would be really really appreciated!









##### 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[] <- 0

trans_matrix1 <- new("markovchain", transitionMatrix = trans_matrix)

# plotting the graph

plot(trans_matrix1, edge.arrow.size = 0.35)

submitted by /u/Flewizzle
[link] [comments]