R語言-生成頻數(shù)表和列聯(lián)表crosstable函數(shù)介紹
列聯(lián)表crosstable
列聯(lián)表不僅可以用來做簡(jiǎn)單的描述性統(tǒng)計(jì),還可以在機(jī)器學(xué)習(xí)中用來比較識(shí)別正確率,F(xiàn)PR,TPR等等數(shù)據(jù),以便我們比較不同的ML模型 or 調(diào)參。
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模型做預(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ù)語法:
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)表的兩個(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)文章
R語言 實(shí)現(xiàn)將factor轉(zhuǎn)換成numeric方法
這篇文章主要介紹了R語言 實(shí)現(xiàn)將factor轉(zhuǎn)換成numeric方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03R語言繪圖樣式設(shè)置操作(符號(hào),線條,顏色,文本屬性)
這篇文章主要介紹了R語言繪圖樣式設(shè)置操作(符號(hào),線條,顏色,文本屬性),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03R語言之xlsx包讀寫Excel數(shù)據(jù)的操作
這篇文章主要介紹了R語言之xlsx包讀寫Excel數(shù)據(jù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04R語言實(shí)現(xiàn)PCA主成分分析圖的示例代碼
主成分分析(Principal?Component?Analysis,PCA)是一種無監(jiān)督的數(shù)據(jù)降維方法,通過主成分分析可以盡可能保留下具備區(qū)分性的低維數(shù)據(jù)特征。本文將用R語言實(shí)現(xiàn)PCA主成分分析圖,需要的可以參考一下2022-04-04R語言利用loess如何去除某個(gè)變量對(duì)數(shù)據(jù)的影響詳解
這篇文章主要給大家介紹了關(guān)于R語言利用loess去除某個(gè)變量對(duì)數(shù)據(jù)的影響的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11R語言技巧Rcpp與Eigen庫之間的相互轉(zhuǎn)換
這篇文章主要為大家介紹了R語言中Rcpp與Eigen庫之間的相互轉(zhuǎn)換的技巧操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11