# NAG Library Routine Document

## 1Purpose

g08acf performs the Median test on two independent samples of possibly unequal size.

## 2Specification

Fortran Interface
 Subroutine g08acf ( x, n, n1, w, i1, i2, p,
 Integer, Intent (In) :: n, n1 Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: i1, i2 Real (Kind=nag_wp), Intent (In) :: x(n) Real (Kind=nag_wp), Intent (Out) :: w(n), p
#include nagmk26.h
 void g08acf_ (const double x[], const Integer *n, const Integer *n1, double w[], Integer *i1, Integer *i2, double *p, Integer *ifail)

## 3Description

The Median test investigates the difference between the medians of two independent samples of sizes ${n}_{1}$ and ${n}_{2}$, denoted by:
 $x1,x2,…,xn1$
and
 $xn1+ 1, xn1+ 2,…, xn,$
where $n={n}_{1}+{n}_{2}$.
The hypothesis under test, ${H}_{0}$, often called the null hypothesis, is that the medians are the same, and this is to be tested against the alternative hypothesis ${H}_{1}$ that they are different.
The test proceeds by forming a $2×2$ frequency table, giving the number of scores in each sample above and below the median of the pooled sample:
 Sample 1 Sample 2 Total Scores $<$ pooled median ${i}_{1}$ ${i}_{2}$ ${i}_{1}+{i}_{2}$ Scores $\ge$ pooled median ${n}_{1}-{i}_{1}$ ${n}_{2}-{i}_{2}$ $n-\left({i}_{1}+{i}_{2}\right)$ Total ${n}_{1}$ ${n}_{2}$ $n$
Under the null hypothesis, ${H}_{0}$, we would expect about half of each group's scores to be above the pooled median and about half below, that is, we would expect ${i}_{1}$, to be about ${n}_{1}/2$ and ${i}_{2}$ to be about ${n}_{2}/2$.
g08acf returns:
 (a) the frequencies ${i}_{1}$ and ${i}_{2}$; (b) the probability, $p$, of observing a table at least as ‘extreme’ as that actually observed, given that ${H}_{0}$ is true. If $n<40$, $p$ is computed directly (‘Fisher's exact test’); otherwise a ${\chi }_{1}^{2}$ approximation is used (see g01aff).
${H}_{0}$ is rejected by a test of chosen size $\alpha$ if $p<\alpha$.

## 4References

Siegel S (1956) Non-parametric Statistics for the Behavioral Sciences McGraw–Hill

## 5Arguments

1:     $\mathbf{x}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the first ${n}_{1}$ elements of x must be set to the data values in the first sample, and the next ${n}_{2}$ ($\text{}={\mathbf{n}}-{n}_{1}$) elements to the data values in the second sample.
2:     $\mathbf{n}$ – IntegerInput
On entry: the total of the two sample sizes, $n$ ($\text{}={n}_{1}+{n}_{2}$).
Constraint: ${\mathbf{n}}\ge 2$.
3:     $\mathbf{n1}$ – IntegerInput
On entry: the size of the first sample ${n}_{1}$.
Constraint: $1\le {\mathbf{n1}}<{\mathbf{n}}$.
4:     $\mathbf{w}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayWorkspace
5:     $\mathbf{i1}$ – IntegerOutput
On exit: the number of scores in the first sample which lie below the pooled median, ${i}_{1}$.
6:     $\mathbf{i2}$ – IntegerOutput
On exit: the number of scores in the second sample which lie below the pooled median, ${i}_{2}$.
7:     $\mathbf{p}$ – Real (Kind=nag_wp)Output
On exit: the tail probability $p$ corresponding to the observed dichotomy of the two samples.
8:     $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ 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 argument, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{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:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}<2$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{n1}}<1$, or ${\mathbf{n1}}\ge {\mathbf{n}}$.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

The probability returned should be accurate enough for practical use.

## 8Parallelism and Performance

g08acf is not threaded in any implementation.

The time taken by g08acf is small, and increases with $n$.

## 10Example

This example is taken from page 112 of Siegel (1956). The data relate to scores of ‘oral socialisation anxiety’ in $39$ societies, which can be separated into groups of size $16$ and $23$ on the basis of their attitudes to illness.

### 10.1Program Text

Program Text (g08acfe.f90)

### 10.2Program Data

Program Data (g08acfe.d)

### 10.3Program Results

Program Results (g08acfe.r)

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