home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / stat / base / spearman.m < prev    next >
Text File  |  1999-04-29  |  2KB  |  62 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:  spearman (x [, y])
  18. ##
  19. ## Computes Spearman's rank correlation coefficient rho for each of the
  20. ## variables specified by the input arguments.
  21. ##
  22. ## For matrices, each row is an observation and each column a variable;
  23. ## vectors are always observations and may be row or column vectors.
  24. ##
  25. ## spearman (x) is equivalent to spearman (x, x).
  26. ##
  27. ## For two data vectors x and y, Spearman's rho is the correlation of
  28. ## the ranks of x and y.
  29. ##
  30. ## If x and y are drawn from independent dist, rho has zero
  31. ## mean and variance 1 / (n - 1), and is asymptotically normally
  32. ## distributed.
  33.  
  34. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  35. ## Description:  Spearman's rank correlation rho
  36.  
  37. function rho = spearman (x, y)
  38.   
  39.   if ((nargin < 1) || (nargin > 2))
  40.     usage ("spearman (x [, y])");
  41.   endif
  42.  
  43.   if (rows (x) == 1)
  44.     x = x';
  45.   endif
  46.   n = rows (x);
  47.   
  48.   if (nargin == 1)
  49.     rho = cor (ranks (x));
  50.   else
  51.     if (rows (y) == 1)
  52.       y = y';
  53.     endif
  54.     if (rows (y) != n)
  55.       error (["spearman:  ", ...
  56.           "x and y must have the same number of observations"]);
  57.     endif
  58.     rho = cor (ranks (x), ranks (y));
  59.   endif
  60.   
  61. endfunction
  62.