我在小型配对排名上计算Spearman的rho。斯皮尔曼以不妥善处理领带而闻名。例如,取2组8个排名,即使6个是两组中的一组中的关系,相关性仍然很高:
> cor.test(c(1,2,3,4,5,6,7,8), c(0,0,0,0,0,0,7,8), method="spearman")
Spearman's rank correlation rho
S = 19.8439, p-value = 0.0274
sample estimates:
rho
0.7637626
Warning message:
Cannot compute exact p-values with ties
对于这些数据,p值<.05似乎具有相当高的统计显着性。在R中是否有一个关系纠正版的Spearman?迄今为止,通过大量关系来计算它的最佳公式是什么?
那么,Kendall tau秩相关也是两个序数(或秩变换)变量之间统计依赖性的非参数检验 - 像Spearman's,但与Spearman不同,可以处理关系。
更具体地说,有三个Kendall tau统计数据 - tau-a,tau-b和tau-c。 tau-b特别适合处理领带。
tau-b统计量通过除数项处理关系(即,该对的两个成员具有相同的序数值),该除数项表示未绑定在x上的对的数量与未绑定在y上的数量之间的几何平均值。
Kendall的tau不是Spearman的 - 他们不一样,但他们也非常相似。您必须根据上下文决定两者是否足够相似,以便可以替换另一个。
例如,tau-b:
Kendall_tau_b = (P - Q) / ( (P + Q + Y0)*(P + Q + X0) )^0.5
P:一致对的数量('一致'表示该对数据点的每个成员的等级一致)
问:不一致对的数量
X0:未绑定在x上的对数
Y0:与y无关的对数
事实上,Spearman的rho的变体明确地说明了关系。在我需要非参数秩相关统计量的情况下,我总是选择tau而不是rho。原因是rho求平方误差,而tau求和绝对差值。鉴于tau和rho都是有能力的统计数据而且我们还有待选择,对我来说,差异性(tau)的线性惩罚似乎一直是表达等级相关性的更自然的方式。这不是一个推荐,你的背景可能会有很大不同,并且另有规定。
我认为exact=FALSE
可以解决问题。
cor.test(c(1,2,3,4,5,6,7,8), c(0,0,0,0,0,0,7,8), method="spearman", exact=FALSE)
Spearman's rank correlation rho
data: c(1, 2, 3, 4, 5, 6, 7, 8) and c(0, 0, 0, 0, 0, 0, 7, 8)
S = 19.8439, p-value = 0.0274
alternative hypothesis: true rho is not equal to 0
sample estimates:
rho
0.7637626
cor.test with method =“spearman”实际上计算了针对关系纠正的Spearman系数。我通过“手动”计算了Zar 1984,Biostatistical Analysis中的方程式中的平衡校正和未校正Spearman系数来检查它。这是代码 - 只需替换自己的变量名来检查自己:
ym <- data.frame(lousy, dors) ## my data
## ranking variables
ym$l <- rank(ym$lousy)
ym$d <- rank(ym$dors)
## calculating squared differences between ranks
ym$d2d <- (ym$l-ym$d)^2
## calculating variables for equations 19.35 and 19.37 in Zar 1984
lice <- as.data.frame(table(ym$lousy))
lice$t <- lice$Freq^3-lice$Freq
dorsal <- as.data.frame(table(ym$dors))
dorsal$t <- dorsal$Freq^3-dorsal$Freq
n <- nrow(ym)
sum.d2 <- sum(ym$d2d)
Tx <- sum(lice$t)/12
Ty <-sum(dorsal$t)/12
## calculating the coefficients
rs1 <- 1 - (6*sum.d2/(n^3-n)) ## "standard" Spearman cor. coeff. (uncorrected for ties) - eq. 19.35
rs2 <- ((n^3-n)/6 - sum.d2 - Tx - Ty)/sqrt(((n^3-n)/6 - 2*Tx)*((n^3-n)/6 - 2*Ty)) ## Spearman cor.coeff. corrected for ties - eq.19.37
##comparing with cor.test function
cor.test(ym$lousy,ym$dors, method="spearman") ## cor.test gives tie-corrected coefficient!
method="spearman"
为你提供绑定纠正的Spearman。根据定义,Spearman的rho只是Pearson对样本数据等级计算的样本相关系数。因此,无论是在场还是没有关系,它都有效。你可以看到,用他们的等级替换你的原始数据(关系中的中间人)和使用method="pearson"
后,你会得到相同的结果:
> cor.test(rank(c(1,2,3,4,5,6,7,8)), rank(c(0,0,0,0,0,0,7,8)), method="pearson")
Pearson's product-moment correlation
data: rank(c(1, 2, 3, 4, 5, 6, 7, 8)) and rank(c(0, 0, 0, 0, 0, 0, 7, 8))
t = 2.8983, df = 6, p-value = 0.0274
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.1279559 0.9546436
sample estimates:
cor
0.7637626
注意,存在一个简化的无关Spearman版本,实际上在没有关系的情况下用于cor.test()
实现,但它等同于上面的定义。cor.test()
实现中)都不计算精确的p值,因此警告。正如Eduardo的帖子所提到的,为了不发出警告,你应该设置exact=FALSE
,论文"A new rank correlation coefficient with application to the consensus ranking problem"旨在解决与领带问题的排名。它还提到不应将Tau-b用作衡量弱排序之间一致性的排序相关性度量。
Emond,E。J.和Mason,D。W.(2002),一种新的等级相关系数,适用于共识排序问题。 J.多评论。敌杀死。肛门,11:17-28。 DOI:10.1002 / mcda.313
我有一个类似的问题,通过阅读这里的答案和R上的帮助文件我看到,当你有关系时,你必须将参数exact = FALSE
添加到cor.test()
函数。通过添加它,它不会尝试计算精确的P值,而是“测试统计量是估计为零均值和单位方差,并且近似正态分布”。在我的情况下,结果完全相同,但没有关于关系的警告。
cor.test(x, y, method = "spearm", exact = FALSE)