这是另一种使用公式的方法,因为您需要导入
statsmodels.formula.api
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
y = [76.67251,140.40808,138.26660,108.20993,53.46417,110.61754,
119.11950,113.57558,85.82045,71.96892,76.81693,86.00139,
93.62010,69.49795,121.99775,114.18707,125.43608,120.63640]
df = pd.DataFrame({'y':y})
model = smf.glm(formula = 'y ~ 1', data = df, family=sm.families.Gamma()).fit()
model.summary()
<class 'statsmodels.iolib.summary.Summary'>
"""
Generalized Linear Model Regression Results
==============================================================================
Dep. Variable: y No. Observations: 18
Model: GLM Df Residuals: 17
Model Family: Gamma Df Model: 0
Link Function: inverse_power Scale: 0.062556
Method: IRLS Log-Likelihood: -83.656
Date: Sun, 20 May 2018 Deviance: 1.1761
Time: 22:00:54 Pearson chi2: 1.06
No. Iterations: 6 Covariance Type: nonrobust
==============================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------
Intercept 0.0099 0.001 16.963 0.000 0.009 0.011
==============================================================================
"""