简体   繁体   中英

Using python to extract regression coefficients

I performed a poisson regression in python and then did poisson.fit().summary to get the following output:

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
==============================================================================

However, my question is, how can I extract the intercept and X value separately?

I tried poisson.params (as suggested in previous posts), but it doesn't seem to work for me. I get such errors

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

I expect for each of the coefficients to be stored in separate variables:

Intercept =  12.8383
X = 0.0094

is this possible?

Doe without code, its hard to say why you are getting the behaviour you are seeing?

Here's a sample complete code that works.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM