Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting coefficients from GLM in Python using statsmodel

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?

like image 672
user2844485 Avatar asked Mar 20 '15 11:03

user2844485


People also ask

How do you find the coefficient in Python?

The Pearson Correlation coefficient can be computed in Python using corrcoef() method from Numpy. The input for this function is typically a matrix, say of size mxn , where: Each column represents the values of a random variable. Each row represents a single sample of n random variables.

What is Statsmodel in Python used for?

statsmodels is a Python module that provides classes and functions for the estimation of many different statistical models, as well as for conducting statistical tests, and statistical data exploration. An extensive list of result statistics are available for each estimator.

What is statsmodels formula API?

statsmodels. formula. api : A convenience interface for specifying models using formula strings and DataFrames. This API directly exposes the from_formula class method of models that support the formula API.


1 Answers

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

like image 163
Josef Avatar answered Oct 18 '22 05:10

Josef