1 ## Copyright (C) 1995-2012 Kurt Hornik
3 ## This file is part of Octave.
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.
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.
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/>.
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.
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.
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.
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.
37 ## The p-value (1 minus the CDF of this distribution at @var{f}) is
38 ## returned in @var{pval}.
40 ## If no output argument is given, the standard one-way ANOVA table is
44 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
45 ## Description: One-way analysis of variance (ANOVA)
47 function [pval, f, df_b, df_w] = anova (y, g)
49 if ((nargin < 1) || (nargin > 2))
53 error ("anova: for `anova (Y)', Y must not be a vector");
55 [group_count, k] = size (y);
57 group_mean = mean (y);
60 error ("anova: for `anova (Y, G)', Y must be a vector");
63 if (! isvector (g) || (length (g) != n))
64 error ("anova: G must be a vector of the same length as Y");
67 i = find (s (2 : n) > s(1 : (n-1)));
70 error ("anova: there should be at least 2 groups");
72 group_label = s ([1, (reshape (i, 1, k-1) + 1)]);
75 v = y (find (g == group_label (i)));
76 group_count (i) = length (v);
77 group_mean (i) = mean (v);
82 total_mean = mean (y(:));
83 SSB = sum (group_count .* (group_mean - total_mean) .^ 2);
84 SST = sumsq (reshape (y, n, 1) - total_mean);
91 pval = 1 - fcdf (f, df_b, df_w);
94 ## This eventually needs to be done more cleanly ...
96 printf ("One-way ANOVA Table:\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);
105 printf ("Test Statistic f %15.4f\n", f);
106 printf ("p-value %15.4f\n", pval);