Skip to content

More numerical issues in genmod families #2037

Closed

Description

@kshedden

#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.

Activity

  1. kshedden commented on Oct 10, 2014

    @kshedden
    ContributorAuthor

    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).

  2. jseabold commented on Oct 10, 2014

    @jseabold
    Member

    Is it possible to make a small example that fails?

  3. kshedden commented on Oct 10, 2014

    @kshedden
    ContributorAuthor

    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:

    Is it possible to make a small example that fails?

    —
    Reply to this email directly or view it on GitHub
    #2037 (comment)
    .

  4. added this to the 0.7 milestone on Jan 11, 2015
  5. josef-pkt commented on Jun 29, 2015

    @josef-pkt
    Member

    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?

  6. added a commit that references this issue on Jun 29, 2015
    80289ec
  7. added a commit that references this issue on Jun 29, 2015
  8. added a commit that references this issue on Jul 4, 2015
    63f1809
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions