Book contents

STATHARBOR ANALYTICS · INTRODUCTION TO STATISTICS · CHAPTER 15

Nonparametric Methods

Parametric tests rely on assumptions such as normality and equal variances. When these assumptions are questionable, nonparametric (distribution–free) methods provide robust alternatives. This chapter introduces common nonparametric tests for comparing medians and distributions, rank–based correlation, and resampling techniques.

Sign and Wilcoxon Signed–Rank Tests

For paired or one–sample problems, the sign test and Wilcoxon signed–rank test assess whether the median difference is zero. Suppose D1,…,DnD_1,\dots,D_n are paired differences. The sign test counts the number of positive differences SS; under the null hypothesis, S∼Binomial(n,0.5)S \sim \mathrm{Binomial}(n, 0.5). The Wilcoxon signed–rank test uses the sum of ranks of the absolute differences with signs.

Example: Median Change in Blood Pressure

Twenty patients’ blood pressures are recorded before and after a treatment. We compute differences and test whether the median change is zero.

Worked example

Wilcoxon signed–rank test in R and Python.

before <- c(140, 150, 138, 145, 155, 160, 148, 152, 149, 143)
after  <- c(135, 148, 135, 142, 150, 158, 145, 150, 146, 141)
diff   <- after - before
wilcox.test(diff)
import numpy as np
from scipy.stats import wilcoxon

before = np.array([140,150,138,145,155,160,148,152,149,143])
after  = np.array([135,148,135,142,150,158,145,150,146,141])
diff   = after - before
stat, p_val = wilcoxon(diff)
print(stat, p_val)

Both tests return the test statistic and p–value. A small p–value indicates evidence against the null hypothesis of zero median change.

Mann–Whitney and Kruskal–Wallis Tests

For comparing two independent samples without assuming normality, the Mann–Whitney UU test (also called Wilcoxon rank–sum) compares the distributions by ranking all observations. The Kruskal–Wallis test generalises to more than two groups and is analogous to one–way ANOVA. These tests analyse whether group distributions differ in location.

Spearman’s Rank Correlation

When relationships are monotonic but not linear, Spearman’s rank correlation measures association using ranks rather than raw values. The Spearman coefficient is ρs=1−6∑di2n(n2−1),\rho_s = 1 - \frac{6\sum d_i^2}{n(n^2-1)}, where did_i are the differences between the ranks of XX and YY. In R, use cor(x,y, method="spearman"); in Python, use scipy.stats.spearmanr.

Permutation Tests

Permutation tests provide exact p–values by computing the distribution of a test statistic under all permutations of labels. They are applicable to difference of means, correlation and other statistics. The algorithm involves:

  1. Computing the observed statistic (e.g., difference of means).

  2. Randomly permuting group labels many times (or enumerating all permutations), calculating the statistic for each permutation.

  3. Estimating the p–value as the proportion of permutations with statistic at least as extreme as the observed.

Exercises

Practice

  1. Compare the median incomes of two regions using the Mann–Whitney test. Simulate data if necessary. How does the result compare to a two–sample t–test?

  2. Use the Kruskal–Wallis test to compare three rehabilitation programs on a measure of improvement. Follow up with pairwise Mann–Whitney tests with Bonferroni correction.

  3. Compute Spearman’s rank correlation between rankings of universities by students and by employers. Interpret the result. Use code in R/Python.

  4. Implement a permutation test in Python or R to compare the means of two samples of size 10. Estimate the p–value and compare with the t–test p–value.

References