Extracting coefficients from GLM in Python using statsmodel

假装没事ソ 提交于 2019-12-05 19:37:51

问题


I have a model which is defined as follows:

import statsmodels.formula.api as smf
model = smf.glm(formula="A ~ B + C + D", data=data, family=sm.families.Poisson()).fit()

The model has coefficients which look like so:

Intercept   0.319813
C[T.foo]   -1.058058
C[T.bar]   -0.749859
D[T.foo]    0.217136
D[T.bar]    0.404791
B           0.262614

I can grab the values of the Intercept and B by doing model.params.Intercept and model.params.B but I can't get the values of each C and D.

I have tried model.params.C[T.foo] for example, and I get and error.

How would I get particular values from the model?


回答1:


model.params is is a pandas.Series. Accessing as attribute is only possible if the name of the entry is a valid python name.

In this case you need to index with the name in quotes, i.e. model.params["C[T.foo]"]

see http://pandas.pydata.org/pandas-docs/dev/indexing.html



来源:https://stackoverflow.com/questions/29165601/extracting-coefficients-from-glm-in-python-using-statsmodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!