Spearman correlation in Matlab!
206 次查看(过去 30 天)
显示 更早的评论
Hey Matlab users,
If I have two series of data:
a = [1 4 6 3 4 6 7 8]; b [34 56 34 56 79 23 48 28];
and I want to perform a Spearman correlation what would be the proper command in MATLAB? I need both p-value and RHO.
Thanks for your help.
Mehdi
0 个评论
采纳的回答
Wayne King
2011-10-27
Hi, If you have the Statistics Toolbox.
a = [1 4 6 3 4 6 7 8];
b = [34 56 34 56 79 23 48 28];
[RHO,PVAL] = corr(a',b','Type','Spearman');
0 个评论
更多回答(2 个)
Rithy Khouy
2022-8-25
a = [1 4 6 3 4 6 7 8];
b = [34 56 34 56 79 23 48 28];
[RHO,PVAL] = corr(a',b','Type','Spearman');
0 个评论
DEVANSHI
2025-3-6,9:49
n = input("Enter the number of observations: ");
x = zeros(1, n);
y = zeros(1, n);
for i = 1:n x(i) = input("Enter the x: ");
y(i) = input("Enter the y: ");
end
[sx, idx_x] = sort(x);
rx = zeros(1, n);
AF_x = 0;
i = 1;
while i <= n j = i;
while j <= n && sx(j) == sx(i) j = j + 1;
end avg_rank = mean(i:j-1);
for k = i:j-1 rx(idx_x(k)) = avg_rank;
end
t = j - i;
if t > 1 AF_x = AF_x + (t^3 - t) / 12;
end
i = j;
end
[sy, idx_y] = sort(y);
ry = zeros(1, n);
AF_y = 0;
i = 1;
while i <= n j = i;
while j <= n && sy(j) == sy(i) j = j + 1;
end avg_rank = mean(i:j-1);
for k = i:j-1 ry(idx_y(k)) = avg_rank;
end
t = j - i; if t > 1 AF_y = AF_y + (t^3 - t) / 12;
end
i = j;
end d = rx - ry;
d2 = sum(d .^ 2);
spc = 1 - (6 * (d2 + AF_x + AF_y)) / (n * (n^2 - 1));
disp("Spearman's Rank Correlation Coefficient:");
disp(spc);
for j=1:n
if x(j)<x(i)
yrank=yrank+1
elseif y(j)==y(i)&& j==i
xcount=xcount+1
end
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!