列联表crosstable

列联表不仅可以用来做简单的描述性统计,还可以在机器学习中用来比较识别正确率,fpr,tpr等等数据,以便我们比较不同的ml模型 or 调参。

2×2列联表一般长下面这样:

total observations in table:  143 
 
             | test_cancer$diagnosis 
   lda.class |         0 |         1 | row total | 
-------------|-----------|-----------|-----------|
           0 |        82 |        11 |        93 | 
             |     0.882 |     0.118 |     0.650 | 
             |     0.988 |     0.183 |           | 
             |     0.573 |     0.077 |           | 
-------------|-----------|-----------|-----------|
           1 |         1 |        49 |        50 | 
             |     0.020 |     0.980 |     0.350 | 
             |     0.012 |     0.817 |           | 
             |     0.007 |     0.343 |           | 
-------------|-----------|-----------|-----------|
column total |        83 |        60 |       143 | 
             |     0.580 |     0.420 |           | 
-------------|-----------|-----------|-----------|

创建列联表crosstable

推荐使用r中“gmodels”包的crosstable()函数来做。

举例

## 使用knn模型做预测
knn_pred_1 = knn(train_cancer[,2:4], test_cancer[,2:4], train_cancer$diagnosis, k=1)
## 创建列联表看预测效果
crosstable(x = knn_pred_1, y = test_cancer$diagnosis, prop.chisq = false)
>
   cell contents
|-------------------------|
|                       n |
|           n / row total |
|           n / col total |
|         n / table total |
|-------------------------|
 
total observations in table:  143 
 
             | test_cancer$diagnosis 
  knn_pred_1 |         0 |         1 | row total | 
-------------|-----------|-----------|-----------|
           0 |        77 |         8 |        85 | 
             |     0.906 |     0.094 |     0.594 | 
             |     0.928 |     0.133 |           | 
             |     0.538 |     0.056 |           | 
-------------|-----------|-----------|-----------|
           1 |         6 |        52 |        58 | 
             |     0.103 |     0.897 |     0.406 | 
             |     0.072 |     0.867 |           | 
             |     0.042 |     0.364 |           | 
-------------|-----------|-----------|-----------|
column total |        83 |        60 |       143 | 
             |     0.580 |     0.420 |           | 
-------------|-----------|-----------|-----------|

注意事项

在crosstable函数中,prop.chisq 这个argument默认是true,但实际上大部分使用场景不需要这个卡方概率,所以可以单独在函数中设置prop.chisq = false

函数语法:

crosstable(x, y, digits=3, max.width = 5, expected=false, prop.r=true, prop.c=true,
           prop.t=true, prop.chisq=true, chisq = false, fisher=false, mcnemar=false,
           resid=false, sresid=false, asresid=false,
           missing.include=false,
           format=c("sas","spss"), dnn = null, ...)

参数说明:

x,y:列联表的两个特征向量

digit:指定结果小数位数

prop.r:行比例是否加入

prop.c:列比例是否加入

prop.t:表比例是否加入

prop.chisq:每个单元的卡方值是否加入

chisq:卡方检验结果是否加入

频数表

频数表给出了各个特征值出现的频数,下面使用r自带的数据集“co2”举例

head(co2)
#得到“conc”特征的频数表
table(co2$conc)

结果:

95 175 250 350 500 675 1000

12 12 12 12 12 12 12

补充:r–生成各种列联表

看代码吧~

library(vcd)
head(arthritis)
 
table(arthritis$treatment,arthritis$improved)
with(arthritis,table(treatment,improved))
mytable <- xtabs(~treatment+improved,data = arthritis)
with(arthritis,xtabs(~treatment+improved,data = arthritis))
 
margin.table(mytable,2) # sum by row
prop.table(mytable,2)  #proportion by column
prop.table(mytable)  #proportion by total
 
addmargins(mytable)
addmargins(mytable,1)
addmargins(prop.table(mytable,2),1)
  
library(gmodels)
crosstable(arthritis$treatment,arthritis$improved) ##sas format

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。如有错误或未考虑完全的地方,望不吝赐教。