欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

R語(yǔ)言-生成頻數(shù)表和列聯(lián)表crosstable函數(shù)介紹

 更新時(shí)間:2021年04月20日 15:13:18   作者:Yann_YU  
這篇文章主要介紹了R語(yǔ)言-生成頻數(shù)表和列聯(lián)表crosstable函數(shù)介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

列聯(lián)表crosstable

列聯(lián)表不僅可以用來(lái)做簡(jiǎn)單的描述性統(tǒng)計(jì),還可以在機(jī)器學(xué)習(xí)中用來(lái)比較識(shí)別正確率,F(xiàn)PR,TPR等等數(shù)據(jù),以便我們比較不同的ML模型 or 調(diào)參。

2x2列聯(lián)表一般長(zhǎng)下面這樣:

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

創(chuàng)建列聯(lián)表crosstable

推薦使用R中“gmodels”包的CrossTable()函數(shù)來(lái)做。

舉例

## 使用knn模型做預(yù)測(cè)
knn_pred_1 = knn(train_cancer[,2:4], test_cancer[,2:4], train_cancer$diagnosis, k=1)
## 創(chuàng)建列聯(lián)表看預(yù)測(cè)效果
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 |           | 
-------------|-----------|-----------|-----------|

注意事項(xiàng)

在crosstable函數(shù)中,prop.chisq 這個(gè)argument默認(rèn)是true,但實(shí)際上大部分使用場(chǎng)景不需要這個(gè)卡方概率,所以可以單獨(dú)在函數(shù)中設(shè)置prop.chisq = FALSE

函數(shù)語(yǔ)法:

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, ...)

參數(shù)說(shuō)明:

x,y:列聯(lián)表的兩個(gè)特征向量

digit:指定結(jié)果小數(shù)位數(shù)

prop.r:行比例是否加入

prop.c:列比例是否加入

prop.t:表比例是否加入

prop.chisq:每個(gè)單元的卡方值是否加入

chisq:卡方檢驗(yàn)結(jié)果是否加入

頻數(shù)表

頻數(shù)表給出了各個(gè)特征值出現(xiàn)的頻數(shù),下面使用R自帶的數(shù)據(jù)集“CO2”舉例

head(CO2)
#得到“conc”特征的頻數(shù)表
table(CO2$conc)

結(jié)果:

95 175 250 350 500 675 1000

12 12 12 12 12 12 12

補(bǔ)充:R--生成各種列聯(lián)表

看代碼吧~

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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

最新評(píng)論