home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / tests / cor_test.m < prev    next >
Text File  |  1997-06-01  |  4KB  |  118 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:  cor_test (X, Y [, ALTERNATIVE [, METHOD]])
  18. ##
  19. ## Test whether two samples X and Y come from uncorrelated populations.
  20. ##
  21. ## The optional argument string ALTERNATIVE describes the alternative
  22. ## hypothesis, and can be "!=" or "<>" (non-zero), ">" (greater than 0),
  23. ## or "<" (less than 0).  The default is the two-sided case.
  24. ##
  25. ## The optional argument string METHOD specifies on which correlation
  26. ## coefficient the test should be based.
  27. ## If METHOD is "pearson" (default), the (usual) Pearson's product
  28. ## moment correlation coefficient is used.  In this case, the data
  29. ## should come from a bivariate normal distribution.  Otherwise, the
  30. ## other two methods offer nonparametric alternatives.
  31. ## If METHOD is "kendall", then Kendall's rank correlation tau is used.
  32. ## If METHOD is "spearman", then Spearman's rank correlation rho is used.
  33. ## Only the first character is necessary.
  34. ##
  35. ## The output is a structure with the following elements:
  36. ##    pval        The p-value of the test.
  37. ##    stat        The value of the test statistic.
  38. ##    dist        The distribution of the test statistic.
  39. ##    params        The parameters of the null distribution of the
  40. ##            test statistic.
  41. ##    alternative    The alternative hypothesis.
  42. ##    method        The method used for testing.
  43. ##
  44. ## If no output argument is given, the pval is displayed.
  45.  
  46. ## Author:  FL <Friedrich.Leisch@ci.tuwien.ac.at>
  47. ## Adapted-by:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  48. ## Description:  Test for zero correlation
  49.  
  50. function t = cor_test (X, Y, ALTERNATIVE, METHOD)
  51.   
  52.   if ((nargin < 2) || (nargin > 4))
  53.     usage ("cor_test (X, Y [, ALTERNATIVE [, METHOD]])")
  54.   endif
  55.  
  56.   if (!is_vector (X) || !is_vector (Y) || length (X) != length (Y))
  57.     error ("cor_test:  X and Y must be vectors of the same length")
  58.   endif
  59.  
  60.   if (nargin < 3)
  61.     ALTERNATIVE = "!=";
  62.   elseif !isstr (ALTERNATIVE)
  63.     error ("cor_test:  ALTERNATIVE must be a string");
  64.   endif
  65.  
  66.   if (nargin < 4)
  67.     METHOD = "pearson";
  68.   elseif !isstr (METHOD)
  69.     error ("cor_test:  METHOD must be a string");
  70.   endif
  71.  
  72.   n = length (X);
  73.   m = METHOD (1);
  74.  
  75.   if (m == "p")
  76.     r = cor (X, Y);
  77.     df = n - 2;
  78.     t.method = "Pearson's product moment correlation";
  79.     t.params = df;
  80.     t.stat = sqrt (df) .* r / sqrt (1 - r.^2);
  81.     t.dist = "t";
  82.     cdf  = t_cdf (t.stat, df);
  83.   elseif (m == "k")
  84.     tau = kendall (X, Y);
  85.     t.method = "Kendall's rank correlation tau";    
  86.     t.params = [];
  87.     t.stat = tau / sqrt ((2 * (2*n+5)) / (9*n*(n-1)));
  88.     t.dist = "stdnormal";
  89.     cdf = stdnormal_cdf (t.stat);
  90.   elseif (m == "s")
  91.     rho = spearman (X, Y);
  92.     t.method = "Spearman's rank correlation rho";
  93.     t.params = [];
  94.     t.stat = sqrt (n-1) * (rho - 6/(n^3-n));
  95.     t.dist = "stdnormal";    
  96.     cdf = stdnormal_cdf (t.stat);
  97.   else
  98.     error ("cor_test:  method `%s' not recognized", METHOD)
  99.   endif
  100.  
  101.   if (strcmp (ALTERNATIVE, "!=") || strcmp (ALTERNATIVE, "<>"))
  102.     t.pval = 2 * min (cdf, 1 - cdf);
  103.   elseif (strcmp (ALTERNATIVE, ">"))
  104.     t.pval = 1 - cdf;
  105.   elseif (strcmp (ALTERNATIVE, "<"))
  106.     t.pval = cdf;
  107.   else
  108.     error ("cor_test:  alternative `%s' not recognized", ALTERNATIVE);
  109.   endif
  110.  
  111.   t.alternative = ALTERNATIVE;
  112.  
  113.   if (nargout == 0)
  114.     printf ("pval:  %g\n", t.pval);
  115.   endif
  116.   
  117. endfunction
  118.