简体   繁体   English

如何在R中创建错误(即混淆)矩阵?

[英]How to create an error (aka confusion) matrix in R?

How can I generate an error (aka confusion) matrix in R? 如何在R中生成错误(即混淆)矩阵?

Let's start with some sample data: 让我们从一些示例数据开始:

a = c(1,2,3,4,5,1,2,2,3,4,2,1,4,5,5,1)
b = c(1,2,3,4,5,1,2,2,3,4,2,2,4,5,2,1)

This is what I am aiming to achieve based on the sample data: 这是我打算根据样本数据实现的目标:

     [,1] [,2] [,3] [,4] [,5] [total]
 [,1]  3    1                    4
 [,2]       4                    4
 [,3]            2               2
 [,4]                3           3
 [,5]       1              3     4
[total]3    6    2   3     3     16   

I can create a simple cross tabulation matrix using table() , however I cannot seem to get the row, column and overall totals. 我可以使用table()创建一个简单的交叉制表矩阵,但是我似乎无法获得行,列和总计。

> table(a,b)
   b
a   1 2 3 4 5
  1 3 1 0 0 0
  2 0 4 0 0 0
  3 0 0 2 0 0
  4 0 0 0 3 0
  5 0 1 0 0 2

To get row, column, and overall totals, just use addmargins : 要获取行,列和总计,只需使用addmargins

> addmargins(table(a, b))
     b
a      1  2  3  4  5 Sum
  1    3  1  0  0  0   4
  2    0  4  0  0  0   4
  3    0  0  2  0  0   2
  4    0  0  0  3  0   3
  5    0  1  0  0  2   3
  Sum  3  6  2  3  2  16

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM