title: R Visualization of markov chains | change values in transition matrix by hand
body: <p>I run a markov model in R, primaly to get the markov graph.<a href="https://i.stack.imgur.com/sRFyv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sRFyv.png" alt="enter image description here"></a></p>
  
  <p>I want to exclude all lines with a probability &lt; 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)</p>
  
  <pre><code># creating a data sample
  df1 &lt;- data.frame(path = c('c1 &gt; c2 &gt; c3', 'c1', 'c2 &gt; c3'), conv = c(1, 0, 0), conv_null = c(0, 1, 1)) # original
  df1
  
  # calculating the models
  mod1 &lt;- markov_model(df1,
                       var_path = 'path',
                       var_conv = 'conv',
                       var_null = 'conv_null',
                       out_more = TRUE)          
  mod1                 
  
  
  # extracting the results of attribution:
  df_res1 &lt;- mod1$result
  df_res1
  
  # extracting a transition matrix: 
  df_trans1 &lt;- mod1$transition_matrix                
  df_trans1
  
  df_trans1 &lt;- dcast(df_trans1, channel_from ~ channel_to, value.var = 'transition_probability')
  df_trans1
  
  
  
  ### plotting the Markov graph ###
  df_trans &lt;- mod1$transition_matrix
  df_trans
  
  # adding dummies in order to plot the graph
  df_dummy &lt;- 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 &lt;- rbind(df_trans, df_dummy)
  df_trans
  
  # ordering channels
  df_trans$channel_from &lt;- factor(df_trans$channel_from,
                                  levels = c('(start)', '(conversion)', '(null)',
                                             'c1',
                                             'c2',
                                             'c3'
                                             ))
  
  df_trans$channel_from
  
  df_trans$channel_to &lt;- factor(df_trans$channel_to,
                                levels = c('(start)', '(conversion)', '(null)', 
                                           'c1',
                                           'c2',
                                           'c3'
                                ))
  
  df_trans$channel_to
  
  df_trans &lt;- dcast(df_trans, channel_from ~ channel_to, value.var = 'transition_probability')
  df_trans
  
  
  # creating the markovchain object
  trans_matrix &lt;- 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)] &lt;- 0
  trans_matrix
  
  
  ####################### I want to delete transition-propabilities &lt; 0.4 from markov graph by setting these values to 0.
  trans_matrix[trans_matrix &lt; 0.4] &lt;- 0 # 
  
  
  ####################### After doing this, the following querie gives me an error: Error! Row sums not equal to one check positions: 1
  trans_matrix1 &lt;- 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)
  </code></pre>