Published

R’s exact Kendall test can return a negative p-value

With 20 perfectly concordant pairs, R’s cor.test() returns an exact two-sided p-value of −4.44e−16. Reversing one variable returns the correct value of 8.22e−19.

With 20 perfectly concordant pairs, R returns a negative p-value from its exact two-sided Kendall test. Reversing one variable gives the correct positive probability. Both arrangements have the same exact two-sided p-value.

Reproduce the result

x <- seq_len(20L)
c(
  concordant = cor.test(x, x, method = "kendall")$p.value,
  discordant = cor.test(x, rev(x), method = "kendall")$p.value,
  exact_reference = 2 / factorial(length(x))
)

The report records the same result in R 4.6.2 and R-devel r90881 on Ubuntu x86-64. The concordant case returns approximately −4.44e−16. The discordant case matches the exact reference, approximately 8.22e−19. Under the null, the 20! rank permutations are equally likely. One gives perfect concordance and one gives perfect discordance, so the two-sided probability for either extreme is 2 / 20!.

In the reported example, the printed result displays p-value < 2.2e-16. Read $p.value to see the negative value passed to downstream code. Taking its logarithm, as in a −log10(p) plot, produces NaN.

Why it happens

By default, cor.test() uses the exact Kendall calculation for fewer than 50 finite pairs with no ties. In the affected code, it computes the upper tail by subtracting a cumulative probability close to 1 from 1. Rounding can erase the small probability or push the cumulative value above 1, producing a negative result after subtraction. The report also documents zero results and a negative result at Kendall’s tau of about 0.769 with 40 pairs.

Upstream report

Tasuku Kobayashi filed R Bugzilla #19319 on November 14, 2026. The report proposes computing the upper tail directly from the opposite tail by symmetry and capping the cumulative probability at 1. It includes a patch and a regression test. Bugzilla listed the report as unconfirmed at publication.