R:找到两个排名之间的肯德尔距离

时间:2015-08-24 01:02:02

标签: r

library(RMallow)
seq1 = do.call("rbind", list(c(1, 2.5, 2.5, 4, 5.5, 5.5, 7.5, 7.5)))
seq2 = do.call("rbind", list(c(1.5, 1.5, 3, 4.5, 4.5, 6, 7, 8)))
seq1
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
# [1,]    1  2.5  2.5    4  5.5  5.5  7.5  7.5
seq2
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
# [1,]  1.5  1.5    3  4.5  4.5    6    7    8
AllKendall(seq1, seq2)
Error in array(STATS, dims[perm]) : 'dims' cannot be of length 0
In addition: Warning message:
In sweep(data.info, 2, seqs.info, "-") :
  STATS is longer than the extent of 'dim(x)[MARGIN]'

我正在使用包AllKendall中的RMallow函数。但是,我试图计算肯德尔在这两个序列之间的距离时遇到了错误。此功能仅适用于超过2个排名吗?我该如何解决这个错误?

1 个答案:

答案 0 :(得分:0)

如果查看AllKendall()函数的文档,它会声明它计算两组不同排名之间的所有Kendall距离。 1-8的列表仅仅是8个对象的排名,而不是排名的。换句话说,AllKendall()包中的RMallow期望您传递一组矩阵。

示例:(将返回Kendall距离的所有可能组合的矩阵)

#expanded on seq1 and seq2

seq3 = do.call("rbind", list(c(1, 2.5, 2.5, 4, 5.5, 5.5, 7.5, 7.5), c(5,6,7,8,4,3,2,1)))
seq4 = do.call("rbind", list(c(1.5, 1.5, 3, 4.5, 4.5, 6, 7, 8), c(8,1,7,2,6,3,5,4)))
AllKendall(seq3, seq4)
     [,1] [,2]
[1,]    0   14
[2,]   21   14

如果您只是想比较一对排名,我建议您使用Kendall()包中的Kendall功能。

#your original sequences
seq1 = do.call("rbind", list(c(1, 2.5, 2.5, 4, 5.5, 5.5, 7.5, 7.5)))
seq2 = do.call("rbind", list(c(1.5, 1.5, 3, 4.5, 4.5, 6, 7, 8)))

值得指出的是,只运行Kendall功能实际上并没有给你肯德尔距离

Kendall(seq1, seq2)
tau = 0.902, 2-sided pvalue =0.004694

相反,您必须保存Kendall对象并单独调用每个部分。

k <- Kendall(seq1, seq2) #from 'Kendall' package

k$tau #Kendall’s tau statistic
[1] 0.9021342
attr(,"Csingle")
[1] TRUE

k$sl #two-sided p-value
[1] 0.004693985
attr(,"Csingle")
[1] TRUE

k$S #Kendall score
[1] 23
attr(,"Csingle")
[1] TRUE

k$D #denominator, tau=S/D
[1] 25.4951
attr(,"Csingle")
[1] TRUE

k$varS #variance of S
[1] 60.54762
attr(,"Csingle")