OLS 拦截的 statsmodels add_constant,这实际上在做什么?

statsmodels add_constant for OLS intercept, what is this actually doing?

通过 statsmodels OLS 拟合审查线性回归 我发现您必须在拟合之前使用 add_constant 将常数“1”添加到自变量中的所有点。然而,我对这种情况下截距的唯一理解是当我们的 x 等于 0 时我们的行的 y 值,所以我不清楚总是在这里注入“1”的目的是什么。这个常量实际上告诉 OLS 拟合是什么?

它不会向您的值添加一个常数,它会向它拟合的线性方程添加一个常数项。在单预测器的情况下,这是将一条线 y = mx 拟合到您的数据与拟合 y = mx + b.

之间的区别
statsmodel 中的

sm.add_constant 与 linearRegression() 中 sklearn 的 fit_intercept 参数相同。如果您不执行 sm.add_constant 或当 LinearRegression(fit_intercept=False) 执行时,则 statsmodels 和 sklearn 算法都假定 y = mx + b 中的 b=0,并且它将适合使用的模型b=0 而不是根据您的数据计算 b 应该是什么。