Sub topic 12
When Assumptions Fail: Non-Parametric Tests
This chapter assumes you can already run a one-sample, two-sample, paired, and one-way ANOVA test. It also assumes you know how to check normality and equal variance, and that you read p-values against a chosen alpha. Here you learn what to do when those checks fail.
What "non-parametric" means
A parametric test, such as the t-test or ANOVA, assumes the data come from a distribution described by a few parameters. For these tests the distribution is normal, described by a mean and a standard deviation. A non-parametric test makes weaker assumptions. Most of them replace each value with its rank (1 for the smallest, 2 for the next, and so on) and work with the ranks.
Ranks have two useful properties:
- An extreme outlier gets only the largest rank. It cannot pull the result the way it pulls a mean.
- Skewed data keep the same rank order whether or not you transform them, so the test does not depend on the shape of the tail.
When to switch
Switch when the evidence points to a real problem, not just because one normality test gave a small p-value.
- Small sample, clearly skewed data. With n under about 15 per group, normality checks have little power, so look at the histogram and normal probability plot as well. If the plot shows a strong skew or outliers you cannot explain away, use a rank-based test.
- Ordinal data. Ratings such as 1 to 5 satisfaction scores have order but no equal spacing, so means are questionable.
- Data with a natural floor. Cycle times, wait times, and defect counts often pile up near zero with a long right tail.
- Large samples. With n of 30 or more per group, the t-test is usually fine despite moderate skew, because the sampling distribution of the mean approaches normal. A tiny p-value from a normality test on 500 points often flags a trivial departure.
A transformation (such as a log) is the other option. It keeps you in the parametric framework but makes results harder to explain to a project team, since the team has to read results on the log scale.
The test map for non-parametric alternatives
| Parametric test | Non-parametric alternative | Minitab name |
|---|---|---|
| One-sample t | One-sample Wilcoxon signed-rank (or sign test) | 1-Sample Wilcoxon, 1-Sample Sign |
| Two-sample t | Mann-Whitney U (Wilcoxon rank-sum) | Mann-Whitney |
| Paired t | Wilcoxon signed-rank on the differences | 1-Sample Wilcoxon on differences |
| One-way ANOVA | Kruskal-Wallis | Kruskal-Wallis |
| One-way ANOVA with unequal shapes | Mood's median test | Mood's Median Test |
Running the tests
The code below simulates right-skewed cycle times, which is typical for process data. It runs the t-test next to the rank-based test so you can see how they compare.
import numpy as np
from scipy import stats
rng = np.random.default_rng(11)
# Two-sample: skewed cycle times (minutes) for old and new method
old = rng.lognormal(mean=2.0, sigma=0.6, size=30)
new = rng.lognormal(mean=1.7, sigma=0.6, size=30)
print("Shapiro old p =", stats.shapiro(old).pvalue)
print("Shapiro new p =", stats.shapiro(new).pvalue)
mw = stats.mannwhitneyu(old, new, alternative="two-sided")
print("Mann-Whitney U =", mw.statistic, "p =", mw.pvalue)
print("Welch t p =", stats.ttest_ind(old, new, equal_var=False).pvalue)
# Effect size: probability a random old value exceeds a random new value
print("P(old > new) =", mw.statistic / (len(old) * len(new)))
# Paired: same 25 units measured before and after a fix
before = rng.lognormal(mean=2.0, sigma=0.5, size=25)
after = before * rng.lognormal(mean=-0.2, sigma=0.2, size=25)
w = stats.wilcoxon(before, after)
print("Wilcoxon signed-rank p =", w.pvalue)
# Three or more groups
g1 = rng.lognormal(2.0, 0.5, 20)
g2 = rng.lognormal(2.1, 0.5, 20)
g3 = rng.lognormal(2.5, 0.5, 20)
h = stats.kruskal(g1, g2, g3)
print("Kruskal-Wallis H =", h.statistic, "p =", h.pvalue)Read the results the same way as before: compare the p-value with alpha, and reject the null hypothesis only if p is at or below alpha. The hypotheses change slightly, as described next.
What the hypotheses really say
Rank tests do not compare means. They test whether values in one group tend to be larger than values in another.
- Mann-Whitney. The null is that the two distributions are the same. The alternative is that one tends to produce larger values. People often report this as "the medians differ," but that reading is only valid when both groups have the same distribution shape and spread. If the shapes differ, the test can reject because of a difference in spread or skew, even when the medians are equal.
- Wilcoxon signed-rank. The null is that the differences are symmetric around zero. This test needs symmetric differences. For a strongly skewed paired difference, use the sign test, which needs no symmetry and only counts how many differences are positive and negative.
- Kruskal-Wallis. The null is that all groups come from the same distribution. A significant result says at least one group differs, but not which one.
The "probability that a random old value exceeds a random new value" in the code is a practical effect size. A value of 0.5 means no difference. A value near 0.7 means the old method is slower about 70% of the time, which is easy to explain to a project team.
Post hoc comparisons after Kruskal-Wallis
After a significant Kruskal-Wallis result, compare the groups pairwise. Dunn's test is the standard method and is available in the scikit-posthocs package. A simple alternative is to run Mann-Whitney on each pair and use a Bonferroni correction, so that each comparison uses alpha divided by the number of pairs. With three groups there are three pairs, so each is tested at 0.05 / 3, or about 0.0167. Skipping the correction inflates the Type I error rate across the set of comparisons.
Trade-offs
- Power. When data really are normal, the Mann-Whitney test has about 95% of the power of the t-test. When data are heavy-tailed or skewed, the rank test can have more power than the t-test. The cost of switching is therefore small, and the risk of using a t-test on bad data can be larger.
- Information lost. Ranks discard the size of differences. Two data sets with the same ranks but very different gaps give the same p-value.
- Estimates. You do not get a confidence interval on the mean. Report medians, the Hodges-Lehmann estimate of the shift, or the effect size above instead.
- Ties. Many repeated values, as in rounded or ordinal data, reduce the accuracy of the p-value. Modern software corrects for ties, but a data set that is mostly ties is better handled with a chi-square test on counts.
- Small samples. With very small n, use exact p-values rather than the normal approximation. In SciPy,
method="exact"is available formannwhitneyuandwilcoxonwhen there are no ties.
Common errors
- Switching tests only because the Shapiro-Wilk p-value was just under 0.05 on a large sample.
- Saying "the medians differ" after Mann-Whitney without checking that the two histograms have a similar shape.
- Using Kruskal-Wallis on repeated measurements of the same units. Use the Friedman test for that design.
- Running a rank test, getting p above alpha, and trying a t-test afterward to see if it "works." Choose the test before looking at the result.
- Reporting only the p-value. Always add a median, an effect size, and the sample size.
Exercise
A team compares repair times (hours) for two service centers, with 12 jobs each. The histograms are strongly right-skewed and each has one extreme outlier. Mann-Whitney gives p = 0.03 with alpha = 0.05, and the two histograms have a similar shape. Which test did the team use and why, and what can they conclude?
Show answer
They used Mann-Whitney because the samples are small, skewed, and contain outliers, so the t-test assumptions are not met. Since p = 0.03 is below 0.05, they reject the null that the two distributions are the same. Because the shapes are similar, they can say the repair-time medians differ, and they should report both medians and an effect size such as P(center A > center B). They should also check whether the difference is large enough to matter in practice.
Exercise
A project has four machines and a skewed measurement. Kruskal-Wallis gives p = 0.002. What is the next step, and how should alpha be handled?
Show answer
The result says at least one machine differs but not which. Run pairwise comparisons with Dunn's test, or Mann-Whitney on all six pairs with a Bonferroni correction. With six pairs, each is tested at 0.05 / 6, or about 0.0083, to keep the overall Type I error near 0.05.