# 17.5.7.2 Algorithms (Moods Median Test)

The procedure below draws on NAG algorithms.

The median test investigates the difference between $K\,\!$ samples of sizes $n_1,n_2,...,n_k\,\!$ denoted by $x_1,x_2,....x_{n_1};x_{n_1+1},x_{n_1+2},...,x_{n_1+n_2};...;x_{n_1+n_2+...+n_{i-1}+1},...x_{n_1+n_2+...+n_i}$

If the median value is not given by the user, the combined data from all groups are sorted and the median is calculated: $md=(x_{(n/2)}+x_{(n/2+1)})/2\,\!$ ,if n is even; $md=x_{((n+1)/2)}\,\!$, if n is odd.

Where $n= \sum_{i=1}^k n_i$, $x_{(1)},...,x_{(n)}\,\!$ is the ordered data of all observations from small to large.

The test proceeds by forming a frequency table, giving the number of scores in each sample above and below the median of the pooled sample:

 Sample 1 Sample 2 …… Sample K Total $Score \le md$ $n_{11}\,\!$ $n_{12}\,\!$ $n_{1k}\,\!$ $R_{1}\,\!$ $Score > md\,\!$ $n_{21}\,\!$ $n_{22}\,\!$ $n_{2k}\,\!$ $R_{2}\,\!$ Total $n_{1}\,\!$ $n_{2}\,\!$ $n_{k}\,\!$ $n\,\!$

The $x^2\,\!$statistic foe all nonempty samples is calculated as: $x^2=\sum_{j=1}^k\sum_{i=1}^2(n_{ij}-e_{ij})^2/e_{ij}\,\!$ where $e_{ij}=R_in_j/n\,\!$

The significance level is from the $x^2\,\!$ distribution with $k-1\,\!$ degrees of freedom, where $k-1\,\!$ is the number of nonempty samples. A message is printed if any cell has an expected value less than one, or more than 20% of the cells have expected values less than five.