-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspearman.m
78 lines (78 loc) · 1.6 KB
/
spearman.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
fprintf("\n");
n=input("Enter the number of observations : ");
fprintf("Enter the observations \n");
for i=1:n
fprintf("Enter x(%d):",i);
x(i)=input("");
fprintf("Enter y(%d):",i);
y(i)=input("");
end
summx=0;
x1=sort(x,'descend');
i=1;
while i<=n
t=1;
for j=i+1 : n
if x1(j)==x1(i)
t=t+1;
end
end
summx=summx+(t*(t*(t)-1));
if t~=1
m= i+t-1;
a=i:m;
for k=i:m
rx1(k)=mean(a);
end
i=i+t;
else
rx1(i)=i;
i=i+1;
end
end
for i=1:n
for j=1:n
if x(i)==x1(j)
rx(i)=rx1(j);
end
end
end
y1=sort(y,'descend');
i=1;
summy=0;
while i<=n
t=1;
for j=i+1 : n
if y1(j)==y1(i)
t=t+1;
end
end
summy=summy+(t*(t*(t)-1));
if t~=1
m= i+t-1;
a=i:m;
for k=i:m
ry1(k)=mean(a);
end
i=i+t;
else
ry1(i)=i;
i=i+1;
end
end
for i=1:n
for j=1:n
if y(i)==y1(j)
ry(i)=ry1(j);
end
end
end
sumd2=0;
for i=1:n
d(i)=rx(i)-ry(i);
d2(i)=d(i)*d(i);
sumd2=sumd2+d2(i);
end
AF=1/12*(summx+summy);
r=1-6*((sumd2+AF)/(n*(n*n-1)));
fprintf("The spearman's rank correlation cofficient = %f",r);