Skip to content

NegativeBinomial vs summary2(): like Hayek vs Keynes #2349

Closed

Description

@kousu

I don't know why but summary2() is not getting along with NegativeBinomial.

rhDNase2.txt

"id" "trt" "fev" "count" "time"
493301 1 28.8 0 168
493303 1 64 0 169
493305 0 67.2 2 168
493309 1 57.6 0 168
493310 0 57.6 0 171
....

bug.py

import statsmodels.api as sm
import pandas

print(sm.version.full_version)

cystic = pandas.read_csv("./rhDNase2.txt", sep=" ", index_col=False)

model = sm.GLM.from_formula("count ~ C(trt) + fev", data=cystic, family=sm.families.NegativeBinomial())
model.exog_names[1] = "trt"
fit = model.fit()
print(fit.summary()) #works
print(fit.summary2())#->TypeError: loglike() got an unexpected keyword argument 'scale'

output

[kousu@galleon A4]$ python bug.py 
0.6.1
                 Generalized Linear Model Regression Results                  
==============================================================================
Dep. Variable:                  count   No. Observations:                  645
Model:                            GLM   Df Residuals:                      642
Model Family:        NegativeBinomial   Df Model:                            2
Link Function:                    log   Scale:                   1.40673520006
Method:                          IRLS   Log-Likelihood:                -914.54
Date:                Fri, 03 Apr 2015   Deviance:                       775.69
Time:                        01:11:12   Pearson chi2:                     903.
No. Iterations:                     8                                         
==============================================================================
                 coef    std err          z      P>|z|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept      1.1639      0.175      6.663      0.000         0.822     1.506
trt           -0.2740      0.132     -2.079      0.038        -0.532    -0.016
fev           -0.0165      0.003     -6.172      0.000        -0.022    -0.011
==============================================================================
Traceback (most recent call last):
  File "bug.py", line 12, in <module>
    print(fit.summary2())#->TypeError: loglike() got an unexpected keyword argument 'scale'
  File "/usr/lib/python3.4/site-packages/statsmodels/genmod/generalized_linear_model.py", line 1124, in summary2
    xname=xname, yname=yname, title=title)
  File "/usr/lib/python3.4/site-packages/statsmodels/iolib/summary2.py", line 137, in add_base
    info = summary_model(results)
  File "/usr/lib/python3.4/site-packages/statsmodels/iolib/summary2.py", line 293, in summary_model
    out[key] = func(results)
  File "/usr/lib/python3.4/site-packages/statsmodels/iolib/summary2.py", line 283, in <lambda>
    info['LL-Null:'] = lambda x: "%#8.5g" % x.llnull
  File "/usr/lib/python3.4/site-packages/statsmodels/tools/decorators.py", line 94, in __get__
    _cachedval = self.fget(obj)
  File "/usr/lib/python3.4/site-packages/statsmodels/genmod/generalized_linear_model.py", line 984, in llnull
    return self.family.loglike(self._endog, self.null, scale=self.scale)
TypeError: loglike() got an unexpected keyword argument 'scale'
[kousu@galleon A4]$ 

Activity

  1. josef-pkt commented on Apr 3, 2015

    @josef-pkt
    Member

    It works in master, looks like it was fixed in 67e7e1e
    signature of family NegativeBinomial.loglike was wrong.

    but it looks like llnull doesn't have test coverage, and no smoke tests for summary/summary2 for all families.

  2. josef-pkt commented on Apr 3, 2015

    @josef-pkt
    Member

    There is still a bug in families.NegativeBinomial.loglike
    scale is not actually used in the loglike calculation, and it should be a keyword argument scale=1

    AFAIR, we don't use the scale here yet for over/underdispersed QMLE, and won't have tests for
    scale !=1
    relevant for LR test which works for QMLE if the cov_type implies only a scaling factor. see #1755

  3. added this to the 0.7 milestone on Apr 3, 2015
  4. josef-pkt commented on Apr 3, 2015

    @josef-pkt
    Member

    another comment: From what I can see, summary2 was picking up llnull automatically after it was added.

  5. josef-pkt commented on Apr 3, 2015

    @josef-pkt
    Member

    @kousu Thanks reporting the issue.

  6. removed this from the 0.7 milestone on Apr 6, 2021
  7. bashtage commented on Jul 30, 2026

    @bashtage
    Member

    Fixed as part of the GLM frequency-weights refactor: Family.loglike signature now includes scale, and NegativeBinomial (no override) inherits it correctly with scale actually used in the calculation. summary2() no longer crashes. Closing as fixed.

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions