7017
|
1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2005, 2006, 2007 |
|
2 ## Kurt Hornik |
3426
|
3 ## |
3922
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
3426
|
10 ## |
3922
|
11 ## Octave is distributed in the hope that it will be useful, but |
3200
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
14 ## General Public License for more details. |
|
15 ## |
3200
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
3200
|
19 |
3454
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} cor_test (@var{x}, @var{y}, @var{alt}, @var{method}) |
|
22 ## Test whether two samples @var{x} and @var{y} come from uncorrelated |
|
23 ## populations. |
3200
|
24 ## |
3454
|
25 ## The optional argument string @var{alt} describes the alternative |
|
26 ## hypothesis, and can be @code{"!="} or @code{"<>"} (non-zero), |
|
27 ## @code{">"} (greater than 0), or @code{"<"} (less than 0). The |
|
28 ## default is the two-sided case. |
3200
|
29 ## |
3454
|
30 ## The optional argument string @var{method} specifies on which |
|
31 ## correlation coefficient the test should be based. If @var{method} is |
|
32 ## @code{"pearson"} (default), the (usual) Pearson's product moment |
|
33 ## correlation coefficient is used. In this case, the data should come |
|
34 ## from a bivariate normal distribution. Otherwise, the other two |
|
35 ## methods offer nonparametric alternatives. If @var{method} is |
|
36 ## @code{"kendall"}, then Kendall's rank correlation tau is used. If |
|
37 ## @var{method} is @code{"spearman"}, then Spearman's rank correlation |
|
38 ## rho is used. Only the first character is necessary. |
3200
|
39 ## |
|
40 ## The output is a structure with the following elements: |
|
41 ## |
3454
|
42 ## @table @var |
|
43 ## @item pval |
|
44 ## The p-value of the test. |
|
45 ## @item stat |
|
46 ## The value of the test statistic. |
|
47 ## @item dist |
|
48 ## The distribution of the test statistic. |
|
49 ## @item params |
|
50 ## The parameters of the null distribution of the test statistic. |
|
51 ## @item alternative |
|
52 ## The alternative hypothesis. |
|
53 ## @item method |
|
54 ## The method used for testing. |
|
55 ## @end table |
|
56 ## |
|
57 ## If no output argument is given, the p-value is displayed. |
|
58 ## @end deftypefn |
3200
|
59 |
3456
|
60 ## Author: FL <Friedrich.Leisch@ci.tuwien.ac.at> |
5428
|
61 ## Adapted-by: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
62 ## Description: Test for zero correlation |
3200
|
63 |
|
64 function t = cor_test (X, Y, ALTERNATIVE, METHOD) |
3426
|
65 |
3200
|
66 if ((nargin < 2) || (nargin > 4)) |
6046
|
67 print_usage (); |
3200
|
68 endif |
|
69 |
4030
|
70 if (!isvector (X) || !isvector (Y) || length (X) != length (Y)) |
8664
|
71 error ("cor_test: X and Y must be vectors of the same length"); |
3200
|
72 endif |
|
73 |
|
74 if (nargin < 3) |
|
75 ALTERNATIVE = "!="; |
5443
|
76 elseif (! ischar (ALTERNATIVE)) |
3456
|
77 error ("cor_test: ALTERNATIVE must be a string"); |
3200
|
78 endif |
|
79 |
|
80 if (nargin < 4) |
|
81 METHOD = "pearson"; |
5443
|
82 elseif (! ischar (METHOD)) |
3456
|
83 error ("cor_test: METHOD must be a string"); |
3200
|
84 endif |
|
85 |
|
86 n = length (X); |
|
87 m = METHOD (1); |
|
88 |
|
89 if (m == "p") |
|
90 r = cor (X, Y); |
|
91 df = n - 2; |
|
92 t.method = "Pearson's product moment correlation"; |
|
93 t.params = df; |
|
94 t.stat = sqrt (df) .* r / sqrt (1 - r.^2); |
|
95 t.dist = "t"; |
|
96 cdf = t_cdf (t.stat, df); |
|
97 elseif (m == "k") |
|
98 tau = kendall (X, Y); |
3426
|
99 t.method = "Kendall's rank correlation tau"; |
3200
|
100 t.params = []; |
|
101 t.stat = tau / sqrt ((2 * (2*n+5)) / (9*n*(n-1))); |
|
102 t.dist = "stdnormal"; |
|
103 cdf = stdnormal_cdf (t.stat); |
|
104 elseif (m == "s") |
|
105 rho = spearman (X, Y); |
|
106 t.method = "Spearman's rank correlation rho"; |
|
107 t.params = []; |
|
108 t.stat = sqrt (n-1) * (rho - 6/(n^3-n)); |
3426
|
109 t.dist = "stdnormal"; |
3200
|
110 cdf = stdnormal_cdf (t.stat); |
|
111 else |
8664
|
112 error ("cor_test: method `%s' not recognized", METHOD); |
3200
|
113 endif |
|
114 |
|
115 if (strcmp (ALTERNATIVE, "!=") || strcmp (ALTERNATIVE, "<>")) |
|
116 t.pval = 2 * min (cdf, 1 - cdf); |
|
117 elseif (strcmp (ALTERNATIVE, ">")) |
|
118 t.pval = 1 - cdf; |
|
119 elseif (strcmp (ALTERNATIVE, "<")) |
|
120 t.pval = cdf; |
|
121 else |
3456
|
122 error ("cor_test: alternative `%s' not recognized", ALTERNATIVE); |
3200
|
123 endif |
|
124 |
|
125 t.alternative = ALTERNATIVE; |
|
126 |
|
127 if (nargout == 0) |
3456
|
128 printf ("pval: %g\n", t.pval); |
3200
|
129 endif |
3426
|
130 |
3200
|
131 endfunction |