使用python提取回归系数

时间:2019-02-14 01:44:04

标签: python parameters statsmodels glm poisson

我在python中执行了泊松回归,然后执行poisson.fit()。summary以获得以下输出:

poisson.fit().summary()
<class 'statsmodels.iolib.summary.Summary'>
"""
                 Generalized Linear Model Regression Results                  
==============================================================================
Dep. Variable:                    Y   No. Observations:                   28
Model:                            GLM   Df Residuals:                       26
Model Family:                 Poisson   Df Model:                            1
Link Function:                    log   Scale:                          1.0000
Method:                          IRLS   Log-Likelihood:            -1.5464e+07
Date:                Wed, 13 Feb 2019   Deviance:                   3.0928e+07
Time:                        19:54:52   Pearson chi2:                 4.43e+07
No. Iterations:                     6   Covariance Type:             nonrobust
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept     12.8383      0.000   2.95e+04      0.000      12.837      12.839
x            0.0094   1.11e-05    848.646      0.000       0.009       0.009
==============================================================================

但是,我的问题是,如何才能分别提取intercept和X值?

我尝试过poisson.params(如先前文章中所建议),但是它似乎对我不起作用。我收到这样的错误

*** AttributeError: 'GLM' object has no attribute 'params'

我希望每个系数都存储在单独的变量中:

Intercept =  12.8383
X = 0.0094

这可能吗?

1 个答案:

答案 0 :(得分:2)

没有代码,很难说出为什么您会看到自己的行为吗?

这是一个有效的示例代码。

import numpy as np
import pandas as pd

import statsmodels.api as sm
import statsmodels.formula.api as smf


df = pd.DataFrame(np.random.randint(100, size=(50,2)))
df.rename(columns={0:'X1', 1:'X2'}, inplace=True)

# GLM Model

model = smf.glm("X2 ~ X1", data=df, family= sm.families.Poisson()).fit()

print(model.summary())
print(model.params)


# Poisson Model

poisson = smf.poisson("X2 ~ X1", data=df).fit()
print (poisson.summary())
print (poisson.params)