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