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} {} cor_test (@var{x}, @var{y}, @var{alt}, @var{method})
21 ## Test whether two samples @var{x} and @var{y} come from uncorrelated
24 ## The optional argument string @var{alt} describes the alternative
25 ## hypothesis, and can be @code{"!="} or @code{"<>"} (non-zero),
26 ## @code{">"} (greater than 0), or @code{"<"} (less than 0). The
27 ## default is the two-sided case.
29 ## The optional argument string @var{method} specifies which
30 ## correlation coefficient to use for testing. If @var{method} is
31 ## @code{"pearson"} (default), the (usual) Pearson's product moment
32 ## correlation coefficient is used. In this case, the data should come
33 ## from a bivariate normal distribution. Otherwise, the other two
34 ## methods offer nonparametric alternatives. If @var{method} is
35 ## @code{"kendall"}, then Kendall's rank correlation tau is used. If
36 ## @var{method} is @code{"spearman"}, then Spearman's rank correlation
37 ## rho is used. Only the first character is necessary.
39 ## The output is a structure with the following elements:
43 ## The p-value of the test.
46 ## The value of the test statistic.
49 ## The distribution of the test statistic.
52 ## The parameters of the null distribution of the test statistic.
55 ## The alternative hypothesis.
58 ## The method used for testing.
61 ## If no output argument is given, the p-value is displayed.
64 ## Author: FL <Friedrich.Leisch@ci.tuwien.ac.at>
65 ## Adapted-by: KH <Kurt.Hornik@wu-wien.ac.at>
66 ## Description: Test for zero correlation
68 function t = cor_test (x, y, alt, method)
70 if ((nargin < 2) || (nargin > 4))
74 if (!isvector (x) || !isvector (y) || length (x) != length (y))
75 error ("cor_test: X and Y must be vectors of the same length");
80 elseif (! ischar (alt))
81 error ("cor_test: ALT must be a string");
86 elseif (! ischar (method))
87 error ("cor_test: METHOD must be a string");
96 t.method = "Pearson's product moment correlation";
98 t.stat = sqrt (df) .* r / sqrt (1 - r.^2);
100 cdf = tcdf (t.stat, df);
102 tau = kendall (x, y);
103 t.method = "Kendall's rank correlation tau";
105 t.stat = tau / sqrt ((2 * (2*n+5)) / (9*n*(n-1)));
106 t.dist = "stdnormal";
107 cdf = stdnormal_cdf (t.stat);
109 rho = spearman (x, y);
110 t.method = "Spearman's rank correlation rho";
112 t.stat = sqrt (n-1) * (rho - 6/(n^3-n));
113 t.dist = "stdnormal";
114 cdf = stdnormal_cdf (t.stat);
116 error ("cor_test: METHOD `%s' not recognized", method);
119 if (strcmp (alt, "!=") || strcmp (alt, "<>"))
120 t.pval = 2 * min (cdf, 1 - cdf);
121 elseif (strcmp (alt, ">"))
123 elseif (strcmp (alt, "<"))
126 error ("cor_test: alternative `%s' not recognized", alt);
132 printf ("pval: %g\n", t.pval);