YNR
YNR

Reputation: 875

How we can compute intercept and slope in statsmodels OLS?

Here I asked how to compute AIC in a linear model. If I replace LinearRegression() method with linear_model.OLS method to have AIC, then how can I compute slope and intercept for the OLS linear model?

import statsmodels.formula.api as smf
regr = smf.OLS(y, X, hasconst=True).fit()

Upvotes: 2

Views: 10731

Answers (2)

Brad Solomon
Brad Solomon

Reputation: 40918

In your example, you can use the params attribute of regr, which will display the coefficients and intercept. They key is that you first need to add a column vector of 1.0s to your X data. Why? The intercept term is technically just the coefficient to a column vector of 1s. That is, the intercept is just a coefficient which, when multiplied by an X "term" of 1.0, produces itself. When you add this to the summed product of the other coefficients and features, to get your nx1 array of predicted values.

Below is an example.

# Pull some data to use in the regression
from pandas_datareader.data import DataReader
import statsmodels.api as sm

syms = {'TWEXBMTH' : 'usd', 
        'T10Y2YM' : 'term_spread', 
        'PCOPPUSDM' : 'copper'
       }

data = (DataReader(syms.keys(), 'fred', start='2000-01-01')
        .pct_change()
        .dropna())
data = data.rename(columns = syms)
# Here's where we assign a column of 1.0s to the X data
# This is required by statsmodels
# You can check that the resulting coefficients are correct by exporting
# to Excel with data.to_clipboard() and running Data Analysis > Regression there
data = data.assign(intercept = 1.)

Now actually running the regression and getting coefficients takes just 1 line in addition to what you have now.

y = data.usd    
X = data.loc[:, 'term_spread':]
regr = sm.OLS(y, X, hasconst=True).fit()
print(regr.params)
term_spread   -0.00065
copper        -0.09483
intercept      0.00105
dtype: float64

So regarding your question on AIC, you'll want to make sure the X data has a constant there as well, before you call .fit.

Note: when you call .fit, you create a regression results wrapper and can access any of the attributes lists here.

Upvotes: 7

serv-inc
serv-inc

Reputation: 38267

For anyone searching on how to get the slope and intercept of a LinearRegression in scikit-learn: it has coef_ and intercept_ properties which show this.

(x, y) = np.random.randn(10,2).T
from sklearn import linear_model
lr = linear_model.LinearRegression()
lr.fit(x.reshape(len(x), 1), y)
lr.coef_ # array([ 0.29387004])
lr.intercept_ # -0.17378418547919167

Upvotes: 3

Related Questions