Skip to content

BUG: GLM null uses log transformed exposure instead of exposure #4597

Closed

Description

@josef-pkt

#4576 (comment)

GLMResults.null uses the model.attribute exposure without backtransforming from log

If I change it to using exp(exposure), then the unit tests still pass. This means that this case is not unit tested.
There are unit tests for null_deviance but not for llnull.

Activity

  1. added
    type-bug-wrong
    on May 6, 2018
  2. added this to the 0.9 milestone on May 6, 2018
  3. josef-pkt commented on May 6, 2018

    @josef-pkt
    MemberAuthor

    PR coming up later today,

  4. josef-pkt commented on May 6, 2018

    @josef-pkt
    MemberAuthor

    something strange:

    If I fix GLMResults.null directly with exp(exposure), then tests pass.
    If I replace kwargs by model._get_init_kwds() (and pop family), then I get one test failure

    _____________________ TestGlmBinomial.test_null_deviance ______________________
    
    self = <statsmodels.genmod.tests.test_glm.TestGlmBinomial object at 0x0000000009
    C7CAC8>
    
        def test_null_deviance(self):
            assert_almost_equal(self.res1.null_deviance, self.res2.null_deviance,
    >                   self.decimal_null_deviance)
    E       AssertionError:
    E       Arrays are not almost equal to 4 decimals
    E        ACTUAL: 35455.297320077821
    E        DESIRED: 34345.3688931
    
    ..\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\genmod\tests\test_glm.
    py:151: AssertionError
    

    using pdb

    (Pdb) self.res1.model._get_init_kwds()
    {'freq_weights': array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
    1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.]), 'offset': None, 'var_weights': array([ 1.,  1.,  1.
    ,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  1.,  1.]), 'family': <statsmodels.genmod.families.family.Binom
    ial object at 0x0000000009E9CE10>, 'exposure': None}
    

    the only difference I can see is that we are not using WLS directly in GLM.null

            if len(kwargs) > 0:
                return GLM(endog, exog, family=self.family,
                           **kwargs).fit().fittedvalues
            else:
                wls_model = lm.WLS(endog, exog,
                                   weights=self._iweights * self._n_trials)
                return wls_model.fit().fittedvalues
    

    I don't understand why we should be using WLS if family is binomial.

    AFAIR:
    WLS with only a constant just computes the (prior-)weighted mean. This is the MLE if we have a canonical link, which is the case with Binomial. In this case using GLM or WLS should be equivalent for fittedvalues (params will be link transformed in one case and not in the other).

    WLS will be incorrect with a non-canonical link, where we don't have (prior-weighted) mean residual =0.

    test_glm.py has a comment, which I don't know what it means, or what's the reason for the comment

    #TODO:
    #Non-Canonical Links for the Binomial family require the algorithm to be
    #slightly changed
    

    ???
    I need some examples and unit tests to work through Null model cases.

  5. josef-pkt commented on May 6, 2018

    @josef-pkt
    MemberAuthor

    In the binomial case, model.endog is not the original endog, but the proportion.
    That is we cannot recreate the model using the attached model.endog.

    (Pdb) self.res1.model.endog
    array([ 0.56009913,  0.7826087 ,  0.59019264,  0.68935428,  0.12307692,
            0.59991099,  0.34970674,  0.61951754,  0.39047619,  0.43955014,
            0.25994695,  0.8893617 ,  0.35071942,  0.2994186 ,  0.53968254,
            0.75027027,  0.54111406,  0.44927536,  0.5503663 ,  0.55652174,
            0.36690647,  0.48997773,  0.48867314,  0.31034483,  0.18518519,
            0.07575758,  0.42970612,  0.45263158,  0.44099379,  0.57017544,
    ...
    
  6. josef-pkt commented on May 6, 2018

    @josef-pkt
    MemberAuthor

    partial correction:

    for computing the null prediction, deviance, llf, the link function doesn't matter if offset and exposure are zero. Any link is just a equivalent reparameterization of a constant.
    e.g. binomial-Logit and binomial-Probit null_deviance are the same.

    So using WLS assuming the canonical link is just the most convenient way to compute null fittedvalues, mu.
    deviance and loglike only depend on mu (and scale) and don't directly depend on link function.

    i.e. there is no bug in the current code by using WLS if offset and exposure are both zero.

  7. josef-pkt commented on May 6, 2018

    @josef-pkt
    MemberAuthor

    so there are only bugs in the GLM path in GLMResults.null

    • exposure not logged
    • wrong endog in case of Binomial with n_trials, which is used if either offset or exposure is a model attribute e.g. offset=np.zeros(nobs) produces the wrong results in this case, which is the case when I used model._get_init_kwds() for offset and exposure.

    another bug is (no full support for weights in 0.9 in some fringes)

    • null currently ignores prior weights, freq and var_weights in the GLM path, (but not in the WLS path)
    (Pdb) self.res1.model._get_init_kwds().keys()
    dict_keys(['freq_weights', 'offset', 'var_weights', 'family', 'exposure'])
    
  8. added a commit that references this issue on May 7, 2018
    6389e85
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