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_median (g08ac)

## Purpose

nag_nonpar_test_median (g08ac) performs the Median test on two independent samples of possibly unequal size.

## Syntax

[i1, i2, p, ifail] = g08ac(x, n1, 'n', n)
[i1, i2, p, ifail] = nag_nonpar_test_median(x, n1, 'n', n)

## Description

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$.
nag_nonpar_test_median (g08ac) 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 nag_stat_contingency_table (g01af)).
${H}_{0}$ is rejected by a test of chosen size $\alpha$ if $p<\alpha$.

## References

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
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:     $\mathrm{n1}$int64int32nag_int scalar
The size of the first sample ${n}_{1}$.
Constraint: $1\le {\mathbf{n1}}<{\mathbf{n}}$.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array x.
The total of the two sample sizes, $n$ ($\text{}={n}_{1}+{n}_{2}$).
Constraint: ${\mathbf{n}}\ge 2$.

### Output Parameters

1:     $\mathrm{i1}$int64int32nag_int scalar
The number of scores in the first sample which lie below the pooled median, ${i}_{1}$.
2:     $\mathrm{i2}$int64int32nag_int scalar
The number of scores in the second sample which lie below the pooled median, ${i}_{2}$.
3:     $\mathrm{p}$ – double scalar
The tail probability $p$ corresponding to the observed dichotomy of the two samples.
4:     $\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{n}}<2$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{n1}}<1$, or ${\mathbf{n1}}\ge {\mathbf{n}}$.
${\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

The probability returned should be accurate enough for practical use.

The time taken by nag_nonpar_test_median (g08ac) is small, and increases with $n$.

## Example

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.
```function g08ac_example

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

x = [13;  6; 12;  7; 12;  7; 10;  7;
10;  7; 10;  7; 10;  8;  9;  8;
17;  6; 16;  8; 15;  8; 15; 10;
15; 10; 14; 10; 14; 11; 14; 11;
13; 12; 13; 12; 13; 12; 12];

n  = numel(x);
n1 = int64(16);
n2 = n-n1;

fprintf('Median test\n\n');
fprintf('Data values\n\n');

fprintf('     Group 1 ');
for j = 1:floor(n1/8)
i1 = (j-1)*8 + 1;
i2 = min(n1,i1+7);
fprintf('%4.0f',x(i1:i2));
fprintf('\n             ');
end
fprintf('\n     Group 2 ');
for j = 1:floor(n2/8)
i1 = (j-1)*8 + 1;
i2 = min(n2,i1+7);
fprintf('%4.0f',x(i1+n1:i2+n1));
fprintf('\n             ');
end

[i1, i2, p, ifail] = g08ac( ...
x, n1);

fprintf('\n%6d scores below median in group 1\n',i1);
fprintf('%6d scores below median in group 2\n\n',i2);
fprintf('     Significance %10.5f\n',p);

```
```g08ac example results

Median test

Data values

Group 1   13   6  12   7  12   7  10   7
10   7  10   7  10   8   9   8

Group 2   17   6  16   8  15   8  15  10
15  10  14  10  14  11  14  11
13  12  13  12  13  12  12

13 scores below median in group 1
6 scores below median in group 2

Significance    0.00088
```