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 |
|
17 ## usage: [pval, z] = wilcoxon_test (x, y [, alt]) |
|
18 ## |
|
19 ## For two matched-pair sample vectors x and y, perform a Wilcoxon |
|
20 ## signed-rank test of the null hypothesis PROB(x > y) == 1/2. |
|
21 ## Under the null, the test statistic z approximately follows a |
3426
|
22 ## standard normal distribution. |
3200
|
23 ## |
|
24 ## With the optional argument string alt, the alternative of interest |
3426
|
25 ## can be selected. |
3200
|
26 ## If alt is "!=" or "<>", the null is tested against the two-sided |
|
27 ## alternative PROB(x > y) != 1/2. |
|
28 ## If alt is ">", the one-sided alternative PROB(x > y) > 1/2 is |
|
29 ## considered, similarly for "<". |
|
30 ## The default is the two-sided case. |
|
31 ## |
|
32 ## pval is the p-value of the test. |
3426
|
33 ## |
3200
|
34 ## If no output argument is given, the p-value of the test is displayed. |
|
35 |
|
36 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
37 ## Description: Wilcoxon signed-rank test |
3426
|
38 |
3200
|
39 function [pval, z] = wilcoxon_test (x, y, alt) |
3426
|
40 |
3200
|
41 if ((nargin < 2) || (nargin > 3)) |
|
42 usage ("[pval, z] = wilcoxon_test (x, y [, alt])"); |
|
43 endif |
3426
|
44 |
3200
|
45 if (! (is_vector (x) && is_vector (y) && (length (x) == length (y)))) |
|
46 error ("wilcoxon_test: x and y must be vectors of the same length"); |
|
47 endif |
|
48 |
|
49 n = length (x); |
|
50 x = reshape (x, 1, n); |
|
51 y = reshape (y, 1, n); |
|
52 d = x - y; |
|
53 d = d (find (d != 0)); |
|
54 n = length (d); |
|
55 if (n > 0) |
|
56 r = ranks (abs (d)); |
|
57 z = sum (r (find (d > 0))); |
|
58 z = ((z - n * (n + 1) / 4) / sqrt (n * (n + 1) * (2 * n + 1) / 24)); |
|
59 else |
|
60 z = 0; |
|
61 endif |
|
62 |
|
63 cdf = stdnormal_cdf (z); |
3426
|
64 |
3200
|
65 if (nargin == 2) |
|
66 alt = "!="; |
|
67 endif |
|
68 |
|
69 if (! isstr (alt)) |
|
70 error("wilcoxon_test: alt must be a string"); |
|
71 elseif (strcmp (alt, "!=") || strcmp (alt, "<>")) |
|
72 pval = 2 * min (cdf, 1 - cdf); |
|
73 elseif (strcmp (alt, ">")) |
|
74 pval = 1 - cdf; |
|
75 elseif (strcmp (alt, "<")) |
|
76 pval = cdf; |
|
77 else |
|
78 error (sprintf ("wilcoxon_test: option %s not recognized", alt)); |
|
79 endif |
|
80 |
|
81 if (nargout == 0) |
|
82 printf (" pval: %g\n", pval); |
|
83 endif |
3426
|
84 |
3200
|
85 endfunction |