在MATLAB中绘制线性回归结果

时间:2017-10-29 12:17:24

标签: matlab plot regression

我正在使用MATLB的fitlm函数来对我的数据进行线性回归。这很容易完成。我还不确定如何绘制我的数据。例如,从回归表中,如果运行下面的代码,我认为回归线将具有截距= 0.023851和斜率= 0.56421。但是,当我按照MATLAB的指令确定截距和斜率时(见下面的代码),我得到了其他值。哪个是正确的,为什么会这样?

x = [0.0001;0.066578214;0.09611659;0.075839223;0.125;0.037889785;0.070220426;0.070648;0.082886425;0.095050538;0.058966667;0.0456;0.070994624;0.048540228;0.06561828;0.053916667;0.035954301;0.037634409;0.044335551;0.061270917;0.163333333;0.079986559;0.070616667]

y = [0.082;0.0452;0.072340;0.0543;0.0932;0.0321;0.078;0.06021;0.0734;0.103;0.0436;0.0482;0.08732;0.05421;0.0589;0.04321;0.043215;0.054321;0.05467;0.0432;0.109;0.0723;0.09821]

mdl = fitlm(x,y,'linear','RobustOpts','on')

%% plot raw data
hold on

plot(x,y,'*') % plot datapoints

%% as suggested by matlab on https://ch.mathworks.com/help/matlab/data_analysis/linear-regression.html
X = [ones(length(x),1) x];
b = X\y % this is however another intercept as seen in the table!

yCalc2 = X*b;
plot(x,yCalc2,'-')
legend('Data','Slope','Slope & Intercept','Location','best');

1 个答案:

答案 0 :(得分:2)

您正在调用mdlRobustOpts值对设置为on。这将使该功能使用稳健的拟合功能。因此,结果通常不同于最小二乘法。您可以尝试运行mdl = fitlm(x,y,'linear','RobustOpts','off')并查看结果与使用\运算符(最小二乘)相同。不可能说哪些值是正确的",它们只是使用不同的方法获得,即优化不同的拟合函数。

相关问题