PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_nonpar_test_mwu (g08ah)
Purpose
nag_nonpar_test_mwu (g08ah) performs the Mann–Whitney test on two independent samples of possibly unequal size.
Syntax
[
u,
unor,
p,
ties,
ranks,
ifail] = g08ah(
x,
y,
tail, 'n1',
n1, 'n2',
n2)
[
u,
unor,
p,
ties,
ranks,
ifail] = nag_nonpar_test_mwu(
x,
y,
tail, 'n1',
n1, 'n2',
n2)
Description
The Mann–Whitney test investigates the difference between two populations defined by the distribution functions and respectively. The data consist of two independent samples of size and , denoted by and , taken from the two populations.
The hypothesis under test,
, often called the null hypothesis, is that the two distributions are the same, that is
, and this is to be tested against an alternative hypothesis
which is
- : ; or
- : , i.e., the 's tend to be greater than the 's; or
- : , i.e., the 's tend to be less than the 's,
using a two tailed, upper tailed or lower tailed probability respectively. You select the alternative hypothesis by choosing the appropriate tail probability to be computed (see the description of argument
tail in
Arguments).
Note that when using this test to test for differences in the distributions one is primarily detecting differences in the location of the two distributions. That is to say, if we reject the null hypothesis in favour of the alternative hypothesis : we have evidence to suggest that the location, of the distribution defined by , is less than the location, of the distribution defined by .
The Mann–Whitney
test differs from the Median test (see
nag_nonpar_test_median (g08ac)) in that the ranking of the individual scores within the pooled sample is taken into account, rather than simply the position of a score relative to the median of the pooled sample. It is therefore a more powerful test if score differences are meaningful.
The test procedure involves ranking the pooled sample, average ranks being used for ties. Let
be the rank assigned to
,
and
the rank assigned to
,
. Then the test statistic
is defined as follows;
is also the number of times a score in the second sample precedes a score in the first sample (where we only count a half if a score in the second sample actually equals a score in the first sample).
nag_nonpar_test_mwu (g08ah) returns:
(a) |
The test statistic . |
(b) |
The approximate Normal test statistic,
where
and
where
is the number of groups of ties in the sample and is the number of ties in the th group.
Note that if no ties are present the variance of reduces to . |
(c) |
An indicator as to whether ties were present in the pooled sample or not. |
(d) |
The tail probability, , corresponding to (adjusted to allow the complement to be used in an upper one tailed or a two tailed test), depending on the choice of tail, i.e., the choice of alternative hypothesis, . The tail probability returned is an approximation of is based on an approximate Normal statistic corrected for continuity according to the tail specified. If and are not very large an exact probability may be desired. For the calculation of the exact probability see nag_nonpar_prob_mwu_noties (g08aj) (no ties in the pooled sample) or nag_nonpar_prob_mwu_ties (g08ak) (ties in the pooled sample).
The value of can be used to perform a significance test on the null hypothesis against the alternative hypothesis . Let be the size of the significance test (that is, is the probability of rejecting when is true). If then the null hypothesis is rejected. Typically might be or . |
References
Conover W J (1980) Practical Nonparametric Statistics Wiley
Neumann N (1988) Some procedures for calculating the distributions of elementary nonparametric teststatistics Statistical Software Newsletter 14(3) 120–126
Siegel S (1956) Non-parametric Statistics for the Behavioral Sciences McGraw–Hill
Parameters
Compulsory Input Parameters
- 1:
– double array
-
The first vector of observations, .
- 2:
– double array
-
The second vector of observations. .
- 3:
– string (length ≥ 1)
-
Indicates the choice of tail probability, and hence the alternative hypothesis.
- A two tailed probability is calculated and the alternative hypothesis is .
- An upper tailed probability is calculated and the alternative hypothesis , i.e., the 's tend to be greater than the 's.
- A lower tailed probability is calculated and the alternative hypothesis , i.e., the 's tend to be less than the 's.
Constraint:
, or .
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the dimension of the array
x.
The size of the first sample, .
Constraint:
.
- 2:
– int64int32nag_int scalar
-
Default:
the dimension of the array
y.
The size of the second sample, .
Constraint:
.
Output Parameters
- 1:
– double scalar
-
The Mann–Whitney rank sum statistic, .
- 2:
– double scalar
-
The approximate Normal test statistic,
, as described in
Description.
- 3:
– double scalar
-
The tail probability,
, as specified by the argument
tail.
- 4:
– logical scalar
-
Indicates whether the pooled sample contained ties or not. This will be useful in checking which function to use should one wish to calculate an exact tail probability.
, no ties were present (use
nag_nonpar_prob_mwu_noties (g08aj) for an exact probability).
, ties were present (use
nag_nonpar_prob_mwu_ties (g08ak) for an exact probability).
- 5:
– double array
-
Contains the ranks of the pooled sample. The ranks of the first sample are contained in the first
n1 elements and those of the second sample are contained in the next
n2 elements.
- 6:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
Cases prefixed with W are classified as warnings and
do not generate an error of type NAG:error_n. See nag_issue_warnings.
-
-
-
-
On entry, | , or . |
- W
-
The pooled sample values are all the same, that is the variance of .
-
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.
Accuracy
The approximate tail probability, , returned by nag_nonpar_test_mwu (g08ah) is a good approximation to the exact probability for cases where and . The relative error of the approximation should be less than , for most cases falling in this range.
Further Comments
The time taken by nag_nonpar_test_mwu (g08ah) increases with and .
Example
This example performs the Mann–Whitney test on two independent samples of sizes and respectively. This is used to test the null hypothesis that the distributions of the two populations from which the samples were taken are the same against the alternative hypothesis that the distributions are different. The test statistic, the approximate Normal statistic and the approximate two tail probability are printed. An exact tail probability is also calculated and printed depending on whether ties were found in the pooled sample or not.
Open in the MATLAB editor:
g08ah_example
function g08ah_example
fprintf('g08ah example results\n\n');
x = [13; 6; 12; 7; 12; 7; 10; 7;
10; 7; 16; 7; 10; 8; 9; 8];
y = [17; 6; 10; 8; 15; 8; 15; 10;
15; 10; 14; 10; 14; 11; 14; 11;
13; 12; 13; 12; 13; 12; 12];
n1 = int64(numel(x));
n2 = int64(numel(y));
fprintf('Mann-Whitney U test\n\n');
fprintf('Sample size of group 1 = %5d\n', n1);
fprintf('Sample size of group 2 = %5d\n\n', n2);
fprintf('Data values\n');
fprintf('\n 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',y(i1:i2));
fprintf('\n ');
end
tail = 'Lower-tail';
[u, unor, p, ties, ranks, ifail] = ...
g08ah(x, y, tail);
if ties
[pexact, ifail] = g08ak( ...
n1, n2, tail, ranks, u);
else
[pexact, ifail] = g08aj( ...
n1, n2, tail, u);
end
fprintf('\nTest statistic = %8.4f\n', u);
fprintf('Normalized test statistic = %8.4f\n', unor);
fprintf('Approx. tail probability = %8.4f\n\n', p);
if ties
fprintf('There are ties in the pooled sample\n\n');
else
fprintf('There are no ties in the pooled sample\n\n');
end
fprintf('Exact tail probability = %8.4f\n', pexact);
g08ah example results
Mann-Whitney U test
Sample size of group 1 = 16
Sample size of group 2 = 23
Data values
Group 1 13 6 12 7 12 7 10 7
10 7 16 7 10 8 9 8
Group 2 17 6 10 8 15 8 15 10
15 10 14 10 14 11 14 11
13 12 13 12 13 12 12
Test statistic = 86.0000
Normalized test statistic = -2.8039
Approx. tail probability = 0.0025
There are ties in the pooled sample
Exact tail probability = 0.0020
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015