假设我们现在有两组数据,如下所示
x
y
55.24
1.2
59.03
1.19
47.27
1.38
52.94
0.94
55
1.81
54
1.75
55.69
1.42
48.85
3.05
63.72
1.28
48.66
1.88
45.33
1.13
40.56
1.92
43.57
2.08
37.38
2.47
54.49
2.55
46.16
2.7
41.48
2.52
46.81
1.45
57.3
1.34
55.06
2.15
63.88
2.2
56.89
2
48.93
2.23
47.18
3.12
44.76
1.75
47.34
2.81
44.81
1.92
48.98
2.35
49.73
2.13
43.47
1.72
66.18
2.08
47.09
1.42
53.09
2.4
63.24
1.84
49.52
2.21
73.48
6.8
78.85
2.93
79.08
2.3
58.29
2.38
39.22
1.25
40.29
1.3
56.72
3.4
53.35
1.46
52.84
1.45
60.55
2.34
58.61
2.25
55.96
1.95
54.73
2.05
58.15
1.75
55.28
4.5
63.04
2.72
44.81
1.29
67.42
3.05
67.12
5
55.87
2.51
60.59
3.06
52.06
1.02
57.35
2.78
57.57
3.02
47.14
0.8
50.58
0.95
66.7
1.72
71.8
2.9
42.48
1.65
42.92
2.62
61.77
6.8
56.31
4.28
57.47
1.42
45.85
3.08
53.03
2.53
78.08
3.45
63.2
2.23
56.79
3.05
66.77
3.53
50.36
2.81
53.45
2.2
56.18
3.4
52.82
3.9
39.14
2.25
43.11
0.98
64.28
3.02
66.13
2.83
60.15
2.9
62.91
5.6
60.95
2.42
48.9
0.73
49.23
2.02
61.73
2.15
47.56
1.68
48.11
1.38
51.5
2.28
54.71
2.3
55.79
1.68
69.41
3.45
67.19
3.08
40.59
1.31
62.63
2.95
39.93
0.73
46.59
1.8
51.94
2.48
现在使用R语言做相关性分析
1 使用函数的说明
这里进行主要使用R语言中的函数cor.test()函数进行相关性系数的计算和检验。
函数功能:对成对数据进行相关性检验。里面有3中方法可供使用,分别是Pearson检验、Kendall检验和Spearman检验。
函数的使用格式为:
cor.test(x, y, alternative = c(“two.sided”, “less”, “greater”), method = c("pearson", "kendall", "spearman"),conf.level = 0.95)
其中x,y是供检验的样本;alternative指定是双侧检验还是单侧检验;method为检验的方法;conf.level为检验的置信水平。
2 数据的图形分析
执行下面的命令绘制散点图:
> data
read.table(file = "data.txt",header = TRUE)
> plot(data$x,
data$y)
得到的图形为:
从图中可以看出,图形中的点的分布并不是非常集中于一条直线,而且散落范围比较大。此时从图形中不能判断二者是否具有相关性。而且由于点的分散,下面将使用三种方法分别对其进行检验,检验中使用双侧检验,即alternative
= “two.sided”。
3 pearson检验
输入下面的命令实现检验
> cor.test(data$x, data$y,alternative = "two.sided",method =
"pearson",conf.level = 0.95)
结果为
Pearson's product-moment correlation
data:data$x and data$y
t = 5.0618, df = 98, p-value = 1.946e-06
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.2842595 0.5981865
sample estimates:
cor
0.4552588
从中可以看出二者的相关性系数为0.4552588,检验p值为1.946e-06<0.05。故x和y是有相关性的,但相关性并不是太大。
4 kendall检验
输入下面的代码进行检验
> cor.test(data$x, data$y,alternative = "two.sided",method =
"kendall", conf.level = 0.95)
结果为
Kendall's rank correlation tau
data:data$x and data$y
z = 4.572, p-value = 4.83e-06
alternative hypothesis: true tau is not equal to 0
sample estimates:
tau
0.3110132
从中可以看出二者的相关性度量值为0.3110132,检验p值为4.83e-06<0.05。故x和y是有相关性的,但相关性也并不是太大。
5 spearman检验
输入下面的代码进行检验
> cor.test(data$x, data$y,alternative = "two.sided",method =
"spearman",conf.level = 0.95)
结果为
Spearman's rank correlation rho
data:data$x and data$y
S = 90673.21, p-value = 1.874e-06
alternative hypothesis: true rho is not equal to 0
sample estimates:
rho
0.4559064
从中可以看出二者的相关性度量值为0.4559064,检验p值为1.874e-06<0.05。故x和y是有相关性的,但相关性也并不是太大。
6 综合分析
从上面的三种检验可以看出,虽然三种检验的结果都显示x与y具有相关性,但从相关性系数方面来看相关性并不是太高。
如果觉得《r语言kendall协和系数_使用R语言函数cor.test()做相关性计算和检验》对你有帮助,请点赞、收藏,并留下你的观点哦!