【问题标题】:Linear regression (regress) discrepancy with polynomial fit (polyfit)线性回归(regress)与多项式拟合(polyfit)的差异
【发布时间】:2023-11-23 18:34:01
【问题描述】:

我有一些来自线性函数 (y=mx+c) 的数据,其中 m=4, c=1 (so: y=4x+1)。

当我尝试使用 regress 取回系数时,我得到一个 R2m 值:

x = [1 2 3 4]
y = [5 9 13 17]
[m,bint,r,rint,stats] = regress(y',x');

%{
>> R = stats(1) % Coefficient of determination
R =
     1
>> m % Linear function coefficients
m = 
     4.333333333333333
%}

polyfit 这样做是正确的:

P = polyfit(x,y,1);

%{
>> P(1)
ans =
    4.000000000000000
>> P(2)
ans =
    1.000000000000000
%}

为什么会这样?

【问题讨论】:

    标签: matlab linear-regression curve-fitting least-squares polynomials


    【解决方案1】:

    您的问题的根源不是遵循文档或regress,其中指出:

    b = regress(y,X) 返回系数估计的向量 b,用于向量 y 中的响应对矩阵 X 中的预测变量进行多元线性回归。 矩阵X 必须包含一列

    如果我们在 2nd 输入中包含一列,我们会得到所需的结果:

    x = [1 2 3 4].';
    y = [5 9 13 17].';
    [m,bint,r,rint,stats] = regress(y,[ones(size(x)) x]);
    
    %{
    Results:
    m =
        1.0000
        4.0000
    bint =
        1.0000    1.0000
        4.0000    4.0000
    r =
       1.0e-14 *
        0.1776
        0.1776
        0.1776
             0
    rint =
       1.0e-13 *
        0.0178    0.0178
       -0.2190    0.2545
       -0.2190    0.2545
       -0.2141    0.2141
    stats =
       1.0e+31 *
        0.0000    1.6902    0.0000    0.0000
    %}
    
    

    【讨论】:

    • 谢谢,我自己解决了这个问题,谢谢回答