数据算法

R语言相关性的度量

2018-05-15  本文已影响113人  肖玉贤

R可以计算多种相关系数,包括pearson相关系数、Spearman相关系数、Kendall相关系数、偏相关系数、多分格(polychoric)相关系数和多系列(polyserial)相关系数。

1.pearson、spearson和Kendall相关

pearson积差相关系数衡量了两个定量变量之间的线性相关程度。spearson等级相关系数则衡量分级定序变量之间的相关程度,Kendall's Tau相关系数也是一种非参数的等级相关度量。

cor( )函数可以极端这三种相关系数,而cov()函数可以用来计算协方差。

> states<- state.x77[,1:6]

> cov(states)

              Population      Income  Illiteracy    Life Exp      Murder      HS Grad

Population 19931683.7588 571229.7796  292.8679592 -407.8424612 5663.523714 -3551.509551

Income      571229.7796 377573.3061 -163.7020408  280.6631837 -521.894286  3076.768980

Illiteracy      292.8680  -163.7020    0.3715306  -0.4815122    1.581776    -3.235469

Life Exp      -407.8425    280.6632  -0.4815122    1.8020204  -3.869480    6.312685

Murder        5663.5237  -521.8943    1.5817755  -3.8694804  13.627465  -14.549616

HS Grad      -3551.5096  3076.7690  -3.2354694    6.3126849  -14.549616    65.237894

> #可以看到,收入和高中毕业率之间存在显著的正先关性,文盲和预期寿命之间存在很强的负相关

> cor(states)

            Population    Income Illiteracy    Life Exp    Murder    HS Grad

Population  1.00000000  0.2082276  0.1076224 -0.06805195  0.3436428 -0.09848975

Income      0.20822756  1.0000000 -0.4370752  0.34025534 -0.2300776  0.61993232

Illiteracy  0.10762237 -0.4370752  1.0000000 -0.58847793  0.7029752 -0.65718861

Life Exp  -0.06805195  0.3402553 -0.5884779  1.00000000 -0.7808458  0.58221620

Murder      0.34364275 -0.2300776  0.7029752 -0.78084575  1.0000000 -0.48797102

HS Grad    -0.09848975  0.6199323 -0.6571886  0.58221620 -0.4879710  1.00000000

> #可以看到,pearson积差相关系数结果中,收入依然和高中毕业率有着很强的正相关;人口和文盲率之间有着很强的负相关性

> cor(states, method="pearson")

            Population    Income Illiteracy    Life Exp    Murder    HS Grad

Population  1.00000000  0.2082276  0.1076224 -0.06805195  0.3436428 -0.09848975

Income      0.20822756  1.0000000 -0.4370752  0.34025534 -0.2300776  0.61993232

Illiteracy  0.10762237 -0.4370752  1.0000000 -0.58847793  0.7029752 -0.65718861

Life Exp  -0.06805195  0.3402553 -0.5884779  1.00000000 -0.7808458  0.58221620

Murder      0.34364275 -0.2300776  0.7029752 -0.78084575  1.0000000 -0.48797102

HS Grad    -0.09848975  0.6199323 -0.6571886  0.58221620 -0.4879710  1.00000000

> cor(states, method="spearman")

          Population    Income Illiteracy  Life Exp    Murder    HS Grad

Population  1.0000000  0.1246098  0.3130496 -0.1040171  0.3457401 -0.3833649

Income      0.1246098  1.0000000 -0.3145948  0.3241050 -0.2174623  0.5104809

Illiteracy  0.3130496 -0.3145948  1.0000000 -0.5553735  0.6723592 -0.6545396

Life Exp  -0.1040171  0.3241050 -0.5553735  1.0000000 -0.7802406  0.5239410

Murder      0.3457401 -0.2174623  0.6723592 -0.7802406  1.0000000 -0.4367330

HS Grad    -0.3833649  0.5104809 -0.6545396  0.5239410 -0.4367330  1.0000000

> #举例分析,可以看到,文盲率和谋杀犯是紧密正相关的,收入和谋杀犯是强负相关的。

> x<- states[,c("Population","Income","Illiteracy","HS Grad")]

> y<- states[,c("Life Exp","Murder")]

> cor(x,y)

              Life Exp    Murder

Population -0.06805195  0.3436428

Income      0.34025534 -0.2300776

Illiteracy -0.58847793  0.7029752

HS Grad    0.58221620 -0.4879710

> #也可以有选择的对变量之间的相关性进行分析,结果表明:人口和预期寿命之间存在显著的负相关;文盲和犯罪率之间存在显著的正相关。

> library(ggm)

> colnames(states)

[1] "Population" "Income"    "Illiteracy" "Life Exp"  "Murder"    "HS Grad" 

> pcor(c(1,5,2,3,6),cov(states))

[1] 0.3462724

> #偏相关是指在控制一个或多个变量定量变量时,另外两个定量变量之间的相互关系。可以使用ggm包中的pcor()函数计算偏相关系数。格式为pcor()

> #pcor(μ,S)

> #其中μ是一个数值向量,前两个数值表示要计算相关系数的变量下标,其余的数值为条件变量的下标。s为变量的协方差阵。

> #这里的ggm包需要安装

> #分析可知,在控制了收入、文盲率和高中毕业率的影响时,人口和谋杀率之间的相关系数为0.346.偏相关系数常用于社会科学的研究中。

> cor.test(states[,3],states[,5])

        Pearson's product-moment correlation

data:  states[, 3] and states[, 5]

t = 6.8479, df = 48, p-value = 1.258e-08

alternative hypothesis: true correlation is not equal to 0

95 percent confidence interval:

0.5279280 0.8207295

sample estimates:

      cor

0.7029752

> #上述检验的是文盲率和谋杀率之间的关系,明显是显著相关的,相关系数为0.703

> library(psych)

> corr.test(states, use="complete")

Call:corr.test(x = states, use = "complete")

Correlation matrix

          Population Income Illiteracy Life Exp Murder HS Grad

Population      1.00  0.21      0.11    -0.07  0.34  -0.10

Income          0.21  1.00      -0.44    0.34  -0.23    0.62

Illiteracy      0.11  -0.44      1.00    -0.59  0.70  -0.66

Life Exp        -0.07  0.34      -0.59    1.00  -0.78    0.58

Murder          0.34  -0.23      0.70    -0.78  1.00  -0.49

HS Grad        -0.10  0.62      -0.66    0.58  -0.49    1.00

Sample Size

[1] 50

Probability values (Entries above the diagonal are adjusted for multiple tests.)

          Population Income Illiteracy Life Exp Murder HS Grad

Population      0.00  0.59      1.00      1.0  0.10      1

Income          0.15  0.00      0.01      0.1  0.54      0

Illiteracy      0.46  0.00      0.00      0.0  0.00      0

Life Exp        0.64  0.02      0.00      0.0  0.00      0

Murder          0.01  0.11      0.00      0.0  0.00      0

HS Grad          0.50  0.00      0.00      0.0  0.00      0

To see confidence intervals of the correlations, print with the short=FALSE option

> #虽然毕业率和人口数的相关系数为0.10,但是并不显著,其中p=0.50;而收入和毕业率的相关系数为0.62,显示显著。


相关性度量的基本知识到这就结束了,咱们下期再见!O(∩_∩)O哈哈~

上一篇下一篇

猜你喜欢

热点阅读