Mann Whitney U Test - Overview

841 Words | May 07, 2019

In this series, I am going to take a journey on Mann-Whitney U test and my Python solution for testing on small sample size. There is going to be an extension for original Mann-Whitney U test in Scipy.

Because the current scipy version only support test for large sample size(n>20), the new script is made to test for small sample size (n < 20).

Part 1: an overview of Mann-Whitney U test

Part 2 : an introduction to Mann-Whitney U test in Python

Use of Mann-Whitney test

The Mann–Whitney U test is a non-parametric alternative test to the independent sample t-test. It is a test of both location and shape. Given two independent samples, it tests whether one variable tends to have values higher than the other (test on sample means). Theoretically, in large samples the Mann-Whitney test can also detect differences in spread even when the medians are very similar.

This test does not assume that the samples is normally distributed, that the variances of the two populations are equal or that the two sample sizes are equal.

Hence, in summary:

  • The Mann-Whitney U test is used as an alternative to a t test when the data are not normally distributed;
  • The Mann-Whitney U test is usually used in cases that data sample size is small;
  • The test can detect differences in shape and spread as well as just differences in medians.

Test Statistic and Calculation Example

Consider a test designed to investigate the effectiveness of a new drug. A total of n=10 participants are randomized to receive either the new drug or a placebo. Participants are asked to record the significance of discomfort (the higher the value, the more uncomfortable). The data are shown below.

Placebo 7 5 6 4 12
New Drug 3 6 4 2 1

The hypothesis is given below and and the test is supposed to run at the 5% level of significance (i.e., ).:

The sample size is small (), so a nonparametric test is appropriate. We are going to use Mann-Whitney U test to solve this problem.

The test statistic for the Mann-Whitney U Test is denoted , whose whose distribution under the null hypothesis is known. In the case of small samples, the distribution is tabulated, but for sample sizes above ~20, approximation using the normal distribution is fairly good.

Small Sample Example

The statistic, , could be easily calculated by hand, especially for small samples.

  1. Assign ranks

    The first step is to rank all the data. To do so we order the data from smallest to largest. This is done on the combined or total sample (i.e., pooling the data from the two treatment groups (n=10)), and assigning ranks from 1 to 10, as follows.

    Sample Value (Ordered)   Ranks  
    Placebo New Drugs Placebo New Drugs
      1   1
      2   2
      3   3
      2   2
    4 4 4.5 4.5
    5   6  
    6 6 7.5 7.5
    7   9  
    12   10  

    For the tie values, we use their mean of ranks.

  2. Sum the ranks for each group

    The second step is to calculate the sum of ranks, , of each group.

    In the placebo group,

    In the new drugs group,

  3. Compute

    Given,

    For this example,

    Thus, the test statistic is .

  4. Compare with critical

    In every test, we must determine whether the observed supports the null hypothesis. This is done following the same approach used in parametric testing. Specifically, we determine a critical value of such that,

    If , we reject in favor of and,

    if we do not reject .

    The table of critical is as follows.

    From this table, we could get the critical and therefore,

    We do not reject and do not have statistically significant evidence at , to show that the two populations are not equal.

When sample size is larger (n > 20)

With samples this large, the value of approaches a normal distribution, and so the null hypothesis can be tested by a Z-test.

  1. Compute

    Here,

  2. Compute the z value of statistic

  3. Determination

    Compare the obtained Z value and the critical Z value to determine whether to retain or reject the null hypothesis. at the 5% level of significance, similarly,

    If , we do not reject and,

    if we reject in favor of .

Python Script for Mann–Whitney U test

The Mann–Whitney U test is included in most modern statistical packages. For instance, in Python, Mann-Whitney U is available in scipy.stat.

However, the scipy version of Mann-Whitney U test noted that

Use only when the number of observation in each sample is > 20.

Because Mann-Whitney U test is very useful in small data sample cases, such as clinical dataset (many clinical trials only have very few samples), a complete application of Mann-Whitney U test, including two conditions of small sample and large sample, was developed.

In Part 2, we go further to have a detailed demo on the new application.


More details could be found:

Tool: Web Application

Package Source Code

Github

Tags:

Categories:

Updated:



LEAVE A COMMENT