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 |
3200
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## 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 |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
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 |
3456
|
39 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
40 ## Description: Spearman's rank correlation rho |
3200
|
41 |
|
42 function rho = spearman (x, y) |
3426
|
43 |
3200
|
44 if ((nargin < 1) || (nargin > 2)) |
3456
|
45 usage ("spearman (x, y)"); |
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 |