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