在 R 中对 Spearman 相关性进行相关性检验时,如何避免出现"无法计算具有联系的精确 p 值"警告?
r programmingserver side programmingprogramming更新于 2025/4/15 13:07:17
当变量不是连续的但可以排序时,我们不使用皮尔逊相关系数来寻找线性关系,在这种情况下,斯皮尔曼相关系数就派上用场了。由于斯皮尔曼相关系数考虑了值的等级,因此相关性检验会忽略相同的等级来查找 p 值,因此我们收到警告"无法计算具有联系的精确 p 值"。这可以通过在 cor.test 函数中使用 exact = FALSE 来避免。
示例
考虑以下向量并执行斯皮尔曼相关测试以检查它们之间的关系 −
x1<-rpois(20,2) y1<-rpois(20,5) cor.test(x1,y1,method="spearman")
输出
Spearman's rank correlation rho data: x1 and y1 S = 1401.7, p-value = 0.8214 alternative hypothesis: true rho is not equal to 0 sample estimates: rho -0.05390585 Warning message: In cor.test.default(x1, y1, method = "spearman") : Cannot compute exact p-value with ties
这里,我们有平局警告,这可以通过使用 exact=FALSE 来避免,如下所示 −
示例
cor.test(x1,y1,method="spearman",exact=FALSE)
输出
Spearman's rank correlation rho data: x1 and y1 S = 1401.7, p-value = 0.8214 alternative hypothesis: true rho is not equal to 0 sample estimates: rho -0.05390585
让我们看更多例子 −
示例
x2<-sample(1:100,500,replace=TRUE) y2<-sample(1:50,500,replace=TRUE) cor.test(x2,y2,method="spearman")
输出
Spearman's rank correlation rho data: x2 and y2 S = 20110148, p-value = 0.4387 alternative hypothesis: true rho is not equal to 0 sample estimates: rho 0.03470902 Warning message: In cor.test.default(x2, y2, method = "spearman") : Cannot compute exact p-value with ties
示例
cor.test(x2,y2,method="spearman",exact=FALSE)
输出
Spearman's rank correlation rho data: x2 and y2 S = 20110148, p-value = 0.4387 alternative hypothesis: true rho is not equal to 0 sample estimates: rho 0.03470902
示例
x3<-sample(101:110,5000,replace=TRUE) y3<-sample(501:510,5000,replace=TRUE) cor.test(x3,y3,method="spearman")
输出
Spearman's rank correlation rho data: x3 and y3 S = 2.0642e+10, p-value = 0.5155 alternative hypothesis: true rho is not equal to 0 sample estimates: rho 0.009199129 Warning message: In cor.test.default(x3, y3, method = "spearman") : Cannot compute exact p-value with ties
示例
cor.test(x3,y3,method="spearman",exact=FALSE)
输出
Spearman's rank correlation rho data: x3 and y3 S = 2.0642e+10, p-value = 0.5155 alternative hypothesis: true rho is not equal to 0 sample estimates: rho 0.009199129