NAG FL Interface
g08aff (test_​kruskal)

Settings help

FL Name Style:


FL Specification Language:


1 Purpose

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

2 Specification

Fortran Interface
Subroutine g08aff ( x, lx, l, k, w, h, p, ifail)
Integer, Intent (In) :: lx, l(k), k
Integer, Intent (Inout) :: ifail
Real (Kind=nag_wp), Intent (In) :: x(lx)
Real (Kind=nag_wp), Intent (Out) :: w(lx), h, p
C Header Interface
#include <nag.h>
void  g08aff_ (const double x[], const Integer *lx, const Integer l[], const Integer *k, double w[], double *h, double *p, Integer *ifail)
The routine may be called by the names g08aff or nagf_nonpar_test_kruskal.

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:
  1. (a)The pooled sample of all the observations is ranked. Average ranks are assigned to tied scores.
  2. (b)The ranks of the observations in each sample are summed, to give the rank sums Ri, for i=1,2,,k.
  3. (c)The Kruskal–Wallis' test statistic H is computed as:
    H=12N(N+1) i=1kRi2li-3(N+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 Arguments

1: x(lx) Real (Kind=nag_wp) array Input
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 Integer Input
On entry: N, the total number of observations.
Constraint: lx=i=1kl(i).
3: l(k) Integer array Input
On entry: l(i) must contain the number of observations li in sample i, for i=1,2,,k.
Constraint: l(i)>0, for i=1,2,,k.
4: k Integer Input
On entry: k, the number of samples.
Constraint: k2.
5: w(lx) Real (Kind=nag_wp) array Workspace
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 Integer Input/Output
On entry: ifail must be set to 0, −1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of −1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value −1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value 0 is recommended. 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:
ifail=1
On entry, k=value.
Constraint: k2.
ifail=2
On entry, i=value and l(i)=value.
Constraint: l(i)>0.
ifail=3
On entry, il(i)=value and lx=value.
Constraint: il(i)=lx.
ifail=4
On entry, all the observations were equal.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

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 Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
g08aff is not threaded in any implementation.

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

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

10.1 Program Text

Program Text (g08affe.f90)

10.2 Program Data

Program Data (g08affe.d)

10.3 Program Results

Program Results (g08affe.r)