# NAG CL Interfaceg08afc (test_​kruskal)

## 1Purpose

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

## 2Specification

 #include
 void g08afc (Integer k, const Integer l[], const double x[], Integer lx, double *h, double *p, NagError *fail)
The function may be called by the names: g08afc, nag_nonpar_test_kruskal or nag_kruskal_wallis_test.

## 3Description

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:
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 ${R}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,k$.
3. (c)The Kruskal–Wallis' test statistic $H$ is computed as:
 $H = 12 N N+1 ∑ i=1 k R i 2 l i - 3 N+1 , where N = ∑ i=1 k l i ,$
i.e., $N$ is the total number of observations. If there are tied scores, $H$ is corrected by dividing by:
 $1 - ∑ t 3 - t N 3 - N$
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 }_{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 g08acc) or the Mann–Whitney $U$ test (see g08amc) is more appropriate).
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

## 5Arguments

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[i-1\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).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_ARRAY_CONS
The contents of array l are not valid.
Constraint: ${\mathbf{l}}\left[\mathit{i}-1\right]>0$, for $\mathit{i}=1,2,\dots ,k$.
NE_INT
On entry, ${\mathbf{lx}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{lx}}={\sum }_{\mathit{i}=1}^{k}{\mathbf{l}}\left[\mathit{i}-1\right]$, for $\mathit{i}=1,2,\dots ,k$.
NE_INT_ARG_LT
On entry, ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{k}}\ge 2$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_X_IDEN
On entry, all elements of x are equal.

## 7Accuracy

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

## 8Parallelism and Performance

g08afc is not threaded in any implementation.

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

## 10Example

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.

### 10.1Program Text

Program Text (g08afce.c)

### 10.2Program Data

Program Data (g08afce.d)

### 10.3Program Results

Program Results (g08afce.r)