在matlab中拟合回归线

时间:2013-11-16 14:15:50

标签: matlab

我如何拟合下图的回归线?

  data = [1 72 134 3.2; 2 81 201 3.5; 3 60 156 7.1; 4 82 148 2.4; 5 75 176 1.2]
  rate=data(:,2)
  weight = data(:,3)
  hour = data(:,4)
  scatter3(rate,weight,hour,'filled')

提前感谢。

2 个答案:

答案 0 :(得分:2)

data = [1 72 134 3.2; 2 81 201 3.5; 3 60 156 7.1; 4 82 148 2.4; 5 75 176 1.2] ;
rate=data(:,2) ;
weight = data(:,3) ;
hour = data(:,4) ;
% Regression 
A = [ones(size(weight)), weight , hour];
coeff = A \ rate ; % Thanks to Andreas!  
% Plot data points
figure(1);clf;
scatter3(weight,hour,rate,'filled') ;hold on
% Plot plane
weightLim = [min(weight) max(weight)];
hourLim = [min(hour) max(hour)];
[x1, x2] = meshgrid(weightLim,hourLim);
Z = coeff(1) + coeff(2)*x1 + coeff(3)*x2 ; 
surf(x1,x2,Z)

答案 1 :(得分:0)

你需要回归线吗?

这是最小二乘估计:

y = rate;
X = [data(:, 3:4) ones(numel(y), 1)];
[b,bint,r,rint,stats] = regress(y, X);

b = (X'*X)^(-1)*X'*y

希望你有超过五分......