R語言的相關性分析

R語言的相關性分析

花花寫於2020-04-06,TCGA和R包都告一段落,這幾天開始學些統計學知識。收集了一些資料,statquest在B站有了中英字幕版(直接搜索statquest即可),也有成套的中文學習筆記可供參考,學習難度下降了不少。筆記鏈接:https://www.yuque.com/biotrainee/biostat統計學的知識點比較瑣碎,很難整理,正在克服困難中。。。

1.示例數據

x1:R語言內置數據集iris的前4列。 x2:R語言內置數據集state.x77

<code>x = iris[,-5]
x2 = state.x77/<code>

state.x77列名的含義:Population:截至1975年7月1日的人口估計Income:人均收入(1974)Illiteracy:文盲率(1970年,占人口百分比)Life Exp:預期壽命(1969-71年)Murder:每10萬人的謀殺和非過失殺人率(1976)HS Grad 高中畢業生百分比(1970)Frost:首都或大城市中最低溫度低於冰點(1931-1960)的平均天數Area:土地面積(平方英里)

2.協方差

使用cov()函數計算。

關於協方差:cov(x,y)>0,表示x、y的變化為正趨勢,<0為負趨勢,=0為無趨勢。協方差對數據的變化範圍敏感,無法反應變化趨勢的強弱和離散程度,但它是一些高級分析的基石。

<code>cov(x$Sepal.Length,x$Petal.Length)
#> [1] 1.274315
cov(x)
#>              Sepal.Length Sepal.Width Petal.Length Petal.Width
#> Sepal.Length    0.6856935  -0.0424340    1.2743154   0.5162707
#> Sepal.Width    -0.0424340   0.1899794   -0.3296564  -0.1216394
#> Petal.Length    1.2743154  -0.3296564    3.1162779   1.2956094
#> Petal.Width     0.5162707  -0.1216394    1.2956094   0.5810063
pheatmap::pheatmap(cov(x))/<code>
R語言的相關性分析

image.png

3.相關

3.1計算相關性係數

cor函數可計算三種相關性係數:pearson,kendallspearman,默認是pearson。pearson是參數檢驗,需要兩個向量均服從正態分佈。另外兩個為非參數檢驗。

  • 輸入值為兩個向量
<code>cor(x$Sepal.Length,x$Petal.Length)
#> [1] 0.8717538
cor(x$Sepal.Length,x$Petal.Length,method = "kendall")
#> [1] 0.7185159
cor(x$Sepal.Length,x$Petal.Length,method = "spearman")
#> [1] 0.8818981/<code>

-輸入值為一個數值型數據框/矩陣

<code>cor(x)
#>              Sepal.Length Sepal.Width Petal.Length Petal.Width
#> Sepal.Length    1.0000000  -0.1175698    0.8717538   0.8179411
#> Sepal.Width    -0.1175698   1.0000000   -0.4284401  -0.3661259
#> Petal.Length    0.8717538  -0.4284401    1.0000000   0.9628654
#> Petal.Width     0.8179411  -0.3661259    0.9628654   1.0000000
pheatmap::pheatmap(cor(x))/<code>
R語言的相關性分析

image.png

可見,計算的結果是x的4個變量(4列)兩兩之間的相關性。

3.2 相關係數的顯著性檢驗

<code>cor.test(x$Sepal.Length,x$Petal.Length)
#> 
#>  Pearson's product-moment correlation
#> 
#> data:  x$Sepal.Length and x$Petal.Length
#> t = 21.646, df = 148, p-value #> alternative hypothesis: true correlation is not equal to 0
#> 95 percent confidence interval:
#>  0.8270363 0.9055080
#> sample estimates:
#>       cor 
#> 0.8717538
cor.test(x$Sepal.Length,x$Petal.Length,method = "kendall")
#> 
#>  Kendall's rank correlation tau
#> 
#> data:  x$Sepal.Length and x$Petal.Length
#> z = 12.647, p-value #> alternative hypothesis: true tau is not equal to 0
#> sample estimates:
#>       tau 
#> 0.7185159
cor.test(x$Sepal.Length,x$Petal.Length,method = "spearman")
#> Warning in cor.test.default(x$Sepal.Length, x$Petal.Length, method =
#> "spearman"): Cannot compute exact p-value with ties
#> 
#>  Spearman's rank correlation rho
#> 
#> data:  x$Sepal.Length and x$Petal.Length
#> S = 66429, p-value #> alternative hypothesis: true rho is not equal to 0
#> sample estimates:
#>       rho 
#> 0.8818981/<code>

cor.test函數還有一個alternative參數,表示單邊/雙邊檢驗。有三個取值:“two.sided”(雙邊檢驗), “less”, “greater”。 相關性係數大於0時,應使用greater; 小於0時,應使用less; 如果不指定,則默認“two.sided”。

4.偏相關

即在控制一個或多個其他變量時,兩個變量之間的相互關係。(這裡的變量都應是連續型變量)

控制某個變量,指的是排除該變量的影響。被控制的變量稱為條件變量。

使用ggm::pcor()函數來計算。用法為:pcor(u, S)。

  • u為一個表示列號的向量,前兩個元素為研究對象,其他元素是條件變量。例如c(1,2,4,5),表示在控制4、5列的條件下,研究1和2列的相關性。
  • S 是協方差矩陣

舉個栗子

人口數量(第一列)和收入水平(第二列)都可能影響文盲率(第三列),如果直接分別計算相關性的話:

<code>cor(x2[,1],x2[,3])
#> [1] 0.1076224
cor(x2[,2],x2[,3])
#> [1] -0.4370752/<code>

相關係數約為0.1和-4.3。控制其中一個變量計算另一個變量的影響,結果則不同。

<code>#install.packages("ggm") 

library(ggm)
#在控制收入的條件下,人口數量對文盲率的影響
pcor(c(1,3,2),cov(x2))
#> [1] 0.2257943
#在控制人口的條件下,收入對文盲率的影響
pcor(c(2,3,1),cov(x2))
#> [1] -0.4725271/<code>

偏相關係數為0.2和-0.47,相比原來,絕對值大了一些。

同樣的道理,控制收入、文盲率的影響,研究人口與謀殺率的偏相關性:

<code>pcor(c(1,5,2,3),cov(x2))
#> [1] 0.3621683/<code>

偏相關性的顯著性檢驗

<code>pcor.test(pcor(c(2,3,1),cov(x2)),q=3,n=50)
#> $tval
#> [1] -3.596675
#> 
#> $df
#> [1] 45
#> 
#> $pvalue
#> [1] 0.0007972922/<code>

用法為:pcor.test(r, q, n)

r是偏相關性計算結果,q是變量數,n是樣本數,在幫助文檔中有描述。


分享到:


相關文章: