如何在 R 中执行 Spearman 相关性检验时避免“无法计算带有重复值的精确 p 值”的警告?
当变量不是连续的,但可以进行排序时,我们不使用皮尔逊相关系数来查找线性关系,在这种情况下,斯皮尔曼相关系数就会派上用场。由于斯皮尔曼相关系数考虑了值的秩,因此相关性检验会忽略相同的秩以查找 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
广告