G08AFF (PDF version)
G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

NAG Library Routine Document


Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

G08AFF performs the Kruskal–Wallis one-way analysis of variance by ranks on k independent samples of possibly unequal sizes.

2  Specification

REAL (KIND=nag_wp)  X(LX), W(LX), H, P

3  Description

The Kruskal–Wallis test investigates the differences between scores from k independent samples of unequal sizes, the ith sample containing li observations. The hypothesis under test, H0, often called the null hypothesis, is that the samples come from the same population, and this is to be tested against the alternative hypothesis H1 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 Ri, for i=1,2,,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.
G08AFF returns the value of H, and also an approximation, p, to the probability of a value of at least H being observed, H0 is true. (H approximately follows a χk-12 distribution). H0 is rejected by a test of chosen size α if p<α. The approximation p is acceptable unless k=3 and l1, l2 or l35 in which case tables should be consulted (e.g., O of Siegel (1956)) or k=2 (in which case the Median test (see G08ACF) or the Mann–Whitney U test (see G08AHF) is more appropriate).

4  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

5  Parameters

1:     X(LX) – REAL (KIND=nag_wp) arrayInput
On entry: the elements of X must contain the observations in the K samples. The first l1 elements must contain the scores in the first sample, the next l2 those in the second sample, and so on.
2:     LX – INTEGERInput
On entry: N, the total number of observations.
Constraint: LX=i=1kLi.
3:     L(K) – INTEGER arrayInput
On entry: Li must contain the number of observations li in sample i, for i=1,2,,k.
Constraint: Li>0, for i=1,2,,k.
4:     K – INTEGERInput
On entry: k, the number of samples.
Constraint: K2.
5:     W(LX) – REAL (KIND=nag_wp) arrayWorkspace
6:     H – REAL (KIND=nag_wp)Output
On exit: the value of the Kruskal–Wallis test statistic, H.
7:     P – REAL (KIND=nag_wp)Output
On exit: the approximate significance, p, of the Kruskal–Wallis test statistic.
8:     IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ or ​1. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
On entry,K<2.
On entry,Li0 for some i, i=1,2,,k.
On entry,LXi=1kLi.
On entry,all the observations were equal.

7  Accuracy

For estimates of the accuracy of the significance p, see G01ECF. The χ2 approximation is acceptable unless k=3 and l1,l2 or l35.

8  Further Comments

The time taken by G08AFF is small, and increases with N and k.
If k=2, the Median test (see G08ACF) or the Mann–Whitney U test (see G08AHF) is more appropriate.

9  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.

9.1  Program Text

Program Text (g08affe.f90)

9.2  Program Data

Program Data (g08affe.d)

9.3  Program Results

Program Results (g08affe.r)

G08AFF (PDF version)
G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012