hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_nonpar_test_median (g08ac)


    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example


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


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


The Median test investigates the difference between the medians of two independent samples of sizes n1 and n2, denoted by:
xn1+ 1, xn1+ 2,, xn,  
where n=n1+n2.
The hypothesis under test, H0, often called the null hypothesis, is that the medians are the same, and this is to be tested against the alternative hypothesis H1 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 i1 i2 i1+i2
Scores  pooled median n1-i1 n2-i2 n-i1+i2
Total n1 n2 n
Under the null hypothesis, H0, 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, to be about n1/2 and i2 to be about n2/2.
nag_nonpar_test_median (g08ac) returns:
(a) the frequencies i1 and i2;
(b) the probability, p, of observing a table at least as ‘extreme’ as that actually observed, given that H0 is true. If n<40, p is computed directly (‘Fisher's exact test’); otherwise a χ12 approximation is used (see nag_stat_contingency_table (g01af)).
H0 is rejected by a test of chosen size α if p<α.


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


Compulsory Input Parameters

1:     xn – double array
The first n1 elements of x must be set to the data values in the first sample, and the next n2 (=n-n1) elements to the data values in the second sample.
2:     n1 int64int32nag_int scalar
The size of the first sample n1.
Constraint: 1n1<n.

Optional Input Parameters

1:     n int64int32nag_int scalar
Default: the dimension of the array x.
The total of the two sample sizes, n (=n1+n2).
Constraint: n2.

Output Parameters

1:     i1 int64int32nag_int scalar
The number of scores in the first sample which lie below the pooled median, i1.
2:     i2 int64int32nag_int scalar
The number of scores in the second sample which lie below the pooled median, i2.
3:     p – double scalar
The tail probability p corresponding to the observed dichotomy of the two samples.
4:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:
On entry,n<2.
On entry,n1<1,
An unexpected error has been triggered by this routine. Please contact NAG.
Your licence key may have expired or may not have been installed correctly.
Dynamic memory allocation failed.


The probability returned should be accurate enough for practical use.

Further Comments

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


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('\n             ');
fprintf('\n     Group 2 ');
for j = 1:floor(n2/8)
  i1 = (j-1)*8 + 1;
  i2 = min(n2,i1+7);
  fprintf('\n             ');

[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

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015