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