# How to avoid the warning “Cannot compute exact p-value with ties” while perform correlation test for Spearman’s correlation in R?

R ProgrammingServer Side ProgrammingProgramming

When the variables are not continuous but could be ranked then we do not use pearson correlation coefficient to find the linear relationship, in this case spearman correlation coefficient comes into the scene. Since the spearman correlation coefficient considers the rank of values, the correlation test ignores the same ranks to find the p-values as a result we get the warning “Cannot compute exact p-value with ties”. This can be avoided by using exact = FALSE inside the cor.test function.

## Example

Consider the below vectors and perform spearman correlation test to check the relationship between them −

Live Demo

x1<-rpois(20,2)
y1<-rpois(20,5)
cor.test(x1,y1,method="spearman")

## Output

   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

Here, we have the warning for ties, this can be avoided by using exact=FALSE as shown below −

## Example

cor.test(x1,y1,method="spearman",exact=FALSE)

## Output

   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

Let’s have a look at some more examples −

## Example

Live Demo

x2<-sample(1:100,500,replace=TRUE)
y2<-sample(1:50,500,replace=TRUE)
cor.test(x2,y2,method="spearman")

## Output

   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

## Example

cor.test(x2,y2,method="spearman",exact=FALSE)

## Output

   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

## Example

Live Demo

x3<-sample(101:110,5000,replace=TRUE)
y3<-sample(501:510,5000,replace=TRUE)
cor.test(x3,y3,method="spearman")

## Output

   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

## Example

cor.test(x3,y3,method="spearman",exact=FALSE)

## Output

   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
Updated on 08-Sep-2020 14:45:45