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