home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / tests / kruskal_wallis_test.m < prev    next >
Text File  |  1997-02-26  |  2KB  |  75 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. ## usage:  [pval, k, df] = kruskal_wallis_test (x1, ...)
  18. ##
  19. ## Perform a Kruskal-Wallis one-factor "analysis of variance".
  20. ##
  21. ## Suppose a variable is observed for k > 1 different groups, and let
  22. ## x1, ..., xk be the corresponding data vectors.
  23. ##
  24. ## Under the null hypothesis that the ranks in the pooled sample are not
  25. ## affected by the group memberships, the test statistic k is
  26. ## approximately chi-square with df = k - 1 degrees of freedom. pval is
  27. ## the p-value (1 minus the CDF of this distribution at k) of this test.
  28. ##
  29. ## If no output argument is given, the pval is displayed.
  30.  
  31. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  32. ## Description:  Kruskal-Wallis test
  33.   
  34. function [pval, k, df] = kruskal_wallis_test (...)
  35.   
  36.   m = nargin;
  37.   if (m < 2)
  38.     usage ("[pval, k, df] = kruskal_wallis_test (x1, ...)");
  39.   endif
  40.   
  41.   n = [];
  42.   p = [];
  43.   va_start;
  44.   for i = 1 : m;
  45.     x = va_arg ();
  46.     if (! is_vector (x))
  47.       error ("kruskal_wallis_test:  all arguments must be vectors");
  48.     endif
  49.     l = length (x);
  50.     n = [n, l];
  51.     p = [p, reshape (x, 1, l)];
  52.   endfor
  53.   
  54.   r = ranks (p);
  55.  
  56.   k = 0;
  57.   j = 0;
  58.   for i = 1 : m;
  59.     k = k + (sum (r ((j + 1) : (j + n(i))))) ^ 2 / n(i);
  60.     j = j + n(i);
  61.   endfor
  62.   
  63.   n    = length (p);
  64.   k    = 12 * k / (n * (n + 1)) - 3 * (n + 1);
  65.   df   = m - 1;
  66.   pval = 1 - chisquare_cdf (k, df);
  67.   
  68.   if (nargout == 0)
  69.     printf ("pval:  %g\n", pval);
  70.   endif
  71.  
  72. endfunction
  73.  
  74.  
  75.