本次筆記內容:
cor.test()
和cor()
rcorr() {Hmisc}
corr.test() {psych}
spearmanCI() {spearmanCI}
spearman求95%CI (update 20200401)
相關系數(correlation coefficient)用于描述兩個變量之間的相關程度。一般在[-1, 1]之間。包括:
- pearson相關系數:適用于連續性變量,且變量服從正態分布的情況,為參數性的相關系數。
- spearman等相關系數:適用于連續性及分類型變量,為非參數性的相關系數。
在本次筆記中僅討論連續型變量的相關系數。
# 示例數據有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自帶包里的函數,兩者差別僅為cor()
只給出相關系數一個值,cor.test()
給出相關系數,p值等。
你可以把數據的兩組feature提出來進行相關性分析,看是否有相關性;也可以把包含多個feature的表格作為cor()
input,得到的是一個對稱的correlation matrix. 即所有feature兩兩比較的相關系數。然后你可以拿去各種可視化。cor.test()
似乎不能這樣用。
使用Hmisc
包的rcorr(),可以得到correlation matrix的p值矩陣。當然rcorr()
也可以像cor()
那樣,只計算兩個feature之間的相關系數。
## 只把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")
# 得到結果如下:
# > 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) # 如下圖所示
如果你想比較attitude
6個feature中前3個與后3個的關聯,并且需要進行多重矯正,需要使用psych包的corr.test()。
你有關于一套sample的兩套feature,比方說兩個dataframe, 其行是相同的(sample),列為不同的feature.那么可以corr.test(df1, df2, method= ...)
來計算兩組feature的相關系數并加以矯正。這時得到的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",則其相關系數與P值其實和cor.test()等得到的一樣。
可以根據P值,把P值做成*
,**
...這樣的的significant levels,便于后面畫熱圖。總的來說以下函數可以塞進去兩個你想比較的dataframe,得到相關系數,矯正后的P值,校正后的P值significant levels矩陣,結合heatmap.2,就可以畫圖了...
spearmanCI()
安裝spearmanCI
包。在cor.test()中method使用pearson, 默認結果中有95%CI,但是spearman沒有。
用法:spearmanCI(df[[var1]], df[[var2]], level=0.95)
注意一下它的Ouput不是一個完整的list...要把它讀出來:
capture.output(spearmanCI(...))
R里做相關系數的函數茫茫多,不止這幾個。以后如果要用到其他的再補上。
### 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的文檔