3200
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3200
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2, or (at your option) |
|
6 ## any later version. |
3426
|
7 ## |
3200
|
8 ## This program is distributed in the hope that it will be useful, but |
|
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
11 ## General Public License for more details. |
|
12 ## |
3200
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this file. If not, write to the Free Software Foundation, |
|
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
16 |
3454
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {[@var{pval}, @var{z}] =} u_test (@var{x}, @var{y}, @var{alt}) |
|
19 ## For two samples @var{x} and @var{y}, perform a Mann-Whitney U-test of |
|
20 ## the null hypothesis PROB (@var{x} > @var{y}) == 1/2 == PROB (@var{x} |
|
21 ## < @var{y}). Under the null, the test statistic @var{z} approximately |
|
22 ## follows a standard normal distribution. Note that this test is |
|
23 ## equivalent to the Wilcoxon rank-sum test. |
3200
|
24 ## |
3454
|
25 ## With the optional argument string @var{alt}, the alternative of |
|
26 ## interest can be selected. If @var{alt} is @code{"!="} or |
|
27 ## @code{"<>"}, the null is tested against the two-sided alternative |
|
28 ## PROB (@var{x} > @var{y}) != 1/2. If @var{alt} is @code{">"}, the |
3457
|
29 ## one-sided alternative PROB (@var{x} > @var{y}) > 1/2 is considered. |
|
30 ## Similarly for @code{"<"}, the one-sided alternative PROB (@var{x} > |
|
31 ## @var{y}) < 1/2 is considered, The default is the two-sided case. |
3200
|
32 ## |
3454
|
33 ## The p-value of the test is returned in @var{pval}. |
3426
|
34 ## |
3200
|
35 ## If no output argument is given, the p-value of the test is displayed. |
3454
|
36 ## @end deftypefn |
3426
|
37 |
3200
|
38 ## This implementation is still incomplete---for small sample sizes, |
|
39 ## the normal approximation is rather bad ... |
|
40 |
3456
|
41 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
42 ## Description: Mann-Whitney U-test |
3200
|
43 |
|
44 function [pval, z] = u_test (x, y, alt) |
3426
|
45 |
3200
|
46 if ((nargin < 2) || (nargin > 3)) |
3456
|
47 usage ("[pval, z] = u_test (x, y, alt)"); |
3200
|
48 endif |
3426
|
49 |
3200
|
50 if (! (is_vector (x) && is_vector (y))) |
3456
|
51 error ("u_test: both x and y must be vectors"); |
3200
|
52 endif |
|
53 |
|
54 n_x = length (x); |
|
55 n_y = length (y); |
3273
|
56 r = ranks ([(reshape (x, 1, n_x)), (reshape (y, 1, n_y))]); |
3200
|
57 z = (sum (r(1 : n_x)) - n_x * (n_x + n_y + 1) / 2) ... |
3426
|
58 / sqrt (n_x * n_y * (n_x + n_y + 1) / 12); |
3200
|
59 |
|
60 cdf = stdnormal_cdf (z); |
3426
|
61 |
3200
|
62 if (nargin == 2) |
|
63 alt = "!="; |
|
64 endif |
|
65 |
|
66 if (! isstr (alt)) |
3456
|
67 error("u_test: alt must be a string"); |
3200
|
68 endif |
|
69 if (strcmp (alt, "!=") || strcmp (alt, "<>")) |
|
70 pval = 2 * min (cdf, 1 - cdf); |
|
71 elseif (strcmp (alt, ">")) |
|
72 pval = cdf; |
|
73 elseif (strcmp (alt, "<")) |
|
74 pval = 1 - cdf; |
|
75 else |
3456
|
76 error ("u_test: option %s not recognized", alt); |
3200
|
77 endif |
|
78 |
|
79 if (nargout == 0) |
3456
|
80 printf (" pval: %g\n", pval); |
3200
|
81 endif |
3426
|
82 |
3200
|
83 endfunction |