annotate scripts/statistics/tests/kruskal_wallis_test.m @ 9051:1bf0ce0930be

Grammar check TexInfo in all .m files Cleanup documentation sources to follow a few consistent rules. Spellcheck was NOT done. (but will be in another changeset)
author Rik <rdrider0-list@yahoo.com>
date Fri, 27 Mar 2009 22:31:03 -0700
parents eb63fbe60fab
children f63ee6a20f57
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, 1999, 2000, 2002, 2005, 2006,
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
2 ## 2007, 2008, 2009 Kurt Hornik
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3273
diff changeset
3 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
4 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
5 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
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: 3456
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: 6046
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: 6046
diff changeset
9 ## your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3273
diff changeset
10 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
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: 3273
diff changeset
14 ## General Public License for more details.
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3273
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: 6046
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: 6046
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{k}, @var{df}] =} kruskal_wallis_test (@var{x1}, @dots{})
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
22 ## Perform a Kruskal-Wallis one-factor "analysis of variance".
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
23 ##
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
24 ## Suppose a variable is observed for @var{k} > 1 different groups, and
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
25 ## let @var{x1}, @dots{}, @var{xk} be the corresponding data vectors.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
26 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
27 ## Under the null hypothesis that the ranks in the pooled sample are not
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
28 ## affected by the group memberships, the test statistic @var{k} is
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
29 ## approximately chi-square with @var{df} = @var{k} - 1 degrees of
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
30 ## freedom.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
31 ##
7485
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
32 ## If the data contains ties (some value appears more than once)
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
33 ## @var{k} is divided by
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
34 ##
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7485
diff changeset
35 ## 1 - @var{sum_ties} / (@var{n}^3 - @var{n})
7485
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
36 ##
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7485
diff changeset
37 ## where @var{sum_ties} is the sum of @var{t}^2 - @var{t} over each group
7485
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
38 ## of ties where @var{t} is the number of ties in the group and @var{n}
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
39 ## is the total number of values in the input data. For more info on
7485
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
40 ## this adjustment see "Use of Ranks in One-Criterion Variance Analysis"
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
41 ## in Journal of the American Statistical Association, Vol. 47,
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
42 ## No. 260 (Dec 1952) by William H. Kruskal and W. Allen Wallis.
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
43 ##
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
44 ## The p-value (1 minus the CDF of this distribution at @var{k}) is
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
45 ## returned in @var{pval}.
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
46 ##
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
47 ## If no output argument is given, the p-value is displayed.
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
48 ## @end deftypefn
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
49
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
50 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
51 ## Description: Kruskal-Wallis test
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3273
diff changeset
52
3979
e0b7a493e5a8 [project @ 2002-07-10 17:45:34 by jwe]
jwe
parents: 3922
diff changeset
53 function [pval, k, df] = kruskal_wallis_test (varargin)
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3273
diff changeset
54
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
55 m = nargin;
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
56 if (m < 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5428
diff changeset
57 print_usage ();
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
58 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3273
diff changeset
59
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
60 n = [];
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
61 p = [];
3979
e0b7a493e5a8 [project @ 2002-07-10 17:45:34 by jwe]
jwe
parents: 3922
diff changeset
62
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
63 for i = 1 : m;
3979
e0b7a493e5a8 [project @ 2002-07-10 17:45:34 by jwe]
jwe
parents: 3922
diff changeset
64 x = varargin{i};
4030
22bd65326ec1 [project @ 2002-08-09 18:58:13 by jwe]
jwe
parents: 3979
diff changeset
65 if (! isvector (x))
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
66 error ("kruskal_wallis_test: all arguments must be vectors");
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
67 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
68 l = length (x);
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
69 n = [n, l];
3273
eb27ea9b7ff8 [project @ 1999-10-12 02:22:25 by jwe]
jwe
parents: 3200
diff changeset
70 p = [p, (reshape (x, 1, l))];
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
71 endfor
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3273
diff changeset
72
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
73 r = ranks (p);
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
74
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
75 k = 0;
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
76 j = 0;
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
77 for i = 1 : m;
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
78 k = k + (sum (r ((j + 1) : (j + n(i))))) ^ 2 / n(i);
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
79 j = j + n(i);
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
80 endfor
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3273
diff changeset
81
7485
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
82 n = length (p);
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
83 k = 12 * k / (n * (n + 1)) - 3 * (n + 1);
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
84
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
85 ## Adjust the result to takes ties into account.
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
86 sum_ties = sum (polyval ([1, 0, -1, 0], runlength (sort (p))));
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
87 k = k / (1 - sum_ties / (n^3 - n));
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
88
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
89 df = m - 1;
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
90 pval = 1 - chisquare_cdf (k, df);
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3273
diff changeset
91
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
92 if (nargout == 0)
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
93 printf ("pval: %g\n", pval);
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
94 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
95
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
96 endfunction
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
97
7485
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
98 ## Test with ties
fe2d956d9007 handle ties in kruskal_wallis_test
Timo Lindfors
parents: 7017
diff changeset
99 %!assert (abs(kruskal_wallis_test([86 86], [74]) - 0.157299207050285) < 0.0000000000001)