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

Tim Lindsey picture Tim Lindsey · Dec 31, 2016 · Viewed 8.8k times · Source

Reviewing linear regressions via statsmodels OLS fit I see you have to use add_constant to add a constant '1' to all your points in the independent variable(s) before fitting. However my only understanding of intercepts in this context would be the value of y for our line when our x equals 0, so I'm not clear what purpose always just injecting a '1' here serves. What is this constant actually telling the OLS fit?

Answer

BrenBarn picture BrenBarn · Dec 31, 2016

It doesn't add a constant to your values, it adds a constant term to the linear equation it is fitting. In the single-predictor case, it's the difference between fitting an a line y = mx to your data vs fitting y = mx + b.