相关性计算

计算大量指标间的相关性,并以热图形式展示。
参考:http://www.sthda.com/english/wiki/correlation-matrix-a-quick-start-guide-to-analyze-format-and-visualize-a-correlation-matrix-using-r-software 【超赞的资料】

相关性热图
#计算相关性
require(Hmisc)
require(pheatmap)

d=read.table("example",header=T,row.names = 1)
head(d)
#        index1 index2 index3 index4 index5 index6 index7 index8 index9 index10
#sample1    134   3.60   29.3    9.0   55.8    2.7    0.4   1.05   0.32    2.01
#sample2    218   2.46   40.0    9.8   44.1    2.5    0.3   0.98   0.24    1.08
#sample3    185   3.74   46.7    6.5   37.5    5.7    1.1   1.75   0.24    1.40
#sample4    220   7.44   33.5    6.9   55.5    2.0    0.2   2.49   0.52    4.13
#sample5    228   4.23   37.4    4.5   53.9    1.3    0.3   1.58   0.19    2.28
#sample6    226   5.36   33.9    7.6   53.1    2.3    0.3   1.82   0.41    2.85

#index之间的相关性:得到相关性矩阵,可作热图/直接输出
res<-cor(d, use="pairwise.complete.obs") #默认method为pearson,可设置其他计算方法;针对缺失数据有不同处理方式
pheatmap(res)

#按列输出(方便筛选)
res2 <- rcorr(as.matrix(d))
out=flattenCorrMatrix(res2$r, res2$P)
head(out)
#     row column         cor           p
#1 index1 index2  0.53579901 0.002276657
#2 index1 index3 -0.06037896 0.751284491
#3 index2 index3 -0.30683542 0.099095706
#4 index1 index4 -0.01099476 0.954016664
#5 index2 index4 -0.14452340 0.446080828
#6 index3 index4  0.30426964 0.102098355

热图是根据相关性值做的,没有体现显著性值(p value),一般需要根据p值来进行筛选。

你可能感兴趣的:(相关性计算)