【问题标题】:Regression lines for cluster of points in MatlabMatlab中点簇的回归线
【发布时间】:2014-03-07 05:06:56
【问题描述】:

我可以在 Matlab 中用一组 x,y 点绘制回归线。但是,如果我有一个点簇(如下图所示),假设我有四个点簇,我想为它们绘制四个回归线。我该怎么做?所有点都保存在 x,y 中。没有办法将它们分开并放入四组不同的变量中。

见下图。忽略图例和标签。知道如何在 Matlab 中做到这一点吗?如果只有一个集群,我可以做到。但我想同时为所有四个集群做。

我现在用于一个集群的代码:

 %----------- Linear regression -----------------
 p= polyfit(x,y,1);
 f= polyval(p,x);
 %----------- Call R-square function ------------
 r2=Rsquare(x,y,p);


 %------------- Plot data -----------------------
 figure()
 plot(x,y,'*k');hold on
 plot(x,f,'-r'); % show linear fit
 xlabel('index');
 ylabel('Intensity a.u.');
 title('Test: Linear regreesion && R-square');
 %------- Show y-data on current figure ---------
 [row col]=size(y);
 for i=1:col
 str=num2str(y(i)); 
 text(x(i),y(i),str,'Color',[0 0 1]);
 end
 %--Show linear equation on current figure -------
 m1=num2str(p(1));c1=num2str(p(2));Rsquare1=num2str(r2(1));
 text(1.05,80,['y= ',m1,'x+',c1,' , R^2= ',Rsquare1,'.'],'FontSize',10,'FontName','Times New           Roman');

【问题讨论】:

  • 您能否发布适用于一组点的代码?
  • 已添加到帖子中。请检查。
  • 你可以使用矩阵索引,比如polyfit(x(1:10),y(1:10),1)吗?为什么不能将它们拆分为单独的变量?
  • 我可以。但这将是一项乏味的工作。因为我的数据很大。并且这种手工工作会出现错误。可能是我现在无法使用矩阵索引。
  • 如果你找不到某种方法来分离你想要拟合的值,那么 MATLAB 就无法“自动”知道你想要做什么......你是如何将数据填充到x,y 矩阵?

标签: matlab 2d regression points


【解决方案1】:

您必须将您的值分成多个集群。这是一个不平凡的操作。这可以通过统计工具箱中的kmeans 来完成,例如:

%// First, I generate some example data in 4 clusters. 

%// intercepts
a = [4 7  0 -5];

%// slopes
b = [0.7 1.0 1.0 0.8];

%// ranges
xmin = [+1  -6  -6  +1];
xmax = [+6  -1  -1  +6];

%// generate clusters 
N = [30 40 25 33];
X = arrayfun(@(ii) (xmax(ii)-xmin(ii))*rand(N(ii),1) + xmin(ii), 1:4, 'UniformOutput', false);
Y = arrayfun(@(ii) a(ii) + b(ii)*X{ii} + randn(size(X{ii})), 1:4, 'UniformOutput', false);


%// Unfortunately, your points not are given in 4 separate clusters, but 
%// in a single array:
X = cat(1,X{:});
Y = cat(1,Y{:});

%// Therefore, you'll have to separate the data again into clusters: 
idx = kmeans([X,Y], 4, 'Replicates', 2);

X = {
    X(idx==1)
    X(idx==2)
    X(idx==3)
    X(idx==4)
};

Y = {
    Y(idx==1)
    Y(idx==2)
    Y(idx==3)
    Y(idx==4)
};


%// Now perform regression on each cluster
ab = arrayfun(@(ii) [ones(size(X{ii})) X{ii}]\Y{ii}, 1:4, 'UniformOutput', false);

%// the original values, and the computed ones
%// note that the order is not the same!
[a; b]
[ab{:}]

%// Plot everything for good measure
figure(1), clf, hold on

plot(...
    X{1}, Y{1}, 'g.',...
    X{2}, Y{2}, 'b.',...
    X{3}, Y{3}, 'r.',...
    X{4}, Y{4}, 'c.')

line([min(X{1}); max(X{1})], ab{1}(1) + ab{1}(2)*[min(X{1}); max(X{1})], 'color', 'k')
line([min(X{2}); max(X{2})], ab{2}(1) + ab{2}(2)*[min(X{2}); max(X{2})], 'color', 'k')
line([min(X{3}); max(X{3})], ab{3}(1) + ab{3}(2)*[min(X{3}); max(X{3})], 'color', 'k')
line([min(X{4}); max(X{4})], ab{4}(1) + ab{4}(2)*[min(X{4}); max(X{4})], 'color', 'k')

结果:

ans =
    4.0000    7.0000         0   -5.0000
    0.7000    1.0000    1.0000    0.8000
ans =
   -4.6503    6.4531    4.5433   -0.6326
    0.7561    0.8916    0.5914    0.7712

考虑到不同的顺序(看图中的颜色),这些结果确实是你所期望的,因为我放了很大程度的噪音:)

【讨论】:

  • 您好,感谢您提供的解决方案。它有问题。我有一些垂直分布的点。因此,对于特定情况,我希望回归线处于垂直方向。可能吗?而且,我希望我的标记(点)是透明的......我没有找到办法......如果你能帮忙,请告诉我......
猜你喜欢
  • 2012-06-27
  • 2013-10-25
  • 2014-09-21
  • 2014-04-17
  • 2016-05-24
  • 1970-01-01
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多