Skip to content

BUG: recreating Binomial model instance with n_trials != 1 #4599

Closed

Description

@josef-pkt

#4597 (comment)

GLM Binomial attaches a transformed endog to the model. This means that we cannot use our standard pattern for recreating a GLM Binomial model (unless it's a Bernoulli case).

There might be related bugs, where we use recreated models, e.g. most likely in fit_constrained and similar.
I don't know, have not checked yet, how MICE or other prefix methods handle this.
The case where I found this is in GLMResults.null which looks incorrect to me for Binomial, although there are unit tests for null_deviance.

Attaching a transformed endog is not a good design and it had me confused in several other places.

It would be better to use a different name, like wendog, in OLS/WLS. However, that would be a backwards incompatible refactoring for users that access the underlying model.endog.

Alternatively we can use a specific fix just for recreating a model, i.e. recover the original endog.

Activity

  1. added this to the 0.10 milestone on May 6, 2018
  2. josef-pkt commented on May 7, 2018

    @josef-pkt
    MemberAuthor

    I need a backwards compatible bugfix for 0.9.

    One option is to store _endog_init in case of Binomial and then check for it when recreating models.
    I don't really like that solution because we will have to add the additional check in every case where we recreate a model, i.e. more model specific special cases.
    I'm not sure which other models use a transformed endog, MNLogit does, VAR/VECM do not, AFAIR, those store the trimmed y and x separately from endog, exog.

    endog = getattr(self.model, '_endog_init', self.model.endog

    One disadvantage of using the original endog is that we need to transform it again when recreating the model.
    BUG: current GLM.fit_regularized also does not work correctly for Binomial count (AFAICS). The unit test is for the binary endog case.
    In this case it would be expensive to transform endog each time in the coordinate descend loop.

    Alternative solution specific to Binomial counts:
    I think we can keep the transformed endog if we attach n_trials as additional init keyword. However, GLM does not have n_trials as __init__ argument. So we would have to add it, or attach n_trials to the created model instance, or use the equivalence to var_weights.var_weights are too new and not fully supported yet.
    We could hide n_trials in GLM(..., **kwargs). I think this is the most promising solution, and would be picked up by all methods that rely on _init_keys. For example, AFAICS base.elastic_net.fit_elasticnet would then do the correct model recreation.

    The relevant code for initializing a Binomial count model is in GLM._setup_binomial which attaches the transformed endog and n_trials. Note n_trials is always attached even for non-Binomial families, in the non-count case it is a 1-D array of ones.

  3. josef-pkt commented on May 9, 2018

    @josef-pkt
    MemberAuthor

    I think the solution for Binomial in #4601 is fine (using n_trials as kwarg)

    We already had one case with endog in get_init_kwds #2259, which has been removed again in master

    +    def _get_init_kwds(self):
               ...
    +        kwds = super(SARIMAX, self)._get_init_kwds()
    +        kwds['endog'] = self.orig_endog
    +        kwds['exog'] = self.orig_exog
    

    current SARIMAX unit tests accesses directly the data attributes
    https://github.com/statsmodels/statsmodels/blob/master/statsmodels/tsa/statespace/tests/test_sarimax.py#L1055

            kwargs = self.model._get_init_kwds()
            endog = mod1.data.orig_endog
            exog = mod1.data.orig_exog
    

    another case to check will be MNLogit,
    which also stores data.orig_endog = endog
    In MNLogit similar to GLM-Binomial it might be more efficient to recreate the model with the transformed endog to avoid repeated recreation of the dummy representation (if we need it inside loops like the elastic_net loop)

    related for computational efficiency #4585 mutate model or recreate model for hyper parameter search

  4. josef-pkt commented on Sep 17, 2018

    @josef-pkt
    MemberAuthor

    is this still prio-high or are all immediate problems fixed?

    I fixed it for fit_constrained. but I don't remember if there is a glm binomial count unit test for fit_zeros, .... in #4576

  5. bashtage commented on Jul 30, 2026

    @bashtage
    Member

    Fixed by the same PR #4601: GLM.init now explicitly re-applies kwargs["n_trials"] after _setup_binomial(), so recreating a Binomial model instance (fit_constrained, elastic_net, etc.) preserves n_trials correctly. The underlying design smell (transformed endog attached to the model) is tracked separately and still open as #5282. Closing this one for the reported symptom.

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