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
#2028 doesn't fix one of the issues I am having with GLM: if any endog == 0 for negative binomial regression, the residual deviances are NaN. The changes as recently merged via #2028 effectively have a term notzero * log(endog/mu), which is still NaN even when endog is zero.
A related issue, the shape parameter in a NegativeBinomial family should be converted to a float when saving as an attribute.
Right now, if I pass an integer greater than 1 into the NegativeBinomial family, e.g. alpha=2 (easy to do by accident), then in the arithmetic for loglike we have 1/self.alpha, so we get 0. In particular if any endog == 0, we end up taking gammaln(endog + 1/alpha) = NaN, so I get a NaN value for llf (but we would have gotten the wrong llf in any case).
Activity
kshedden commentedon Oct 10, 2014
A related issue, the shape parameter in a NegativeBinomial family should be converted to a float when saving as an attribute.
Right now, if I pass an integer greater than 1 into the NegativeBinomial family, e.g. alpha=2 (easy to do by accident), then in the arithmetic for loglike we have 1/self.alpha, so we get 0. In particular if any endog == 0, we end up taking gammaln(endog + 1/alpha) = NaN, so I get a NaN value for llf (but we would have gotten the wrong llf in any case).
jseabold commentedon Oct 10, 2014
Is it possible to make a small example that fails?
kshedden commentedon Oct 10, 2014
I get NaN in the last line from the following example:
import numpy as np
import statsmodels.api as sm
n = 200
p = 3
exog = np.random.normal(size=(n,p))
exog[:, 0] = 1
lin_pred = 4 + exog[:, 1] + exog[:, 2]
expval = np.exp(lin_pred)
endog = np.random.poisson(expval)
endog[0] = 0
model = sm.GLM(endog, exog, family=sm.families.NegativeBinomial(alpha=1.))
result = model.fit()
print result.resid_deviance[0]
On Fri, Oct 10, 2014 at 12:00 AM, Skipper Seabold [email protected]
wrote:
josef-pkt commentedon Jun 29, 2015
family NegativeBinomial doesn't have 0log0 protection. But, AFAICS this only appears in resid_dev.
I guess this would mean that it is broken for all data with endog=0. Do we have unit tests?
BUG genmod NegativeBinomial float alpha closes statsmodels#2037
Merge pull request #2487 from josef-pkt/bug_glm_negbin_2037
BUG genmod NegativeBinomial float alpha closes statsmodels#2037