Spearman correlation in Matlab!

조회 수: 88 (최근 30일)
M G
M G 2011년 10월 27일
답변: DEVANSHI 2025년 3월 6일
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

채택된 답변

Wayne King
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');

추가 답변 (2개)

Rithy Khouy
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');

DEVANSHI
DEVANSHI 2025년 3월 6일
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

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by