Skip to content

zero values in Poisson GLM / perfect prediction in GLM #1391

Open

Description

@jseabold

Are we guarding against these things?

https://stackoverflow.com/questions/21785049/statsmodels-poisson-glm-different-than-r/21788917#21788917

import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
import statsmodels.api as sm
from scipy.stats.stats import pearsonr

data = pd.read_csv('https://gist.github.com/jseabold/9008683/raw/250ff03e6a3ab3514fe4c0e2a95d4b5fcab9aad1/AT_Austria.csv')

What's going on here? The parameters are wrong, it converges to a big negative number

Model = smf.glm('Data~Origin+Destination+Dij', data=data, offset=np.log(data['Offset']), family=sm.families.Poisson()).fit()

This matches R's GLM (default solver fails, bfgs does something at least), but isn't necessarily converged.

mod = smf.poisson('Data~Origin+Destination+Dij', data=data, offset=np.log(data['Offset'])).fit(method='bfgs')
print mod.mle_retvals['converged']

R gives these warnings

Warning messages:
1: glm.fit: fitted rates numerically 0 occurred 
2: glm.fit: fitted rates numerically 0 occurred 

Activity

  1. jseabold commented on Feb 14, 2014

    @jseabold
    MemberAuthor

    Apparently sometimes converged is True, sometimes I get a not necessarily met precision warning.

  2. added this to the 0.6 milestone on Feb 14, 2014
  3. josef-pkt commented on Feb 14, 2014

    @josef-pkt
    Member

    adding a dummy to capture the effect the the "outlier" data

    Data['dummy'] = (Data['Dij'] < 1e-25).astype(int)
    Data2 = Data
    Model = smf.glm('Data~Origin+Destination+Dij+dummy', data=Data2, offset=np.log(Data2['Offset']), family=sm.families.Poisson(link=sm.families.links.log)).fit()
    

    R-squared for doubly-constrained model is: 0.975327876037

    However, it doesn't work for Poisson.

    I haven't checked the code: IIRC somewhere we have to do some clipping for the corner case. This might happen in this case, and R and statsmodels treat the numerical corner in a different way.

    I don't know where or for which condition we should raise a warning or exception.

  4. modified the milestones: Someday, 0.6 on Sep 20, 2014
  5. josef-pkt commented on Sep 20, 2014

    @josef-pkt
    Member

    from the comments to the stackoverflow question, the reason for the difference between R and statsmodels seems to be different trimming of extreme observations (to avoid overflow or similar)

    moving to milestone Someday since I don't see anything obvious to do.

  6. added
    type-possible-bug
    on Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-genmodcorner-casetype-possible-bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions