# A Comprehensive Guide to Non\-Parametric Tests: Focus on the Mann\-Whitney U Test

- Canonical: https://33rdsquare.com/a-comprehensive-guide-on-non-parametric-tests/
- Published: 2024-09-03
- Author: Jordan Brown
- Categories: [Artificial Intelligence & Machine Learning & ChatGPT](https://33rdsquare.com/category/tech/ai/)

---

## Introduction

When analyzing data, it‘s important to choose the right statistical test for the job. Parametric tests, such as t-tests and ANOVA, are powerful tools but rely on certain assumptions about the data, namely that it follows a normal distribution. However, real-world data often violates these assumptions. This is where non-parametric tests come in handy.

Non-parametric tests are statistical methods that don‘t make strict assumptions about the distribution of the data. They are useful when your data is not normally distributed, sample sizes are small, or you‘re dealing with ordinal or ranked data. Non-parametric tests focus on the median or rank ordering of observations rather than means and standard deviations.

In this article, we‘ll provide an overview of the most common non-parametric tests, with a special emphasis on the Mann-Whitney U test. We‘ll explain when to use each test, how they work, and demonstrate implementation in Python. By the end, you‘ll have a solid understanding of non-parametric testing and be able to apply these techniques to your own data analysis.

## Overview of Non-Parametric Tests

There are several non-parametric tests available, each suited for different types of data and research questions. Here are some of the most widely used:

**Mann-Whitney U Test:** Used to compare the distributions of two independent groups. It‘s the non-parametric equivalent of the independent samples t-test.

**Wilcoxon Signed-Rank Test:** Compares the distributions of two related samples, such as before and after measurements on the same individuals. It‘s the non-parametric version of the paired samples t-test.

**Kruskal-Wallis Test:** An extension of the Mann-Whitney U test for comparing distributions across three or more independent groups. It‘s the non-parametric alternative to one-way ANOVA.

**Friedman Test:** Compares distributions across three or more related samples. It‘s the non-parametric equivalent of repeated measures ANOVA.

**Spearman‘s Rank Correlation:** Measures the strength and direction of the monotonic relationship between two variables. It‘s a non-parametric alternative to Pearson‘s correlation.

In the next section, we‘ll take a closer look at the Mann-Whitney U test and demonstrate how to use it in practice.

## The Mann-Whitney U Test

The Mann-Whitney U test, also known as the Wilcoxon rank-sum test, is used to determine whether there are differences in the distributions of two independent groups. It tests the null hypothesis that the distribution of the dependent variable is the same across the two groups.

The test works by ranking all observations from both groups in ascending order and calculating a statistic called U based on the ranks. If the null hypothesis is true, the distributions of ranks should be similar between the two groups.

**When to use the Mann-Whitney U Test:**

- Your dependent variable is ordinal, interval, or ratio data
- Your independent variable consists of two categorical, independent groups
- You want to compare the distributions of the two groups
- Data in each group doesn‘t follow a normal distribution or sample sizes are small

**The Mann-Whitney U Statistic:**

The Mann-Whitney U test statistic is calculated as:

U = n1 * n2 + (n1(n1+1))/2 – R1

Where:

- n1 = sample size of first group
- n2 = sample size of second group
- R1 = sum of ranks in first group

The formula calculates the number of times an observation from one group precedes an observation from the other group. A small U value indicates that the two groups have different distributions.

**Implementing the Mann-Whitney U Test in Python:**

Let‘s walk through an example of using the Mann-Whitney U test in Python with the scipy library. Suppose we want to compare the distributions of test scores between two classes:

```
from scipy.stats import mannwhitneyu

# Test scores for each class
class1 = [80, 75, 90, 62, 95, 78, 65, 72, 88, 83]
class2 = [68, 82, 73, 79, 84, 71, 97, 75, 69, 86]

# Perform Mann-Whitney U test
statistic, p_value = mannwhitneyu(class1, class2)

print(f‘Mann-Whitney U Statistic = {statistic:.4f}, p-value = {p_value:.4f}‘)

if p_value < 0.05:
    print("Reject null hypothesis: Significant difference between class distributions")
else:
    print("Fail to reject null hypothesis: No significant difference between distributions")
```

Output:

```
Mann-Whitney U Statistic = 41.0000, p-value = 0.4963
Fail to reject null hypothesis: No significant difference between distributions
```

Here, we first imported the mannwhitneyu function from scipy.stats. We then defined two lists containing the test scores for each class. To perform the test, we simply passed the two lists into mannwhitneyu().

The function returns the test statistic U and the associated p-value. The p-value represents the probability of observing test results at least as extreme as the results actually observed, under the assumption that the null hypothesis is true.

In this case, with a p-value of 0.4963, we fail to reject the null hypothesis at a typical 0.05 significance level. We don‘t have sufficient evidence to conclude the score distributions differ significantly between the classes.

## Testing for Normality

Before deciding whether to use a parametric or non-parametric test, it‘s often useful to check if your data follows a normal distribution. Some common methods for assessing normality include:

**Shapiro-Wilk Test:**
 Tests the null hypothesis that the data was drawn from a normal distribution. Provides a W statistic that small values indicate your sample is not normally distributed.

**Anderson-Darling Test:**
 A modification of the Kolmogorov-Smirnov test that gives more weight to the tails. The null hypothesis is that the data follow a specified distribution (normal by default). Larger values indicate less normality.

**Kolmogorov-Smirnov Test:**
 Compares your sample distribution with a reference probability distribution (normal by default). Larger D values indicate your sample distribution is less similar to the reference.

Here‘s how to conduct these normality tests in Python:

```
from scipy.stats import shapiro, anderson, kstest

data = [80, 75, 90, 62, 95, 78, 65, 72, 88, 83]

# Shapiro-Wilk test
stat, p = shapiro(data)
print(f‘Shapiro-Wilk statistic={stat:.3f}, p={p:.3f}‘)

# Anderson-Darling test
result = anderson(data)
print(f‘Anderson-Darling statistic={result.statistic:.3f}‘)
p = result.significance_level[result.statistic > result.critical_values]
print(f‘     significance level={p}‘)

# Kolmogorov-Smirnov test
stat, p = kstest(data, ‘norm‘)
print(f‘Kolmogorov-Smirnov statistic={stat:.3f}, p={p:.3f}‘)
```

Output:

```
Shapiro-Wilk statistic=0.952, p=0.679
Anderson-Darling statistic=0.326
     significance level=0.45
Kolmogorov-Smirnov statistic=0.158, p=0.817
```

For each test, we get a test statistic and p-value. Higher p-values (typically >0.05) indicate the data is probably normal, while lower p-values suggest the data is likely not normal. Here, all three tests indicate the sample data likely comes from a normal distribution. If that wasn‘t the case, we‘d want to consider using non-parametric methods instead.

## Interpreting Results and Drawing Conclusions

Once you‘ve conducted a non-parametric test, you need to interpret the results. The key value to look at is usually the p-value, which indicates the probability of observing the results given the null hypothesis is true. A small p-value (typically <0.05) suggests you should reject the null hypothesis in favor of the alternative.

However, don‘t just blindly adhere to the 0.05 threshold. Consider the practical significance of the effect size, the consequences of Type I and Type II errors, and any limitations of your analysis. Use the p-value as a guide, but don‘t let it completely override critical thinking about your results.

Also remember that non-parametric tests are less powerful than their parametric counterparts when assumptions are met. If you have normally distributed data, consider using parametric methods for greater power to detect effects.

## Frequently Asked Questions

**Q: What are the assumptions of non-parametric tests?**

A: Non-parametric tests have fewer assumptions than parametric tests. They don‘t assume data follows any specific distribution. However, some assumptions still apply:

- Samples are independent
- The dependent variable is ordinal, interval, or ratio
- Within a group, observations are independent and identically distributed
- The distributions in each group have roughly the same shape and spread

Always check your data meets the assumptions of your intended non-parametric test.

**Q: How do I choose between parametric and non-parametric tests?**

A: Consider the following factors when deciding between a parametric and non-parametric test:

- Types of variables: Non-parametric methods are usually used for ordinal, interval, or ratio dependent variables with a categorical independent variable.
- Normality: If your data is normally distributed, parametric tests are more powerful. If not, consider non-parametric methods.
- Sample size: Non-parametric tests are often used when you have small sample sizes (<30 per group) since normality is harder to assess.
- Hypotheses: Parametric tests compare group means, while non-parametric tests compare group medians or probability distributions. Choose the test that matches your research question.

When in doubt, you can run both parametric and non-parametric tests. If they agree, you can be confident in your conclusions. If they disagree, investigate further.

**Q: Can non-parametric tests be used for more than two groups?**

A: Yes, the Kruskal-Wallis test extends the Mann-Whitney U test for comparing three or more independent groups. It‘s the non-parametric version of one-way ANOVA. For three or more related groups, use the Friedman test, which is the non-parametric alternative to repeated measures ANOVA.

## Conclusion

Non-parametric tests are valuable tools to have in your data analysis toolkit. They provide valid options for hypothesis testing when the assumptions of parametric methods aren‘t met. The Mann-Whitney U test for comparing distributions between two independent groups is a versatile and widely used non-parametric method.

However, non-parametric tests aren‘t always the answer. When you have normally distributed data, parametric tests provide greater power. Choosing the right test requires careful consideration of your variables, assumptions, hypotheses, and research context.

We covered a lot in this guide, but there‘s still more to learn. Additional non-parametric techniques to explore include Spearman correlation, chi-square tests, and bootstrapping methods. The field of non-parametric statistics is an active area of research with new developments happening all the time.

The key is getting hands-on experience with real data. Use the code examples provided here as a starting point and try applying non-parametric tests to your own datasets. Over time you‘ll build an intuition for when and how to use these powerful techniques. Happy analyzing!

## Additional Resources

To learn more about non-parametric testing, check out these resources:

- [Scipy documentation on statistical functions](https://docs.scipy.org/doc/scipy/reference/stats.html#statistical-functions)
- [Machine Learning Mastery tutorial on non-parametric tests in Python](https://machinelearningmastery.com/nonparametric-statistical-significance-tests-in-python/)
- [NIST Handbook on non-parametric methods](https://www.itl.nist.gov/div898/handbook/prc/section2/prc2.htm)
- [Nonparametric Statistical Methods textbook](https://www.amazon.com/Nonparametric-Statistical-Methods-Myles-Hollander/dp/0470387378)

---

Source: [A Comprehensive Guide to Non\-Parametric Tests: Focus on the Mann\-Whitney U Test](https://33rdsquare.com/a-comprehensive-guide-on-non-parametric-tests/)
