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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) 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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3200
|
18 |
3454
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {[@var{pval}, @var{k}, @var{df}] =} kruskal_wallis_test (@var{x1}, @dots{}) |
3200
|
21 ## Perform a Kruskal-Wallis one-factor "analysis of variance". |
|
22 ## |
3454
|
23 ## Suppose a variable is observed for @var{k} > 1 different groups, and |
|
24 ## let @var{x1}, @dots{}, @var{xk} be the corresponding data vectors. |
3200
|
25 ## |
|
26 ## Under the null hypothesis that the ranks in the pooled sample are not |
3454
|
27 ## affected by the group memberships, the test statistic @var{k} is |
|
28 ## approximately chi-square with @var{df} = @var{k} - 1 degrees of |
|
29 ## freedom. |
3200
|
30 ## |
3454
|
31 ## The p-value (1 minus the CDF of this distribution at @var{k}) is |
|
32 ## returned in @var{pval}. |
|
33 ## |
|
34 ## If no output argument is given, the p-value is displayed. |
|
35 ## @end deftypefn |
3200
|
36 |
5428
|
37 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
38 ## Description: Kruskal-Wallis test |
3426
|
39 |
3979
|
40 function [pval, k, df] = kruskal_wallis_test (varargin) |
3426
|
41 |
3200
|
42 m = nargin; |
|
43 if (m < 2) |
6046
|
44 print_usage (); |
3200
|
45 endif |
3426
|
46 |
3200
|
47 n = []; |
|
48 p = []; |
3979
|
49 |
3200
|
50 for i = 1 : m; |
3979
|
51 x = varargin{i}; |
4030
|
52 if (! isvector (x)) |
3456
|
53 error ("kruskal_wallis_test: all arguments must be vectors"); |
3200
|
54 endif |
|
55 l = length (x); |
|
56 n = [n, l]; |
3273
|
57 p = [p, (reshape (x, 1, l))]; |
3200
|
58 endfor |
3426
|
59 |
3200
|
60 r = ranks (p); |
|
61 |
|
62 k = 0; |
|
63 j = 0; |
|
64 for i = 1 : m; |
|
65 k = k + (sum (r ((j + 1) : (j + n(i))))) ^ 2 / n(i); |
|
66 j = j + n(i); |
|
67 endfor |
3426
|
68 |
3200
|
69 n = length (p); |
|
70 k = 12 * k / (n * (n + 1)) - 3 * (n + 1); |
|
71 df = m - 1; |
|
72 pval = 1 - chisquare_cdf (k, df); |
3426
|
73 |
3200
|
74 if (nargout == 0) |
3456
|
75 printf ("pval: %g\n", pval); |
3200
|
76 endif |
|
77 |
|
78 endfunction |
|
79 |
|
80 |