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