Mercurial > hg > octave-nkf
annotate scripts/statistics/tests/cor_test.m @ 11469:c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 09 Jan 2011 12:41:21 -0800 |
parents | 693e22af08ae |
children | 1740012184f9 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2005, 2006, 2007, 2009 |
7017 | 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 ## |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
30 ## The optional argument string @var{method} specifies which |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
31 ## correlation coefficient to use for testing. If @var{method} is |
3454 | 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 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
35 ## methods offer nonparametric alternatives. If @var{method} is |
3454 | 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. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10606
diff
changeset
|
45 ## |
3454 | 46 ## @item stat |
47 ## The value of the test statistic. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10606
diff
changeset
|
48 ## |
3454 | 49 ## @item dist |
50 ## The distribution of the test statistic. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10606
diff
changeset
|
51 ## |
3454 | 52 ## @item params |
53 ## The parameters of the null distribution of the test statistic. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10606
diff
changeset
|
54 ## |
3454 | 55 ## @item alternative |
56 ## The alternative hypothesis. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10606
diff
changeset
|
57 ## |
3454 | 58 ## @item method |
59 ## The method used for testing. | |
60 ## @end table | |
61 ## | |
62 ## If no output argument is given, the p-value is displayed. | |
63 ## @end deftypefn | |
3200 | 64 |
3456 | 65 ## Author: FL <Friedrich.Leisch@ci.tuwien.ac.at> |
5428 | 66 ## Adapted-by: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 67 ## Description: Test for zero correlation |
3200 | 68 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
69 function t = cor_test (x, y, alt, method) |
3426 | 70 |
3200 | 71 if ((nargin < 2) || (nargin > 4)) |
6046 | 72 print_usage (); |
3200 | 73 endif |
74 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
75 if (!isvector (x) || !isvector (y) || length (x) != length (y)) |
8664 | 76 error ("cor_test: X and Y must be vectors of the same length"); |
3200 | 77 endif |
78 | |
79 if (nargin < 3) | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
80 alt = "!="; |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
81 elseif (! ischar (alt)) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
82 error ("cor_test: alt must be a string"); |
3200 | 83 endif |
84 | |
85 if (nargin < 4) | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
86 method = "pearson"; |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
87 elseif (! ischar (method)) |
3456 | 88 error ("cor_test: METHOD must be a string"); |
3200 | 89 endif |
90 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
91 n = length (x); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
92 m = method (1); |
3200 | 93 |
94 if (m == "p") | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
95 r = cor (x, y); |
3200 | 96 df = n - 2; |
97 t.method = "Pearson's product moment correlation"; | |
98 t.params = df; | |
99 t.stat = sqrt (df) .* r / sqrt (1 - r.^2); | |
100 t.dist = "t"; | |
10606
ec34c7acd057
Replace deprecated function calls in statistics tests
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
101 cdf = tcdf (t.stat, df); |
3200 | 102 elseif (m == "k") |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
103 tau = kendall (x, y); |
3426 | 104 t.method = "Kendall's rank correlation tau"; |
3200 | 105 t.params = []; |
106 t.stat = tau / sqrt ((2 * (2*n+5)) / (9*n*(n-1))); | |
107 t.dist = "stdnormal"; | |
108 cdf = stdnormal_cdf (t.stat); | |
109 elseif (m == "s") | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
110 rho = spearman (x, y); |
3200 | 111 t.method = "Spearman's rank correlation rho"; |
112 t.params = []; | |
113 t.stat = sqrt (n-1) * (rho - 6/(n^3-n)); | |
3426 | 114 t.dist = "stdnormal"; |
3200 | 115 cdf = stdnormal_cdf (t.stat); |
116 else | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
117 error ("cor_test: method `%s' not recognized", method); |
3200 | 118 endif |
119 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
120 if (strcmp (alt, "!=") || strcmp (alt, "<>")) |
3200 | 121 t.pval = 2 * min (cdf, 1 - cdf); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
122 elseif (strcmp (alt, ">")) |
3200 | 123 t.pval = 1 - cdf; |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
124 elseif (strcmp (alt, "<")) |
3200 | 125 t.pval = cdf; |
126 else | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
127 error ("cor_test: alternative `%s' not recognized", alt); |
3200 | 128 endif |
129 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
130 t.alternative = alt; |
3200 | 131 |
132 if (nargout == 0) | |
3456 | 133 printf ("pval: %g\n", t.pval); |
3200 | 134 endif |
3426 | 135 |
3200 | 136 endfunction |