当变量不是连续的但可以排序时,我们不使用皮尔逊相关系数来找到线性关系,在这种情况下,斯皮尔曼相关系数就出现了。由于spearman相关系数考虑了值的等级,因此相关性测试忽略了相同的等级来找到p值,结果我们得到警告“无法计算具有联系的精确p值”。可以通过在cor.test函数中使用精确= FALSE来避免这种情况。
请看以下向量并执行spearman相关测试以检查它们之间的关系-
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
在这里,我们有领带的警告,可以通过使用精确= 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