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