R語言-生成頻數(shù)表和列聯(lián)表crosstable函數(shù)介紹
列聯(lián)表crosstable
列聯(lián)表不僅可以用來做簡單的描述性統(tǒng)計,還可以在機器學習中用來比較識別正確率,F(xiàn)PR,TPR等等數(shù)據(jù),以便我們比較不同的ML模型 or 調參。
2x2列聯(lián)表一般長下面這樣:
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ù)來做。
舉例
## 使用knn模型做預測 knn_pred_1 = knn(train_cancer[,2:4], test_cancer[,2:4], train_cancer$diagnosis, k=1) ## 創(chuàng)建列聯(lián)表看預測效果 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函數(shù)中,prop.chisq 這個argument默認是true,但實際上大部分使用場景不需要這個卡方概率,所以可以單獨在函數(shù)中設置prop.chisq = FALSE
函數(shù)語法:
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ù)說明:
x,y:列聯(lián)表的兩個特征向量
digit:指定結果小數(shù)位數(shù)
prop.r:行比例是否加入
prop.c:列比例是否加入
prop.t:表比例是否加入
prop.chisq:每個單元的卡方值是否加入
chisq:卡方檢驗結果是否加入
頻數(shù)表
頻數(shù)表給出了各個特征值出現(xiàn)的頻數(shù),下面使用R自帶的數(shù)據(jù)集“CO2”舉例
head(CO2) #得到“conc”特征的頻數(shù)表 table(CO2$conc)
結果:
95 175 250 350 500 675 1000
12 12 12 12 12 12 12
補充: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
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
R語言 實現(xiàn)將factor轉換成numeric方法
這篇文章主要介紹了R語言 實現(xiàn)將factor轉換成numeric方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03R語言之xlsx包讀寫Excel數(shù)據(jù)的操作
這篇文章主要介紹了R語言之xlsx包讀寫Excel數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04R語言利用loess如何去除某個變量對數(shù)據(jù)的影響詳解
這篇文章主要給大家介紹了關于R語言利用loess去除某個變量對數(shù)據(jù)的影響的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-11-11