Skip to content

BUG: incorrect pearson_chi2 with binomial counts #3612

Closed

Description

@josef-pkt

discovered while checking #3410
#3410 (comment)

self._n_trials is missing in pearson_chi2 but not in resid_pearson
only resid_pearson has verifying unit tests but not pearson_chi2

correction
test_glm.py has test_pearson_chi2 in the Check mixin
pearson_chi2 is in the binomial Stata results Star98

to confirm the pearson_chi2 bug with binomial in master

last example of gist notebook (I renamed res to unique names)

(res_binom.resid_pearson**2).sum(), res_binom.pearson_chi2, (res_binom_wt.resid_pearson**2).sum(), res_binom_wt.pearson_chi2
Out[24]:
(4053.3993437537383, 9.5037638756406455, 4053.399343753348, 4053.3993437533463)

Activity

  1. added
    type-bug-wrong
    on Apr 17, 2017
  2. josef-pkt commented on Mar 28, 2018

    @josef-pkt
    MemberAuthor

    something fishy, either I'm not thinking correctly, or the unit tests for pearson_chi2 are only regression tests.
    statsmodels.datasets.star98 has a two column endog, so this should be a Binomial with n_trials > 1.

    ???

  3. josef-pkt commented on Mar 28, 2018

    @josef-pkt
    MemberAuthor

    Running the unit test example in R, script is in tests/results folder,
    I get the same number as in the unit test for pearson chi2.
    Note, R glm summary does not compute pearson chi2, but can compute pearson residuals
    base on https://stats.stackexchange.com/questions/29653/how-can-i-compute-pearsons-chi2-test-statistic-for-lack-of-fit-on-a-logisti

    > sum(residuals(m1, type = "pearson")^2)
    [1] 4051.921
    

    additional check, deviance and aic from the R glm summary are the same as in the unit tests (at least at the print precision that I see in R right now)

  4. josef-pkt commented on Mar 28, 2018

    @josef-pkt
    MemberAuthor

    results in Stata are also the same as in the unit tests

    set type double
    import delimited "M:\...\statsmodels\datasets\star98\star98.csv", clear
    
    glm  pr50m lowinc perasian perblack perhisp perminte avyrsexp avsalk perspenk ptratio pctaf pctchrt pctyrrnd perminte_avyrsexp perminte_avsal avyrsexp_avsal perspen_ptratio perspen_pctaf ptratio_pctaf perminte_avyrsexp_avsal perspen_ptratio_pctaf, family(binomial mathtot)
    
    
                      e(N) =  303
                     e(ic) =  3
                      e(k) =  21
                   e(k_eq) =  1
                   e(k_dv) =  1
              e(converged) =  1
                     e(ll) =  -2998.612558993971
                   e(chi2) =  26631.39532026602
                      e(p) =  0
                     e(rc) =  0
                    e(aic) =  19.9314360329635
                   e(rank) =  21
                  e(power) =  0
                   e(df_m) =  20
                     e(df) =  282
                     e(vf) =  1
                    e(phi) =  1
             e(k_eq_model) =  0
                   e(nbml) =  0
                    e(bic) =  2467.492766564803
             e(dispers_ps) =  14.36851423114536
            e(deviance_ps) =  4051.921013182992
              e(dispers_p) =  14.36851423114536
             e(deviance_p) =  4051.921013182992
              e(dispers_s) =  14.46370715502995
             e(deviance_s) =  4078.765417718445
                e(dispers) =  14.46370715502995
               e(deviance) =  4078.765417718445
    
  5. josef-pkt commented on Mar 29, 2018

    @josef-pkt
    MemberAuthor

    still weird, but not a bug_wrong
    maybe (likely) I was doing or reading something in the wrong way

    the example is binomial, and the numbers are close to Stata at pretty good precision given that we do nonlinear optimization.

    >>> tst.res1.model.n_trials
    array([ 807.,  184.,  571., ...,  595.,  709.,  156.])
    
    >>> from statsmodels.genmod.tests.test_glm import TestGlmBinomial
    loading pytest
    >>> TestGlmBinomial.setup_class()
    >>> tst = TestGlmBinomial()
    >>> tst.res1.pearson_chi2
    4051.9210131831724
    >>> tst.res1.deviance
    4078.7654177184513
    >>> tst.res1.aic
    6039.2251179879513
    
    >>> (tst.res1.resid_pearson**2).sum() / tst.res1.df_resid
    14.368514231145999
    >>> (tst.res1.resid_pearson**2).sum()
    4051.9210131831719
    

    aside we are using the same definition for AIC as R
    from the test suite
    self.aic_R = 6039.22511799

  6. added and removed
    type-bug-wrong
    on Mar 29, 2018
  7. josef-pkt commented on Mar 29, 2018

    @josef-pkt
    MemberAuthor

    removing labels related to bug-wrong

  8. josef-pkt commented on Mar 29, 2018

    @josef-pkt
    MemberAuthor

    I was not thinking wrongly, the bug was fixed in 7631bba#diff-bacf4e7dec296273aba74b04be3c42f8 with an innocent sounding commit message
    part of PR #3692 which also added the unit tests for pearson_chi2

    (So it's not possible to replicate the bug with current master :)

    closing

  9. added
    type-bug-wrong
    and removed on Mar 29, 2018
  10. added this to the 0.9 milestone on Mar 29, 2018
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-genmodtype-bug-wrong

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions