Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_nonpar_test_kruskal (g08af)

## Purpose

nag_nonpar_test_kruskal (g08af) performs the Kruskal–Wallis one-way analysis of variance by ranks on $k$ independent samples of possibly unequal sizes.

## Syntax

[h, p, ifail] = g08af(x, l, 'lx', lx, 'k', k)
[h, p, ifail] = nag_nonpar_test_kruskal(x, l, 'lx', lx, 'k', k)

## Description

The Kruskal–Wallis test investigates the differences between scores from $k$ independent samples of unequal sizes, the $i$th sample containing ${l}_{i}$ observations. The hypothesis under test, ${H}_{0}$, often called the null hypothesis, is that the samples come from the same population, and this is to be tested against the alternative hypothesis ${H}_{1}$ that they come from different populations.
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:
 $H=12NN+1 ∑i=1kRi2li-3N+1, where ​N=∑i=1kli,$
i.e., $N$ is the total number of observations. If there are tied scores, $H$ is corrected by dividing by:
 $1-∑t3-t N3-N$
where $t$ is the number of tied scores in a sample and the summation is over all tied samples.
nag_nonpar_test_kruskal (g08af) 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 }_{k-1}^{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 nag_nonpar_test_median (g08ac)) or the Mann–Whitney $U$ test (see nag_nonpar_test_mwu (g08ah)) is more appropriate).

## References

Moore P G, Shirley E A and Edwards D E (1972) Standard Statistical Calculations Pitman
Siegel S (1956) Non-parametric Statistics for the Behavioral Sciences McGraw–Hill

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{x}\left({\mathbf{lx}}\right)$ – double array
The elements of x must contain the observations in the k samples. The first ${l}_{1}$ elements must contain the scores in the first sample, the next ${l}_{2}$ those in the second sample, and so on.
2:     $\mathrm{l}\left({\mathbf{k}}\right)$int64int32nag_int array
${\mathbf{l}}\left(\mathit{i}\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}\right)>0$, for $\mathit{i}=1,2,\dots ,k$.

### Optional Input Parameters

1:     $\mathrm{lx}$int64int32nag_int scalar
Default: the dimension of the array x.
$N$, the total number of observations.
Constraint: ${\mathbf{lx}}=\sum _{i=1}^{k}{\mathbf{l}}\left(i\right)$.
2:     $\mathrm{k}$int64int32nag_int scalar
Default: the dimension of the array l.
$k$, the number of samples.
Constraint: ${\mathbf{k}}\ge 2$.

### Output Parameters

1:     $\mathrm{h}$ – double scalar
The value of the Kruskal–Wallis test statistic, $H$.
2:     $\mathrm{p}$ – double scalar
The approximate significance, $p$, of the Kruskal–Wallis test statistic.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{k}}<2$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{l}}\left(i\right)\le 0$ for some $i$, $i=1,2,\dots ,k$.
${\mathbf{ifail}}=3$
 On entry, ${\mathbf{lx}}\ne \sum _{i=1}^{k}{\mathbf{l}}\left(i\right)$.
${\mathbf{ifail}}=4$
 On entry, all the observations were equal.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

For estimates of the accuracy of the significance $p$, see nag_stat_prob_chisq (g01ec). The ${\chi }^{2}$ approximation is acceptable unless $k=3$ and ${l}_{1},{l}_{2}$ or ${l}_{3}\le 5$.

The time taken by nag_nonpar_test_kruskal (g08af) is small, and increases with $N$ and $k$.
If $k=2$, the Median test (see nag_nonpar_test_median (g08ac)) or the Mann–Whitney $U$ test (see nag_nonpar_test_mwu (g08ah)) is more appropriate.

## Example

This example is taken from 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.
```function g08af_example

fprintf('g08af example results\n\n');

x = [23; 27; 26; 19; 30;
29; 25; 33; 36; 32; 28; 30; 31;
38; 31; 28; 35; 33; 36;
30; 27; 28; 22; 33; 34; 34; 32;
31; 33; 31; 28; 30; 24; 29; 30];
l = [int64(5);8;6;8;8];

fprintf('Kruskal-Wallis test\n\n');
fprintf('Data values\n\n');
fprintf('  Group    Observations');

ix = 1;
for j = 1:numel(l)
fprintf('\n%5d    ', j);
fprintf('%4.0f', x(ix:ix+l(j)-1));
ix = ix + l(j);
end

[h, p, ifail] = g08af(x, l);

fprintf('\n\nTest statistic        %8.3f\n', h);
fprintf('Degrees of freedom    %4d\n', numel(l)-1);
fprintf('Significance          %8.3f\n', p);

```
```g08af example results

Kruskal-Wallis test

Data values

Group    Observations
1      23  27  26  19  30
2      29  25  33  36  32  28  30  31
3      38  31  28  35  33  36
4      30  27  28  22  33  34  34  32
5      31  33  31  28  30  24  29  30

Test statistic          10.537
Degrees of freedom       4
Significance             0.032
```