title: Calculating True/False Positive and True/False Negative Values from Matrix in R
body: <p>For subsequent discussion, I am using the <strong>keras</strong> package in R.</p>
  
  <p>Given a confusion matrix created as follows:</p>
  
  <pre><code># Get confusion matrix for predictions
  classes &lt;- model %&gt;% predict_classes(test, batch_size=128)
  ct &lt;- table(test.target, classes)
  cm &lt;- as.matrix(ct)
  </code></pre>
  
  <p>For which <code>ct</code> gives the following confusion matrix:</p>
  
  <pre><code>           classes
  test.target   0   1   2
            0 805 192   0
            1  74 862   0
            2   2   0 477
  </code></pre>
  
  <p>How can I calculate the True Positive (TP), False Positive (FP), True Negative (TN) and False Negative (FN) values?</p>
  
  <p>For clarification, I calculate the True Positive (TP) value by getting the diagonal of the matrix:</p>
  
  <p><code>tp &lt;- diag(cm)</code></p>
  
  <p>However, my attempt of calculating the FP value gives me negative numbers (which I guess cant be right, correct?):</p>
  
  <pre><code># Get false positive rates (FP)
  fp &lt;- c()
  for(i in seq_len(ncol(ct))) {
    fp &lt;- append(fp, sum(cm[,i])-cm[i,i])
  }
  </code></pre>
  
  <p><strong>EDIT</strong>: The <code>dput(cm)</code> is as follows:</p>
  
  <pre><code>structure(c(805L, 74L, 2L, 192L, 862L, 0L, 0L, 0L, 477L), .Dim = c(3L, 
  3L), .Dimnames = list(test.target = c("0", "1", "2"), classes = c("0", 
  "1", "2")), class = "table")
  </code></pre>