加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

cor()函数

(2017-02-06 16:54:52)
标签:

r语言

数据分析

 R自带的程序包stats中,使用cor()可以计算相关系数。

> data(trees)

> str(trees)

'data.frame':     31 obs. of  3 variables:

 $ Girth : num  8.3 8.6 8.8 10.5 10.7 10.8 11 11 11.1 11.2 ...

 $ Height: num  70 65 63 72 81 83 66 75 80 75 ...

 $ Volume: num  10.3 10.3 10.2 16.4 18.8 19.7 15.6 18.2 22.6 19.9 ...

> data.frame(trees)

   Girth Height Volume

1    8.3     70   10.3

2    8.6     65   10.3

3    8.8     63   10.2

4   10.5     72   16.4

5   10.7     81   18.8

6   10.8     83   19.7

7   11.0     66   15.6

8   11.0     75   18.2

9   11.1     80   22.6

9   11.1     80   22.6

10  11.2     75   19.9

11  11.3     79   24.2

12  11.4     76   21.0

13  11.4     76   21.4

14  11.7     69   21.3

15  12.0     75   19.1

16  12.9     74   22.2

17  12.9     85   33.8

18  13.3     86   27.4

19  13.7     71   25.7

20  13.8     64   24.9

21  14.0     78   34.5

22  14.2     80   31.7

23  14.5     74   36.3

24  16.0     72   38.3

25  16.3     77   42.6

26  17.3     81   55.4

27  17.5     82   55.7

28  17.9     80   58.3

29  18.0     80   51.5

30  18.0     80   51.0

31  20.6     87   77.0

> cor(trees,method = "pearson")

           Girth    Height    Volume

Girth  1.0000000 0.5192801 0.9671194

Height 0.5192801 1.0000000 0.5982497

Volume 0.9671194 0.5982497 1.0000000

> cor(trees,method = "spearman")

           Girth    Height    Volume

Girth  1.0000000 0.4408387 0.9547151

Height 0.4408387 1.0000000 0.5787101

Volume 0.9547151 0.5787101 1.0000000

> cor(trees,method = "kendall")

           Girth    Height    Volume

Girth  1.0000000 0.3168641 0.8302746

Height 0.3168641 1.0000000 0.4496306

Volume 0.8302746 0.4496306 1.0000000

相关性检验使用程序包stats中的cor.test(),用法和cor()类似,参数alternative=”two.sided”,”less”,”greater”分别表示双侧检验、右侧检验、左侧检验。参数conf.level控制置信水平。

> cor.test(trees[,1],trees[,2],method = "pearson")

 

      Pearson's product-moment correlation

 

data:  trees[, 1] and trees[, 2]

t = 3.2722, df = 29, p-value = 0.002758

alternative hypothesis: true correlation is not equal to 0

95 percent confidence interval:

 0.2021327 0.7378538

sample estimates:

      cor

0.5192801

程序包Kendall主要用于计算Kendall相关系数和Mann-Kendall趋势性检验。Kendallxy)可以计算相关系数,并进行检验。如果数据没有打结,结果和corx,y,method=”kendall”)、cor.test(x,y,method=”kendall”)一样,如果数据打结,相关系数仍相同,但是检验的p值不同。

> x<-c(1.5,1.5,3,4,6,6,6,8,9.5,9.5,11,12)

> y<-c(2.5,2.5,7,4.5,1,4.5,6,11.5,11.5,8.5,8.5,10)

> cor.test(x,y,method = "kendall")

 

      Kendall's rank correlation tau

 

data:  x and y

z = 2.3846, p-value = 0.0171

alternative hypothesis: true tau is not equal to 0

sample estimates:

      tau

0.5528638

 

Warning message:

In cor.test.default(x, y, method = "kendall") :

  Cannot compute exact p-value with ties

> library(Kendall)

> summary(Kendall(x,y))

Score =  34 , Var(Score) = 203.303

denominator =  61.49797

tau = 0.553, 2-sided pvalue =0.020645

程序包pspearman中的spearman.test()在不打结时提供了更精确的p值。cor.test(,method=”spearman”)根据数据量的不同分别使用了AS89t-distribution两种近似方法,在spearman.test中参数approximation=c(“exact”,”AS89”,”t-distribution”)可以选择近似的方法,默认是精确值。

> library(pspearman)

> x<-1:10

> y<-c(5:1,6,10:7)

> out1<-spearman.test(x,y)

> out2<-spearman.test(x,y,approximation = "AS89")

> out3<-cor.test(x,y,method = "spearman")

> out1$p.value

[1] 0.05443067

> out2$p.value

[1] 0.05444507

> out3$p.value

[1] 0.05444507

程序包SuppDists则给出了SpearmanKendall相关系数的密度函数、分布函数、分位点函数以及随机数,下面的命令给出了样本数10,相关系数为0.95P值。

> library(SuppDists)

> pSpearman(0.95,10)

[1] 0.9999755

> pKendall(0.95,10)

[1] 0.9999997

0

阅读 收藏 喜欢 打印举报/Report
前一篇:相关可视化
后一篇:安装rJava包
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有