【问题标题】:plotting linear regression results in MATLAB在 MATLAB 中绘制线性回归结果
【发布时间】:2018-04-10 12:53:51
【问题描述】:

我正在使用 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');

【问题讨论】:

    标签: matlab plot regression


    【解决方案1】:

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

    【讨论】:

    • 非常感谢,我真的很想了解其中的区别!
    猜你喜欢
    • 1970-01-01
    • 2015-11-02
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 2019-10-20
    • 1970-01-01
    • 2021-11-23
    相关资源
    最近更新 更多