The test proceeds as follows:

(a)The pooled sample of all the observations is ranked. Average ranks are assigned to tied scores.

(b)The ranks of the observations in each sample are summed, to give the rank sums ${R}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,k$.

(c)The Kruskal–Wallis' test statistic $H$ is computed as:
i.e., $N$ is the total number of observations. If there are tied scores, $H$ is corrected by dividing by:
where $t$ is the number of tied scores in a group and the summation is over all tied groups.
g08afc returns the value of
$H$, and also an approximation,
$p$, to the probability of a value of at least
$H$ being observed,
${H}_{0}$ is true. (
$H$ approximately follows a
${\chi}_{k1}^{2}$ distribution).
${H}_{0}$ is rejected by a test of chosen size
$\alpha $ if
$p<\alpha \text{.}$ The approximation
$p$ is acceptable unless
$k=3$ and
${l}_{1}$,
${l}_{2}$ or
${l}_{3}\le 5$ in which case tables should be consulted (e.g., O of
Siegel (1956)) or
$k=2$ (in which case the Median test (see
g08acc) or the Mann–Whitney
$U$ test (see
g08amc) is more appropriate).

1:
$\mathbf{k}$ – Integer
Input

On entry: the number of samples, $k$.
Constraint:
${\mathbf{k}}\ge 2$.

2:
$\mathbf{l}\left[{\mathbf{k}}\right]$ – const Integer
Input

On entry: ${\mathbf{l}}\left[\mathit{i}1\right]$ must contain the number of observations ${l}_{\mathit{i}}$ in sample $\mathit{i}$, for $\mathit{i}=1,2,\dots ,k$.
Constraint:
${\mathbf{l}}\left[\mathit{i}1\right]>0$, for $\mathit{i}=1,2,\dots ,k$.

3:
$\mathbf{x}\left[{\mathbf{lx}}\right]$ – const double
Input

On entry: the elements of
x must contain the observations in the
k groups. The first
${l}_{1}$ elements must contain the scores in the first group, the next
${l}_{2}$ those in the second group, and so on.

4:
$\mathbf{lx}$ – Integer
Input

On entry: the total number of observations, $N$.
Constraint:
${\mathbf{lx}}={\sum}_{i=1}^{k}{\mathbf{l}}\left[i1\right]$.

5:
$\mathbf{h}$ – double *
Output

On exit: the value of the Kruskal–Wallis test statistic, $H$.

6:
$\mathbf{p}$ – double *
Output

On exit: the approximate significance, $p$, of the Kruskal–Wallis test statistic.

7:
$\mathbf{fail}$ – NagError *
Input/Output

The NAG error argument (see
Section 7 in the Introduction to the NAG Library CL Interface).
For estimates of the accuracy of the significance
$p$, see
g01ecc. The
${\chi}^{2}$ approximation is acceptable unless
$k=3$ and
${l}_{1},{l}_{2}$ or
${l}_{3}\le 5$.
This example is taken from Moore
et al. Moore et al. (1972). There are 5 groups of sizes
$5$,
$8$,
$6$, 8 and
$8$. The data represent the weight gain, in pounds, of pigs from five different litters under the same conditions.