且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在statsmodels OLS中计算截距和斜率?

更新时间:2022-12-09 19:35:50

在您的示例中,您可以使用 regr params 属性,该属性将显示系数和截距.它们的关键是您首先需要向X数据添加 1.0 s的列向量.为什么?从技术上讲,截距项只是对列向量1s的系数.也就是说,截距只是一个系数,当乘以X的项" 1.0时,会产生自身.将其添加到其他系数和特征的总和后,即可得到nx1个预测值数组.

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.

下面是一个例子.

# 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.)

现在实际运行回归并获取系数仅需要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

因此,关于您在 AIC ,您需要确保在调用 .fit 之前,X数据在其中也具有常量.

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.

注意:调用 .fit 时,您将创建一个回归结果包装器,并且可以访问

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