Search code examples
pythontypeerrorstatsmodelsglm

Python statsmodels.glm - TypeError when family=Poisson()


I have a list y of endogenous values where len(y) = n I have a n x 5 nympy array X of exogenous values

when I run

import statsmodels.api as sm
sm.GLM(y, X).fit().summary() 

everything runs fine.

However, when I add the family argument:

sm.GLM(y, X, family=Poisson()).fit().summary()

I get an error:

TypeError: __init__() missing 2 required positional arguments: 'endog' and 'exog'

I have tried recasting the y object as numpy array, I tried explicitly declaring arguments - nothing seems to help and cannot find anything in docs to help.


Solution

  • The usage of GLM is correct.

    However, I guess your Poisson is the discrete_model.Poisson which is a separate model. GLM needs the family Poisson.

    The correct usage using the api should be

    sm.GLM(y, X, family=sm.families.Poisson()).fit().summary()
    

    or with direct imports, from an example that I was working on

    from statsmodels.genmod.generalized_linear_model import GLM
    from statsmodels.genmod import families
    
    mod = GLM(y, x, family=families.Poisson())
    res = mod.fit()
    print(res.summary())