annotate scripts/statistics/tests/wilcoxon_test.m @ 10793:be55736a0783

Grammarcheck the documentation from m-files.
author Rik <octave@nomad.inbox5.com>
date Sun, 18 Jul 2010 20:35:16 -0700
parents a1dbe9d80eee
children 1740012184f9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2005, 2006, 2007
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
2 ## Kurt Hornik
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
3 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
4 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
5 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6701
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6701
diff changeset
9 ## your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
10 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
14 ## General Public License for more details.
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
15 ##
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6701
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6701
diff changeset
18 ## <http://www.gnu.org/licenses/>.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
19
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
20 ## -*- texinfo -*-
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
21 ## @deftypefn {Function File} {[@var{pval}, @var{z}] =} wilcoxon_test (@var{x}, @var{y}, @var{alt})
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
22 ## For two matched-pair sample vectors @var{x} and @var{y}, perform a
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
23 ## Wilcoxon signed-rank test of the null hypothesis PROB (@var{x} >
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
24 ## @var{y}) == 1/2. Under the null, the test statistic @var{z}
6701
3933e0693fe0 [project @ 2007-06-12 21:25:51 by jwe]
jwe
parents: 6046
diff changeset
25 ## approximately follows a standard normal distribution when @var{n} > 25.
3933e0693fe0 [project @ 2007-06-12 21:25:51 by jwe]
jwe
parents: 6046
diff changeset
26 ##
3933e0693fe0 [project @ 2007-06-12 21:25:51 by jwe]
jwe
parents: 6046
diff changeset
27 ## @strong{Warning}: This function assumes a normal distribution for @var{z}
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
28 ## and thus is invalid for @var{n} @leq{} 25.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
29 ##
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
30 ## With the optional argument string @var{alt}, the alternative of
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
31 ## interest can be selected. If @var{alt} is @code{"!="} or
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
32 ## @code{"<>"}, the null is tested against the two-sided alternative
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
33 ## PROB (@var{x} > @var{y}) != 1/2. If alt is @code{">"}, the one-sided
3457
e031284eea27 [project @ 2000-01-19 08:49:56 by jwe]
jwe
parents: 3456
diff changeset
34 ## alternative PROB (@var{x} > @var{y}) > 1/2 is considered. Similarly
e031284eea27 [project @ 2000-01-19 08:49:56 by jwe]
jwe
parents: 3456
diff changeset
35 ## for @code{"<"}, the one-sided alternative PROB (@var{x} > @var{y}) <
e031284eea27 [project @ 2000-01-19 08:49:56 by jwe]
jwe
parents: 3456
diff changeset
36 ## 1/2 is considered. The default is the two-sided case.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
37 ##
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
38 ## The p-value of the test is returned in @var{pval}.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
39 ##
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
40 ## If no output argument is given, the p-value of the test is displayed.
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
41 ## @end deftypefn
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
42
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
43 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
44 ## Description: Wilcoxon signed-rank test
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
45
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
46 function [pval, z] = wilcoxon_test (x, y, alt)
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
47
6701
3933e0693fe0 [project @ 2007-06-12 21:25:51 by jwe]
jwe
parents: 6046
diff changeset
48 if (nargin < 2 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5443
diff changeset
49 print_usage ();
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
50 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
51
4030
22bd65326ec1 [project @ 2002-08-09 18:58:13 by jwe]
jwe
parents: 3922
diff changeset
52 if (! (isvector (x) && isvector (y) && (length (x) == length (y))))
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
53 error ("wilcoxon_test: x and y must be vectors of the same length");
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
54 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
55
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
56 n = length (x);
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
57 x = reshape (x, 1, n);
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
58 y = reshape (y, 1, n);
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
59 d = x - y;
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
60 d = d (find (d != 0));
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
61 n = length (d);
6701
3933e0693fe0 [project @ 2007-06-12 21:25:51 by jwe]
jwe
parents: 6046
diff changeset
62 if (n > 25)
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
63 r = ranks (abs (d));
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
64 z = sum (r (find (d > 0)));
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
65 z = ((z - n * (n + 1) / 4) / sqrt (n * (n + 1) * (2 * n + 1) / 24));
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
66 else
6701
3933e0693fe0 [project @ 2007-06-12 21:25:51 by jwe]
jwe
parents: 6046
diff changeset
67 error ("wilcoxon_test: implementation requires more than 25 different pairs");
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
68 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
69
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
70 cdf = stdnormal_cdf (z);
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
71
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
72 if (nargin == 2)
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
73 alt = "!=";
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
74 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
75
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5428
diff changeset
76 if (! ischar (alt))
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
77 error("wilcoxon_test: alt must be a string");
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
78 elseif (strcmp (alt, "!=") || strcmp (alt, "<>"))
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
79 pval = 2 * min (cdf, 1 - cdf);
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
80 elseif (strcmp (alt, ">"))
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
81 pval = 1 - cdf;
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
82 elseif (strcmp (alt, "<"))
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
83 pval = cdf;
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
84 else
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
85 error ("wilcoxon_test: option %s not recognized", alt);
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
86 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
87
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
88 if (nargout == 0)
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
89 printf (" pval: %g\n", pval);
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
90 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
91
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
92 endfunction