Skip to content

CAssociation · Guide test 078

Spearman rank correlation test

Tests rank association and is commonly used to detect monotonic relationships.

Runs in CodeSplashPython example validatedR example validated

Do it in CodeSplash

In the app, set it up in Analyze with one of these methods:

In Correlation test (Pearson / Spearman / Kendall), choose Coefficient = Spearman ρ (ranks).

Do it in CodeSplash Run this test on your own data, in Python or R.

Bench

Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.

Bench · Spearman rank correlation test

30
0.40
0.050

Alternative

Your data

Simulated with a known ρ

If H₀ were true

S = 2696
  1. The reference: how S varies across samples when H₀ is true.
  2. Your statistic, from the data above.
  3. The p-value: the total probability of outcomes no more likely than yours (p = .029).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

ρs=cor⁡(R(x),R(y)),S=n3−n6 (1−ρs)\rho_s = \operatorname{cor}\big(R(x), R(y)\big),\qquad S = \dfrac{n^3-n}{6}\,(1-\rho_s)

With your numbers

ρs=0.40,S=2696,n=30\rho_s = 0.40,\qquad S = 2696,\qquad n = 30
R(x),R(y)R(x), R(y)
the ranks of each variable
ρs\rho_s
Pearson's r computed on the ranks
SS
R's statistic: small S means a positive association

The verdict

S = 2696, p = .029, ρ = 0.40

p ≤ α = .050: reject H₀

Effect size: Spearman correlation ρ = 0.400

Ranks make it insensitive to outliers and to any monotone transformation. R tests S against its exact null distribution when there are no ties, and uses a t approximation otherwise. This bench follows R's defaults; the CodeSplash method always reports the t approximation (R's exact = FALSE).

Statistic
2696.000
p-value
0.0292
Spearman rank correlation test on 30 (x, y) pairs: S = 2696, p = .029, ρ = 0.40. The p-value is below α = .050, so the data are hard to reconcile with H0.

Spearman rank correlation test on 30 (x, y) pairs: S = 2696, p = .029, ρ = 0.40. The p-value is below α = .050, so the data are hard to reconcile with H₀.

Question and hypotheses

H0: no rank association; a permutation test formally tests independence.

Assumptions

Independent paired observations with at least ordinal measurements. Exact permutation calibration requires exchangeability under independence.

Formula

rs=Corr⁡(rank⁡(X),rank⁡(Y))r_s=\operatorname{Corr}(\operatorname{rank}(X),\operatorname{rank}(Y))

Parameters and notation

Use midranks for ties. Without ties rs=1−6Σdi²/[n(n²−1)], where di is the paired rank difference.

Structure of the method

Replace both variables by ranks, using a defined tie rule, and compute Pearson correlation of those ranks. An exact or Monte Carlo pairing permutation yields an independence reference; commonly used t approximations instead provide large-sample calibration for the rank coefficient.

Estimated and nuisance parameters

The empirical rank transform and rank correlation are estimated from the observed pairs; no parametric marginal means or variances are assumed. Tie-group sizes influence the statistic and null distribution. Sample size is the number of usable pairs after a documented missing-data rule.

Settings and design choices

Choose direction, tie handling, asymptotic versus permutation calibration, and the permutation budget. The ten-pair examples illustrate asymptotic calls rather than a recommended exact analysis. A permutation count sets p-value resolution and Monte Carlo precision; it is not a model regularization parameter.

Fitting and adaptation

Recompute ranks if observations change. For a pairing permutation under independence, keep each variable vector intact and permute their correspondence; precomputed ranks can then be reordered. Block or time structure needs a justified restricted scheme, not ordinary free permutations. No fine-tuning stage applies.

Sensitivity and robustness

Heavy ties can reduce information and alter calibration. Monotone transformations preserve rankings, but thresholding and coarse rounding need not. A U-shaped dependence can have weak rank correlation; nonsignificance is not a general independence conclusion. Many-variable screening requires multiplicity control.

How to use and calibrate

Use permutation inference for smaller samples or a justified asymptotic approximation; state tie treatment.

Example application

Assess whether increasing exposure is associated with increasing ordered response.

Interpretation and pitfalls

Watch out

It measures monotonic association, not every possible relationship. An asymptotic t approximation is not exact for ranks.

What changed in edition 2

  • New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.

Python and R

Paired observations, two-sided association test. Ties and exact-versus-asymptotic settings can cause small cross-language differences.

Python · native

Validated — Executed successfully in the validation environment · needs numpy, scipy

import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
x = np.array([4.2,5.1,5.4,4.8,6.0,5.8,4.9,5.6,6.2,5.0])
y = np.array([4.0,4.4,4.8,5.0,5.1,4.3,4.7,4.6,5.2,4.9])
print(stats.spearmanr(x,y))
Output
SignificanceResult(statistic=np.float64(0.406060606060606), pvalue=np.float64(0.24428229408662647))
R

Validated — Executed successfully in the validation environment · needs Base/standard library

x <- c(4.2,5.1,5.4,4.8,6.0,5.8,4.9,5.6,6.2,5.0)
y <- c(4.0,4.4,4.8,5.0,5.1,4.3,4.7,4.6,5.2,4.9)
print(cor.test(x,y,method="spearman",exact=FALSE))
Output
	Spearman's rank correlation rho

data:  x and y
S = 98, p-value = 0.2443
alternative hypothesis: true rho is not equal to 0
sample estimates:
      rho 
0.4060606

Sources

Statistics guide v4 (historical):
R: Executed successfully in the validation environment
Reference's record:
Edition 2 review: Python: executed · R: not executed (Rscript unavailable). Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.

From the CodeSplash statistics guide (v4), entry 078, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT078.