MATLAB中是否存在相关比?

时间:2019-04-22 20:04:33

标签: matlab statistics correlation

Matlab中是否有任何计算correlation ratio的函数? 这是我尝试执行的实现,但结果不正确。

function cr = correlation_ratio(X, Y, L)
ni = zeros(1, L);
sigmai = ni;

for i = 0:(L-1)
   Yn = Y(X == i);
   ni(1, i+1) = numel(Yn);
   m = (1/ni(1, i+1))*sum(Yn);
   sigmai(1, i+1) = (1/ni(1, i+1))*sum((Yn - m).^2);
end
n = sum(ni);
prod = ni.*sigmai;
cr = (1-(1/n)*sum(prod))^0.5;

1 个答案:

答案 0 :(得分:1)

这是the Wikipedia page上的等式:

correlation ratio

其中:

  • η是相关比,
  • y x,i 是样本值( x 是类标签, i 样本索引),
  • y x (带顶部的横条)是 x
  • 类的样本值的平均值
  • y (条形图在顶部)是所有类别中所有样本的平均值,并且
  • n x x 类中的样本数。

这就是我将其解释为代码的方式:

function eta = correlation_ratio(X, Y)
X = X(:); % make sure we've got column vectors, simplifies things below a bit
Y = Y(:);
L = max(X);
mYx = zeros(1, L+1); % we'll write mean per class here
nx = zeros(1, L+1);  % we'll write number of samples per class here
for i = unique(X).'
   Yn = Y(X == i);
   if numel(Yn)>1
      mYx(i+1) = mean(Yn);
      nx(i+1) = numel(Yn);
   end
end
mY = mean(Y);        % mean across all samples
eta = sqrt(sum(nx .* (mYx - mY).^2) / sum((Y-mY).^2));

可以将循环替换为accumarray