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