3

I run a markov model in R, primaly to get the markov graph.enter image description here

I want to exclude all lines with a probability < 0,4 from transistion matrix (In this case the line from start to c2 should be deleted.). I tried this by setting these values to 0. But changing values in transition matrix results in an Error: Please see below: I marked the position of interrest with "#######################" (line 76)

# 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)) # original
df1

# calculating the models
mod1 <- markov_model(df1,
                     var_path = 'path',
                     var_conv = 'conv',
                     var_null = 'conv_null',
                     out_more = TRUE)          
mod1                 


# extracting the results of attribution:
df_res1 <- mod1$result
df_res1

# extracting a transition matrix: 
df_trans1 <- mod1$transition_matrix                
df_trans1

df_trans1 <- dcast(df_trans1, channel_from ~ channel_to, value.var = 'transition_probability')
df_trans1



### plotting the Markov graph ###
df_trans <- mod1$transition_matrix
df_trans

# 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
                         )) # die Übergangswarhscheinlichkeit von zu sich selber eintragen
df_dummy

df_trans <- rbind(df_trans, df_dummy)
df_trans

# ordering channels
df_trans$channel_from <- factor(df_trans$channel_from,
                                levels = c('(start)', '(conversion)', '(null)',
                                           'c1',
                                           'c2',
                                           'c3'
                                           ))

df_trans$channel_from

df_trans$channel_to <- factor(df_trans$channel_to,
                              levels = c('(start)', '(conversion)', '(null)', 
                                         'c1',
                                         'c2',
                                         'c3'
                              ))

df_trans$channel_to

df_trans <- dcast(df_trans, channel_from ~ channel_to, value.var = 'transition_probability')
df_trans


# 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_matrix


####################### I want to delete transition-propabilities < 0.4 from markov graph by setting these values to 0.
trans_matrix[trans_matrix < 0.4] <- 0 # 


####################### After doing this, the following querie gives me an error: Error! Row sums not equal to one check positions: 1
trans_matrix1 <- new("markovchain", transitionMatrix = trans_matrix)  
trans_matrix1

# plotting the graph
plot(trans_matrix1, edge.arrow.size = 0.5, size = 100, cex.main = 0.11, cex.lab = 0.5, cex.axis = 0.5)
flozygy
  • 83
  • 2
  • 8
  • without knowing the package, your error implies that since you reduced anything less than 0.4 to 0, one or more row sum is no longer one. you will need to rescale appropriately. try dividing each row by its sum – Chris Littler Aug 14 '18 at 15:49

1 Answers1

3

The transition matrix is no longer a transition matrix if you set some positive entries to 0, because the row sums must equal one. So new("markovchain", ....) does not work with such a matrix.

But if you want the plot only, this is possible by modifying the slot transitionMatrix:

library(markovchain)
tm <- rbind(c(0.3, 0.5, 0.2), c(0.1, 0.1, 0.8), c(0.6, 0.2, 0.2))
states <- c("a", "b", "c")
mc <- new("markovchain", states=states, transitionMatrix=tm, name="X")

tm[tm<0.4] <- 0
dimnames(tm) <- list(states, states)
mc@transitionMatrix <- tm

plot(mc)

enter image description here

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225