R语言 Kendall相关测试

R语言 Kendall相关测试

相关性是一种统计测量,表明两个变量之间的关系有多强。它也涉及多个变量之间的关系。例如,如果人们有兴趣知道父亲和儿子的身高之间是否有关系,可以计算相关系数来回答这个问题。一般来说,它位于-1和+1之间。它是协方差的比例版本,提供了关系的方向和强度。它是无尺寸的。主要有两种类型的相关系数。

  • 参数相关 – 皮尔逊相关(r):它测量两个变量(x和y)之间的线性关系,被称为参数相关测试,因为它取决于数据的分布。
  • 非参数相关 – Kendall(tau)和Spearman(rho) : 它们是基于等级的相关系数,被称为非参数相关。

肯德尔等级相关系数公式

Kendall Rank Correlation是基于等级的相关系数,也被称为非参数性相关。计算Kendall Rank Correlation的公式如下。

R编程中的Kendall相关测试

其中

  • 协和对: 一对观测值(x1,y1)和(x2,y2)遵循以下属性
    • x1 > x2和y1 > y2或
    • x1 < x2 和 y1 < y2
  • 不和谐的一对: 一对观察值(x1,y1)和(x2,y2),遵循的属性是
    • x1 > x2 和 y1 < y2 或
    • x1 < x2 and y1 > y2
  • n: 样本总数

注:X1=X2Y1=Y2 没有被归类为一致或不一致的一对被忽略了。

在R中的实现

R语言提供了两种计算相关系数的方法。通过使用函数cor()或 cor.test() 可以计算。可以注意到, cor() 计算的是相关系数,而 cor.test() 计算的是成对样本之间的关联或相关测试。它同时返回相关系数和相关的显著性水平(或P值)。

语法:

cor(x, y, method = “kendall”)

cor.test(x, y, method = “kendall”)

参数:

x, y: 具有相同长度的数字向量

方法: 相关的方法

例1 :

使用cor()方法

例子

# R program to illustrate 
# Kendall Correlation Testing 
# Using cor() 
  
# Taking two numeric 
# Vectors with same length 
x = c(1, 2, 3, 4, 5, 6, 7)  
y = c(1, 3, 6, 2, 7, 4, 5) 
  
# Calculating  
# Correlation coefficient 
# Using cor() method 
result = cor(x, y, method = "kendall") 
  
# Print the result 
cat("Kendall correlation coefficient is:", result) 
R

输出

Kendall correlation coefficient is: 0.4285714
R

使用cor.test()方法

示例

# R program to illustrate 
# Kendall Correlation Testing 
# Using cor.test() 
  
# Taking two numeric 
# Vectors with same length 
x = c(1, 2, 3, 4, 5, 6, 7)  
y = c(1, 3, 6, 2, 7, 4, 5) 
  
# Calculating  
# Correlation coefficient 
# Using cor.test() method 
result = cor.test(x, y, method = "kendall") 
  
# Print the result 
print(result) 
R

输出

Kendall's rank correlation tau

data:  x and y
T = 15, p-value = 0.2389
alternative hypothesis: true tau is not equal to 0
sample estimates:
      tau 
0.4285714 
R

在上面的输出中。

  • T是检验统计量的值(T=15)
  • p值是检验统计量的显著性水平(p值=0.2389)。
  • 替代假设是描述替代假设的字符串(真tau不等于0)。
  • 样本估计值是相关系数。对于Kendall相关系数,它被命名为tau(Cor.coeff = 0.4285)。

例2 :

例子

# R program to illustrate 
# Kendall Correlation Testing 
  
# Import data into RStudio 
df = read.csv("Auto.csv") 
  
# Taking two column 
# Vectors with same length 
x = dfmpg 
y = dfweight 
  
  
# Calculating 
# Correlation coefficient 
# Using cor() method 
result = cor(x, y, method = "kendall") 
  
# Print the result 
cat("Kendall correlation coefficient is:", result) 
  
# Using cor.test() method 
res = cor.test(x, y, method = "kendall") 
print(res) 
R

输出

Kendall correlation coefficient is: -0.7517463
    Kendall's rank correlation tau

data:  x and y
z = -19.161, p-value < 2.2e-16
alternative hypothesis: true tau is not equal to 0
sample estimates:
       tau 
-0.7517463 
R

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册