You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
zero values in Poisson GLM / perfect prediction in GLM #1391
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']
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.
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.
Activity
jseabold commentedon Feb 14, 2014
Apparently sometimes converged is True, sometimes I get a not necessarily met precision warning.
josef-pkt commentedon Feb 14, 2014
adding a dummy to capture the effect the the "outlier" data
R-squared for doubly-constrained model is: 0.975327876037However, 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.
josef-pkt commentedon Sep 20, 2014
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
Somedaysince I don't see anything obvious to do.