相關性分析
相關性分析是指對兩個或多個具備相關性的變量元素進行分析饭冬,從而衡量兩個變量因素的相關密切程度。相關性分析旨在研究兩個或兩個以上隨機變量之間相互依存關系的方向和密切程度。
一般來講研究對象(樣品或處理組)之間使用距離分析,而元素(物種或環(huán)境因子)之間進行相關性分析。兩個變量之間的相關性可以用簡單相關系數(shù)(例如皮爾森相關系數(shù)等)進行表示腮介,相關系數(shù)越接近1,兩個元素相關性越大端衰,相關系數(shù)越接近0叠洗,兩個元素越獨立。
相關性指標
(1)Pearson相關系數(shù)(皮爾遜積差相關系數(shù))
Pearson相關系數(shù)是用于表示相關性大小的最常用指標旅东,數(shù)值介于-1~1之間灭抑,越接近0相關性越低,越接近-1或1相關性越高抵代。正負號表明相關方向腾节,正號為正相關、負號為負相關荤牍。適用于兩個正態(tài)分布的連續(xù)變量禀倔。
(2)Spearman等級相關系數(shù)(斯皮爾曼秩相關系數(shù))
利用兩變量的秩次大小來進行分析,屬于非參數(shù)統(tǒng)計方法参淫。適用于不滿足Pearson相關系數(shù)正態(tài)分布要求的連續(xù)變量。也可以用于有序分類變量的之間的相關性測量愧杯。
(3)Kendall's Tau相關系數(shù)
Kendall's Tau相關系數(shù)是一種非參數(shù)檢驗涎才,適用于兩個有序分類變量。
(4)其它
此外衡量兩個變量之間關系的方法還有:卡方檢驗力九、Fisher精確檢驗等耍铜。
cor函數(shù)
Pearson、Spearman跌前、Kendall相關系數(shù)都可以通過cor函數(shù)實現(xiàn)棕兼,cov協(xié)方差函數(shù)參數(shù)同cor函數(shù)。
(1)用法
cor(x,y=NULL,use="everything",method= c("pearson","kendall","spearman"))
cor(x, use='everything', method='pearson') #計算矩陣相關系數(shù)
cor(mtcars$mpg, mtcars$cyl) #計算兩兩相關系數(shù)
- x:矩陣或數(shù)據(jù)框抵乓。
- use:指定缺失數(shù)據(jù)的處理方式伴挚。可選項:all.obs(假設不存在缺失數(shù)據(jù))灾炭、everything(數(shù)據(jù)存在缺失值時茎芋,相關系數(shù)計算結果會顯示missing)、complete.obs(行刪除)蜈出、pairwise.complete.obs(成對刪除)田弥。
- method:指定相關系數(shù)的類型≌≡可選類型為pearson偷厦、spearman商叹、kendall。
(2)R Script
> testdata1[1:5,1:5] #隨意找的數(shù)據(jù)
CF1 CF2 CF3 CF4 CM1
rna13468 66.97984 80.07318 54.87525 91.65463 1.401584
rna885 26.53467 44.51450 33.65076 40.72113 60.633389
rna32332 0.00000 37.71825 11.89813 0.00000 1.403419
rna8744 42.44415 52.31791 60.35968 54.00533 39.524090
rna16488 0.00000 0.00000 0.00000 0.00000 0.000000
> cor_data <- cor(testdata1,method="pearson")
> round(cor_data[1:5,1:5],3)
CF1 CF2 CF3 CF4 CM1
CF1 1.000 0.668 0.697 0.952 -0.129
CF2 0.668 1.000 0.923 0.664 -0.534
CF3 0.697 0.923 1.000 0.647 -0.386
CF4 0.952 0.664 0.647 1.000 -0.203
CM1 -0.129 -0.534 -0.386 -0.203 1.000
> cor_data <- cor(testdata1,method="spearman")
> round(cor_data[1:5,1:5],3)
CF1 CF2 CF3 CF4 CM1
CF1 1.000 0.728 0.706 0.904 -0.055
CF2 0.728 1.000 0.785 0.734 -0.373
CF3 0.706 0.785 1.000 0.605 -0.212
CF4 0.904 0.734 0.605 1.000 -0.182
CM1 -0.055 -0.373 -0.212 -0.182 1.000
corrplot
(1)用法
corrplot(corr, #相關性系數(shù)矩陣
method = c("circle", "square", "ellipse", "number", "shade", "color", "pie"),
#可視化的方法只泼,可以是圓形剖笙、方形、橢圓形、數(shù)值肛跌、陰影页慷、顏色或餅圖形
type = c("full", "lower", "upper"),
#指定展示的方式,可以是完全的酪夷、下三角或上三角
add = FALSE,
col = NULL, #指定圖形展示的顏色,默認以均勻的顏色展示
bg = "white", #背景色
title = "", #標題
is.corr = TRUE, #是否為相關系數(shù)繪圖
diag = TRUE, #是否展示對角線上的結果
outline = FALSE, #是否繪制圓形孽惰、方形或橢圓形的輪廓
mar = c(0,0,0,0), #設置圖形的四邊間距
addgrid.col = NULL,
#當選擇的方法為顏色或陰影時晚岭,默認的網(wǎng)格線顏色為白色,否則為灰色
addCoef.col = NULL,
#為相關系數(shù)添加顏色勋功,默認不添加相關系數(shù)坦报,只有方法為number時,該參數(shù)才起作用
addCoefasPercent = FALSE, #是否將相關系數(shù)轉換為百分比格式
order = c("original", "AOE", "FPC", "hclust", "alphabet"),
#指定相關系數(shù)排序的方法狂鞋,可以是原始順序original片择、特征向量角序AOE、第一主成分順序FPC骚揍、
#層次聚類順序hclust和字母順序字管,一般AOE排序結果都比FPC要好
hclust.method = c("complete", "ward", "single", "average",
"mcquitty", "median", "centroid"),
#當order為hclust時,該參數(shù)可以是層次聚類中的7種之一
addrect = NULL, #當order為hclust時信不,可以為添加相關系數(shù)圖添加矩形框
rect.col = "black", #指定矩形框的顏色
rect.lwd = 2, #指定矩形框的線寬
tl.pos = NULL,
#指定文本標簽(變量名稱)的位置嘲叔,當type=full時,默認標簽位置在左邊和頂部(lt)抽活,
#當type=lower時硫戈,默認標簽在左邊和對角線(ld),當type=upper時下硕,默認標簽在頂部和對角線丁逝,
#d表示對角線,n表示不添加文本標簽
tl.cex = 1, #指定文本標簽的大小
tl.col = "red", #指定文本標簽的顏色
tl.offset = 0.4, tl.srt = 90,
cl.pos = NULL,
#圖例(顏色)位置梭姓,當type=upper或full時果港,圖例在右側,當type=lower時糊昙,圖例在底部辛掠,
#不需要圖例時,只需指定該參數(shù)為n
cl.lim = NULL,
cl.length = NULL, cl.cex = 0.8, cl.ratio = 0.15,
cl.align.text = "c",cl.offset = 0.5,
addshade = c("negative", "positive", "all"),
#只有當method=shade時,該參數(shù)才有用萝衩,參數(shù)值可以是negative/positive和all回挽,分別表示對負相關系數(shù)、
#正相關系數(shù)和所有相關系數(shù)添加陰影猩谊。注意:正相關系數(shù)的陰影是45度千劈,負相關系數(shù)的陰影是135度
shade.lwd = 1, #指定陰影的線寬
shade.col = "white", #指定陰影線的顏色
p.mat = NULL, sig.level = 0.05,
insig = c("pch","p-value","blank", "n"),
pch = 4, pch.col = "black", pch.cex = 3,
plotCI = c("n","square", "circle", "rect"),
lowCI.mat = NULL, uppCI.mat = NULL, ...)
(2)R Script
library(corrplot)
##默認參數(shù)
corrplot(cor_data)
##可視化方法
#"circle", "square", "ellipse", "number", "shade", "color", "pie"
corrplot(cor_data, method="pie",title="method=pie")
##展示的方式
#"full", "lower", "upper"
corrplot(cor_data, type="upper",title="type=upper")
##混合圖形樣式
#corrplot.mixed(matrix,lower="number",upper="circle")
#tl.col修改對角線的顏色,lower.col修改下三角的顏色,number.cex修改下三角字體大小
corrplot.mixed(cor_data,lower="ellipse",upper="pie")
corrplot.mixed(cor_data,lower="number",upper="pie",
tl.col="green",lower.col="skyblue",number.cex=1)
##order
#"original", "AOE", "FPC", "hclust", "alphabet"
#如果是hclust:
#addrect=4 是分組矩形
#rect.col = "black" 矩形框的顏色
#rect.lwd = 2 矩形框的線寬
#hclust.method = c("complete", "ward", "single", "average",
#"mcquitty", "median", "centroid")
corrplot(cor_data,order="hclust",hclust.method="average",addrect=4)
corrplot(cor_data,order="AOE")
##顏色
col1 <- colorRampPalette(c("blue","white","red"))
corrplot(cor_data,order="hclust",addrect=4,
col=col1(100),
bg="khaki1",addgrid.col="green",
tl.col="purple",tl.cex=0.7)
##添加數(shù)字
corrplot(cor_data,method="color",order="hclust",addrect=4,
col=col1(100),
tl.col="black",addCoef.col="grey",addCoefasPercent=T)
ggcorrplot
ggcorrplot包內只有2個函數(shù),一個cor_pmat()用于計算p值牌捷,一個ggcorrplot()用于繪圖墙牌。ggcorrplot相當于精簡版的corrplot包,只有主題更加豐富多樣暗甥。
(1)用法
ggcorrplot(corr, method = c("square", "circle"), type = c("full", "lower", "upper"),
ggtheme = ggplot2::theme_minimal, title = "",
show.legend = TRUE, legend.title = "Corr",
show.diag = FALSE,
colors = c("blue", "white", "red"), outline.color = "gray",
hc.order = FALSE, hc.method = "complete",
lab = FALSE, lab_col = "black", lab_size = 4, p.mat = NULL, sig.level = 0.05,
insig = c("pch", "blank"), pch = 4, pch.col = "black", pch.cex = 5,
tl.cex = 12, tl.col = "black", tl.srt = 45, digits = 2 )
(2)R Script
library(ggcorrplot)
##計算p值
cor_p <- cor_pmat(cor_data)
round(cor_p[1:5,1:5],3)
##默認繪圖square
ggcorrplot(cor_data)
##可視化方法
ggcorrplot(cor_data,method="circle")
##使用聚類順序
ggcorrplot(cor_data,hc.order=TRUE,outline.color="white")
##展示的方式
ggcorrplot(cor_data,hc.order=TRUE,outline.color="white",
type="lower")
##更改顏色
ggcorrplot(cor_data,hc.order=TRUE,outline.color="white",
type="lower",colors = c("#6D9EC1", "white", "#E46726"))
##更改主題
ggcorrplot(cor_data,hc.order=TRUE,outline.color="white",
type="lower",colors = c("#6D9EC1", "white", "#E46726"),
ggtheme = ggplot2::theme_void())
#添加相關系數(shù)
ggcorrplot(cor_data,hc.order=TRUE,outline.color="white",
type="lower",colors = c("#6D9EC1", "white", "#E46726"),
lab = TRUE)
#不顯著的畫x
ggcorrplot(cor_data,hc.order=TRUE,outline.color="white",
type="lower",colors = c("#6D9EC1", "white", "#E46726"),
p.mat = cor_p)
ggcorr
(1)R Script
##計算相關系數(shù)
ggcorr(testdata1,method=c("pairwise","spearman"))
##指定顏色標度中包含的斷點數(shù)
ggcorr(testdata1,method=c("pairwise","spearman"),
nbreaks = 5)
##設置圖例
ggcorr(testdata1,method=c("pairwise","spearman"),
name="12345", legend.position="bottom", legend.size=12) +
guides(fill=guide_colorbar(barwidth=18, title.vjust=0.75)) +
theme(legend.title=element_text(size=14))
##設置顏色
ggcorr(testdata1,method=c("pairwise","spearman"),
low="steelblue", mid="white", high="darkred")
##畫圓形
ggcorr(testdata1,method=c("pairwise","spearman"),
geom = "circle",min_size=2,max_size=6)
##添加相關系數(shù)
ggcorr(testdata1,method=c("pairwise","spearman"),
label=TRUE,label_size=3,label_color="white")
##控制變量標簽
ggcorr(testdata1,method=c("pairwise","spearman"),
hjust=0.75, size=5, color="grey50",layout.exp=1)
##顯示較高的相關系數(shù)
ggcorr(testdata1,method=c("pairwise","spearman"),
label=TRUE, hjust=0.75,geom="blank") +
geom_point(size=10, aes(color=coefficient>0,
alpha=abs(coefficient)>0.5)) +
scale_alpha_manual(values=c("TRUE"=0.25,"FALSE"=0)) +
guides(color=FALSE,alpha=FALSE)
樣品間相似性(similarity)和距離(distance)
(1)表示距離的方法
- 歐式距離(Euclidean Distance)
√(a2+b2+c^2)
dist(t(x),p=2)
- 曼哈頓距離(Manhattan Distance)
|a1-a2|+|b1-b2|+|c1-c2|
dist(t(x),"manhattan")
- 切比雪夫距離(Chebyshev Distance)
max(|a1-a2|,|b1-b2|,|c1-c2|)
dist(t(x),"maximum")
- 閔可夫斯基距離(Minkowski Distance)
dist(t(x),"minkowski")
- 標準化歐氏距離(Standardized Euclidean distance)
先將數(shù)據(jù)各維分量標準化到均值方差相等喜滨,即(x-μ)/δ,標準化后的值=(標準化前的值-分量的均值)/分量的標準差撤防。
x1 = scale(t(x), center=T,scale=T)
dist(x1)
- 馬氏距離(Mahalanobis Distance)
- 蘭式距離
dist(t(x), method = "canberra")
- 夾角余弦(Cosine)
- 漢明距離(Hamming distance)
兩個等長字符串s1與s2之間的漢明距離定義為將其中一個變?yōu)榱硗庖粋€所需要作的最小替換次數(shù)虽风。
x <- c(1, 0, 0)
y <- c(1, 0, 1)
hamming.distance(x, y) #1
- 杰卡德相似系數(shù)(Jaccard similarity coefficient)
dist(t(x), method = "Jaccard")
- 相關系數(shù)(Correlation coefficient)與相關距離(Correlation distance)
1-cor(x)
- 信息熵(Information Entropy)
信息熵是衡量分布的混亂程度或分散程度的一種度量。分布越分散(分布越平均)寄月,信息熵就越大辜膝。分布越有序(分布越集中),信息熵就越小漾肮。 - kl散度
(2)dist用法
This function computes and returns the distance matrix computed by using the specified distance measure to compute the distances between the rows of a data matrix.
這個函數(shù)用特定的方法計算矩陣的行之間的距離厂抖,并返回距離矩陣。
dist(x, method = "euclidean", diag = FALSE, upper = FALSE, p = 2)
- method:可以是"euclidean", "maximum", "manhattan", "canberra", "binary", "minkowski"
- diag:是否顯示對角線的值
- upper:是否顯示上三角的值
- p:The power of the Minkowski distance
(3)scale用法
scale(x, center = TRUE, scale = TRUE)
scale是對矩陣的每一列進行標準化克懊,如果要對行標準化需要先轉置验游。如heatmapdata <- t(scale(t(heatmapdata)))
(4)R Script
sampleDist <- dist(t(testdata1))
sampleDistMatrix <- as.matrix(sampleDist)
colnames(sampleDistMatrix) <- NULL
colors <- colorRampPalette(rev(brewer.pal(9,"Blues")))(255)
pheatmap(sampleDistMatrix,
clustering_distance_rows=sampleDist,
clustering_distance_cols=sampleDist,
color = colors)
作者:bio_meow
鏈接:http://www.reibang.com/p/aeb9f612e888
來源:簡書
著作權歸作者所有。商業(yè)轉載請聯(lián)系作者獲得授權保檐,非商業(yè)轉載請注明出處。