Spearman correlation in Matlab!

95 ビュー (過去 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 Center および File ExchangeCorrelation and Convolution についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by