]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/tests/anova.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / tests / anova.m
1 ## Copyright (C) 1995-2012 Kurt Hornik
2 ##
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {[@var{pval}, @var{f}, @var{df_b}, @var{df_w}] =} anova (@var{y}, @var{g})
21 ## Perform a one-way analysis of variance (ANOVA).  The goal is to test
22 ## whether the population means of data taken from @var{k} different
23 ## groups are all equal.
24 ##
25 ## Data may be given in a single vector @var{y} with groups specified by
26 ## a corresponding vector of group labels @var{g} (e.g., numbers from 1
27 ## to @var{k}).  This is the general form which does not impose any
28 ## restriction on the number of data in each group or the group labels.
29 ##
30 ## If @var{y} is a matrix and @var{g} is omitted, each column of @var{y}
31 ## is treated as a group.  This form is only appropriate for balanced
32 ## ANOVA in which the numbers of samples from each group are all equal.
33 ##
34 ## Under the null of constant means, the statistic @var{f} follows an F
35 ## distribution with @var{df_b} and @var{df_w} degrees of freedom.
36 ##
37 ## The p-value (1 minus the CDF of this distribution at @var{f}) is
38 ## returned in @var{pval}.
39 ##
40 ## If no output argument is given, the standard one-way ANOVA table is
41 ## printed.
42 ## @end deftypefn
43
44 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
45 ## Description: One-way analysis of variance (ANOVA)
46
47 function [pval, f, df_b, df_w] = anova (y, g)
48
49   if ((nargin < 1) || (nargin > 2))
50     print_usage ();
51   elseif (nargin == 1)
52     if (isvector (y))
53       error ("anova: for `anova (Y)', Y must not be a vector");
54     endif
55     [group_count, k] = size (y);
56     n = group_count * k;
57     group_mean = mean (y);
58   else
59     if (! isvector (y))
60       error ("anova: for `anova (Y, G)', Y must be a vector");
61     endif
62     n = length (y);
63     if (! isvector (g) || (length (g) != n))
64       error ("anova: G must be a vector of the same length as Y");
65     endif
66     s = sort (g);
67     i = find (s (2 : n) > s(1 : (n-1)));
68     k = length (i) + 1;
69     if (k == 1)
70       error ("anova: there should be at least 2 groups");
71     else
72       group_label = s ([1, (reshape (i, 1, k-1) + 1)]);
73     endif
74     for i = 1 : k;
75       v = y (find (g == group_label (i)));
76       group_count (i) = length (v);
77       group_mean (i) = mean (v);
78     endfor
79
80   endif
81
82   total_mean = mean (y(:));
83   SSB = sum (group_count .* (group_mean - total_mean) .^ 2);
84   SST = sumsq (reshape (y, n, 1) - total_mean);
85   SSW = SST - SSB;
86   df_b = k - 1;
87   df_w = n - k;
88   v_b = SSB / df_b;
89   v_w = SSW / df_w;
90   f = v_b / v_w;
91   pval = 1 - fcdf (f, df_b, df_w);
92
93   if (nargout == 0)
94     ## This eventually needs to be done more cleanly ...
95     printf ("\n");
96     printf ("One-way ANOVA Table:\n");
97     printf ("\n");
98     printf ("Source of Variation   Sum of Squares    df  Empirical Var\n");
99     printf ("*********************************************************\n");
100     printf ("Between Groups       %15.4f  %4d  %13.4f\n", SSB, df_b, v_b);
101     printf ("Within Groups        %15.4f  %4d  %13.4f\n", SSW, df_w, v_w);
102     printf ("---------------------------------------------------------\n");
103     printf ("Total                %15.4f  %4d\n", SST, n - 1);
104     printf ("\n");
105     printf ("Test Statistic f     %15.4f\n", f);
106     printf ("p-value              %15.4f\n", pval);
107     printf ("\n");
108   endif
109
110 endfunction