home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / tests / anova.m next >
Text File  |  1997-02-26  |  4KB  |  106 lines

  1. ## Copyright (C) 1995, 1996, 1997  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## Performs a one-way analysis of variance (ANOVA).  The goal is to test
  18. ## whether the population means of data taken from k different groups
  19. ## are all equal.
  20. ##
  21. ## anova (y, g) provides all data in a single vector y;  g is the vector
  22. ## of corresponding group labels (e.g., numbers from 1 to k). This is
  23. ## the general form which does not impose any restriction on the number
  24. ## of data in each group or the group labels (other than that they must
  25. ## be scalars).
  26. ##
  27. ## anova (y), where y is a matrix, treats each column as a group. This
  28. ## form is only appropriate for balanced ANOVA where the numbers of
  29. ## samples from each group are all equal.
  30. ##
  31. ## Under the null of constant means, the statistic f follows an F
  32. ## distribution with df_b and df_w degrees of freedom.  pval is the
  33. ## p-value (1 minus the CDF of this distribution at f) of the test.
  34. ##
  35. ## If no output argument is given, the standard one-way ANOVA table is
  36. ## printed.
  37.  
  38. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  39. ## Description:  One-way analysis of variance (ANOVA)
  40.   
  41. function [pval, f, df_b, df_w] = anova (y, g)
  42.   
  43.   if ((nargin < 1) || (nargin > 2))
  44.     usage ("anova (y [, g])");
  45.   elseif (nargin == 1)
  46.     if (is_vector (y))
  47.       error ("anova:  for `anova (y)', y must not be a vector");
  48.     endif
  49.     [group_count, k] = size (y);
  50.     n = group_count * k;
  51.     group_mean = mean (y);
  52.   else
  53.     if (! is_vector (y))
  54.       error ("anova:  for `anova (y, g)', y must be a vector");
  55.     endif
  56.     n = length (y);
  57.     if (! is_vector (g) || (length (g) != n))
  58.       error (["anova:  for `anova (y, g)', g must be a vector", ...
  59.         " of the same length y"]);
  60.     endif
  61.     s = sort (g);
  62.     i = find (s (2 : n) > s(1 : (n-1)));
  63.     k = length (i) + 1;
  64.     if (k == 1)
  65.       error ("anova:  there should be at least 2 groups");
  66.     else
  67.       group_label = s ([1, reshape (i, 1, k-1) + 1]);
  68.     endif
  69.     for i = 1 : k;
  70.       v = y (find (g == group_label (i)));
  71.       group_count (i) = length (v);
  72.       group_mean (i) = mean (v);
  73.     endfor
  74.     
  75.   endif
  76.   
  77.   total_mean = mean (group_mean);  
  78.   SSB = sum (group_count .* (group_mean - total_mean) .^ 2);
  79.   SST = sumsq (reshape (y, n, 1) - total_mean); 
  80.   SSW = SST - SSB;
  81.   df_b = k - 1;
  82.   df_w = n - k;
  83.   v_b = SSB / df_b;
  84.   v_w = SSW / df_w;
  85.   f = v_b / v_w;
  86.   pval = 1 - f_cdf (f, df_b, df_w);
  87.   
  88.   if (nargout == 0)
  89.     ## This eventually needs to be done more cleanly ...
  90.     printf ("\n");
  91.     printf ("One-way ANOVA Table:\n");
  92.     printf ("\n");
  93.     printf ("Source of Variation   Sum of Squares    df  Empirical Var\n");
  94.     printf ("*********************************************************\n");
  95.     printf ("Between Groups       %15.4f  %4d  %13.4f\n", SSB, df_b, v_b);
  96.     printf ("Within Groups        %15.4f  %4d  %13.4f\n", SSW, df_w, v_w);
  97.     printf ("---------------------------------------------------------\n");
  98.     printf ("Total                %15.4f  %4d\n", SST, n - 1);
  99.     printf ("\n");
  100.     printf ("Test Statistic f     %15.4f\n", f);    
  101.     printf ("p-value              %15.4f\n", pval);
  102.     printf ("\n");
  103.   endif  
  104.   
  105. endfunction
  106.