R使用筆記:相關(guān)系數(shù):cor.test();corr.test();rcorr()

本次筆記內(nèi)容:

  • cor.test()cor()
  • rcorr() {Hmisc}
  • corr.test() {psych}
  • spearmanCI() {spearmanCI} spearman求95%CI (update 20200401)

相關(guān)系數(shù)(correlation coefficient)用于描述兩個變量之間的相關(guān)程度。一般在[-1, 1]之間。包括:

  • pearson相關(guān)系數(shù):適用于連續(xù)性變量,且變量服從正態(tài)分布的情況,為參數(shù)性的相關(guān)系數(shù)。
  • spearman等相關(guān)系數(shù):適用于連續(xù)性及分類型變量,為非參數(shù)性的相關(guān)系數(shù)。

在本次筆記中僅討論連續(xù)型變量的相關(guān)系數(shù)。

# 示例數(shù)據(jù)有6個變量:
data("attitude")
head(attitude)
    rating complaints privileges learning raises critical advance
1     43         51         30       39     61       92      45
2     63         64         51       54     63       73      47
3     71         70         68       69     76       86      48
4     61         63         45       47     54       84      35
5     81         78         56       66     71       83      47
6     43         55         49       44     54       49      34

cor.test()cor()都是R自帶包里的函數(shù),兩者差別僅為cor()只給出相關(guān)系數(shù)一個值,cor.test()給出相關(guān)系數(shù),p值等。

你可以把數(shù)據(jù)的兩組feature提出來進行相關(guān)性分析,看是否有相關(guān)性;也可以把包含多個feature的表格作為cor()input,得到的是一個對稱的correlation matrix. 即所有feature兩兩比較的相關(guān)系數(shù)。然后你可以拿去各種可視化。cor.test()似乎不能這樣用。
使用Hmisc包的rcorr(),可以得到correlation matrix的p值矩陣。當然rcorr()也可以像cor()那樣,只計算兩個feature之間的相關(guān)系數(shù)。

## 只把attitude中的rating和complaints作為input
cortest_ra_com <- cor.test(attitude$rating, attitude$complaints, method = "pearson")
cor_ra_com <- cor(attitude$rating, attitude$complaints, method = "pearson")
# 得到結(jié)果如下:
# > cortest_ra_com

#   Pearson's product-moment
#   correlation

# data:  attitude$rating and attitude$complaints
# t = 7.737, df = 28,
# p-value = 1.988e-08
# alternative hypothesis: true correlation is not equal to 0
# 95 percent confidence interval:
# 0.6620128 0.9139139
# sample estimates:
#      cor 
# 0.8254176 

# > cor_ra_com
# [1] 0.8254176


## 把attitue中6個feature都作為input
cor_ <- cor(attitude, method = 'pearson')
View(cor_) # 如下圖所示
library(Hmisc)
cortest <- rcorr(as.matrix(attitude), type = "pearson")
View(cortest$P) # 如下圖所示
View(cor_)與View(cortest$r)結(jié)果一樣
View(cortest$P)

如果你想比較attitude6個feature中前3個與后3個的關(guān)聯(lián),并且需要進行多重矯正,需要使用psych包的corr.test()。
你有關(guān)于一套sample的兩套feature,比方說兩個dataframe, 其行是相同的(sample),列為不同的feature.那么可以corr.test(df1, df2, method= ...)來計算兩組feature的相關(guān)系數(shù)并加以矯正。這時得到的output不是對稱的,而是ncol(df1) * ncol(df2)
需要注意如果input為兩個dataframe, 兩者的row必須長度和順序都一致。

library(psych)
cortest_psy <- corr.test(attitude[1:3], attitude[4:6], method = "pearson")
cortest_psy_sdj <- corr.test(attitude[1:3], attitude[4:6], method = "pearson", adjust = "fdr")
# 如果不矯正,即adjust ="none",則其相關(guān)系數(shù)與P值其實和cor.test()等得到的一樣。

可以根據(jù)P值,把P值做成*,**...這樣的的significant levels,便于后面畫熱圖??偟膩碚f以下函數(shù)可以塞進去兩個你想比較的dataframe,得到相關(guān)系數(shù),矯正后的P值,校正后的P值significant levels矩陣,結(jié)合heatmap.2,就可以畫圖了...

spearmanCI()
安裝spearmanCI包。在cor.test()中method使用pearson, 默認結(jié)果中有95%CI,但是spearman沒有。
用法:spearmanCI(df[[var1]], df[[var2]], level=0.95)
注意一下它的Ouput不是一個完整的list...要把它讀出來:
capture.output(spearmanCI(...))

R里做相關(guān)系數(shù)的函數(shù)茫茫多,不止這幾個。以后如果要用到其他的再補上。

### fun_to_corr: 
#### input: heat_in_1, heat_in_2: f_ra/g_ra/biochem/kegg_in...you can select the feature first
#### output: list(), t_cor is correlation index(-1~1), t_p is raw p-value, t_p_sig is formatted with significant levels
#### formatted significant levels: 0~0.001: **; 0.001~0.01: *; 0.01~0.1: +; >0.1 nothing
fun_to_corr <- function(heat_in_1, heat_in_2) {
  t <- corr.test(heat_in_1, heat_in_2, use = "pairwise", method = "spearman", adjust = "fdr")
  t_cor <- data.frame(t$r, check.names = FALSE)
  t_p <- data.frame(t$p, check.names = FALSE)
  cut_sig <- function(p) {
    out <- cut(p, breaks = c(0, 0.001,0.01,0.1,1), include.lowest = T, labels = c("**", "*", "+", ""))
    return(out)
  }
  t_p_sig <- apply(t_p, 2, cut_sig)
  rownames(t_p_sig) <- rownames(t_p)
  return(list(t_cor = t_cor, t_p_sig = t_p_sig, t_p = t_p))
}

Ref:
更多見STHDA的教程
corr.test的文檔

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 特征選擇 特征選擇(排序)對于數(shù)據(jù)科學家、機器學習從業(yè)者來說非常重要。好的特征選擇能夠提升模型的性能,更能幫助我們...
    hzyido閱讀 6,857評論 1 16
  • 結(jié)合Scikit-learn介紹幾種常用的特征選擇方法 作者:Edwin Jarvis 特征選擇(排序)對于數(shù)據(jù)科...
    阿甘run閱讀 3,338評論 1 14
  • 看到一篇好文章分享出來,看別人是如何選特征的,作者是Edwin Jarvis 作者:Edwin Jarvis 特征...
    智能互連閱讀 5,774評論 0 7
  • 今天是個特別的日子里,昨天夜里的風雨,今晨雨過天晴。心情無比舒暢。做完尼姑嗎瑜伽后,又做“感恩冥想”。吃過早飯,準...
    美玲_閱讀 263評論 0 0
  • 現(xiàn)實總是會在你艱難的時刻給你增加難度。 心里不禁暗自感嘆命運總是喜歡折磨苦難之人。 陪伴了兩年出頭的蘋果5S終于熬...
    南方十二閱讀 224評論 0 0

友情鏈接更多精彩內(nèi)容