# why do we need to sometime jitter the data when using correlation (Spearman) in R

For some reason when I am computing the spearman,

cor.test(data$$ag, data$$co, method = "spearman")


it gives me a warning

Warning message: In cor.test.default(data\$ag, data\$co, method = "spearman") : Cannot compute exact p-value with ties

I googled and people suggested to jitter the data:

ag.jitter <- jitter(data$$ag) co.jitter <- jitter(data$$co)


And now it works without any warning. I wonder what could cause the first warning?

cor.test(ag.jitter, co.jitter, method = "spearman")

• Who suggested to jitter your data? What justification did they give? I would just use exact = FALSE or instead use Kendall's tau (method = "kendall"). – alan ocallaghan Feb 11 at 1:01
• The warning is caused by the fact that you have ties in your data and Spearman is a correlation based on the ranks of the data; the ties imply there is not a unique ordering, and therefore ranking, of the data, among other things. cor.test.default does not calculate an exact p-value in this case. – jbowman Feb 11 at 4:26
• Try calculating a p-value several times on the sane data using jittering each time. What do you notice? ... If you do it a large number of times, what do you notice about the average correlation after jittering? – Glen_b Feb 11 at 10:13

In general, in my opinion, modifying your data to use a statistical test with restrictive assumptions (in this case, no ties) is not advisable when an equivalent non-restrictive statistical method exists. Using a Spearman correlation test without an exact p-value, or using Kendall's $$\tau$$ in place of Spearman's $$\rho$$ are both valid approaches for data with ties.

As the simulation below illustrates, jittering data can induce fairly large changes in the observed p-values. Using a non-exact correlation test or Kendall's $$\tau$$ produce more sensible results.

x <- rnorm(1000, sd = 0.1)
x[1:100] <- x
y <- x + rnorm(1000, sd = 1)
y[200:300] <- y

plot(x, y) cor.test(x, y, method = "spearman")
#> Warning in cor.test.default(x, y, method = "spearman"): Cannot compute exact p-
#> value with ties
#>
#>  Spearman's rank correlation rho
#>
#> data:  x and y
#> S = 152678660, p-value = 0.007922
#> alternative hypothesis: true rho is not equal to 0
#> sample estimates:
#>        rho
#> 0.08392713

pvals <- replicate(1000,
cor.test(
jitter(x),
jitter(y),
method = "spearman")\$p.value
)

hist(pvals, breaks = "FD") cor.test(x, y, method = "spearman", exact = FALSE)
#>
#>  Spearman's rank correlation rho
#>
#> data:  x and y
#> S = 152678660, p-value = 0.007922
#> alternative hypothesis: true rho is not equal to 0
#> sample estimates:
#>        rho
#> 0.08392713
cor.test(x, y, method = "kendall")
#>
#>  Kendall's rank correlation tau
#>
#> data:  x and y
#> z = 2.6233, p-value = 0.008709
#> alternative hypothesis: true tau is not equal to 0
#> sample estimates:
#>        tau
#> 0.05590293