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 n1${n}_{1}$ and n2${n}_{2}$, denoted by:
 x1,x2, … ,xn1 $x1,x2,…,xn1$
and
 xn1 + 1, xn1 + 2, … , xn, $xn1+ 1, xn1+ 2,…, xn,$
where n = n1 + n2$n={n}_{1}+{n}_{2}$.
The hypothesis under test, H0${H}_{0}$, often called the null hypothesis, is that the medians are the same, and this is to be tested against the alternative hypothesis H1${H}_{1}$ that they are different.
The test proceeds by forming a 2 × 2$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 i1${i}_{1}$ i2${i}_{2}$ i1 + i2${i}_{1}+{i}_{2}$ Scores ≥ $\ge$ pooled median n1 − i1${n}_{1}-{i}_{1}$ n2 − i2${n}_{2}-{i}_{2}$ n − (i1 + i2)$n-\left({i}_{1}+{i}_{2}\right)$ Total n1${n}_{1}$ n2${n}_{2}$ n$n$
Under the null hypothesis, H0${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 i1${i}_{1}$, to be about n1 / 2${n}_{1}/2$ and i2${i}_{2}$ to be about n2 / 2${n}_{2}/2$.
nag_nonpar_test_median (g08ac) returns:
 (a) the frequencies i1${i}_{1}$ and i2${i}_{2}$; (b) the probability, p$p$, of observing a table at least as ‘extreme’ as that actually observed, given that H0${H}_{0}$ is true. If n < 40$n<40$, p$p$ is computed directly (‘Fisher's exact test’); otherwise a χ12${\chi }_{1}^{2}$ approximation is used (see nag_stat_contingency_table (g01af)).
H0${H}_{0}$ is rejected by a test of chosen size α$\alpha$ if p < α$p<\alpha$.

## References

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

## Parameters

### Compulsory Input Parameters

1:     x(n) – double array
n, the dimension of the array, must satisfy the constraint n2${\mathbf{n}}\ge 2$.
The first n1${n}_{1}$ elements of x must be set to the data values in the first sample, and the next n2${n}_{2}$ ( = nn1$\text{}={\mathbf{n}}-{n}_{1}$) elements to the data values in the second sample.
2:     n1 – int64int32nag_int scalar
The size of the first sample n1${n}_{1}$.
Constraint: 1n1 < n$1\le {\mathbf{n1}}<{\mathbf{n}}$.

### Optional Input Parameters

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

w

### Output Parameters

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

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

## Example

```function nag_nonpar_test_median_example
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];
n1 = int64(16);
[i1, i2, p, ifail] = nag_nonpar_test_median(x, n1)
```
```

i1 =

13

i2 =

6

p =

8.8086e-04

ifail =

0

```
```function g08ac_example
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];
n1 = int64(16);
[i1, i2, p, ifail] = g08ac(x, n1)
```
```

i1 =

13

i2 =

6

p =

8.8086e-04

ifail =

0

```