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 |
3453
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} spearman (@var{x}, @var{y}) |
|
22 ## Compute Spearman's rank correlation coefficient @var{rho} for each of |
|
23 ## the variables specified by the input arguments. |
3200
|
24 ## |
|
25 ## For matrices, each row is an observation and each column a variable; |
|
26 ## vectors are always observations and may be row or column vectors. |
|
27 ## |
3453
|
28 ## @code{spearman (@var{x})} is equivalent to @code{spearman (@var{x}, |
|
29 ## @var{x})}. |
3200
|
30 ## |
3453
|
31 ## For two data vectors @var{x} and @var{y}, Spearman's @var{rho} is the |
|
32 ## correlation of the ranks of @var{x} and @var{y}. |
3200
|
33 ## |
3453
|
34 ## If @var{x} and @var{y} are drawn from independent distributions, |
|
35 ## @var{rho} has zero mean and variance @code{1 / (n - 1)}, and is |
|
36 ## asymptotically normally distributed. |
|
37 ## @end deftypefn |
3200
|
38 |
5428
|
39 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
40 ## Description: Spearman's rank correlation rho |
3200
|
41 |
|
42 function rho = spearman (x, y) |
3426
|
43 |
3200
|
44 if ((nargin < 1) || (nargin > 2)) |
6046
|
45 print_usage (); |
3200
|
46 endif |
|
47 |
|
48 if (rows (x) == 1) |
|
49 x = x'; |
|
50 endif |
|
51 n = rows (x); |
3426
|
52 |
3200
|
53 if (nargin == 1) |
|
54 rho = cor (ranks (x)); |
|
55 else |
|
56 if (rows (y) == 1) |
|
57 y = y'; |
|
58 endif |
|
59 if (rows (y) != n) |
3456
|
60 error ("spearman: x and y must have the same number of observations"); |
3200
|
61 endif |
|
62 rho = cor (ranks (x), ranks (y)); |
|
63 endif |
3426
|
64 |
3200
|
65 endfunction |