You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG: recreating Binomial model instance with n_trials != 1 #4599
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.
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.
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.
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
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.
Activity
josef-pkt commentedon May 7, 2018
I need a backwards compatible bugfix for 0.9.
One option is to store
_endog_initin 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.endogOne 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_weightsare 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, AFAICSbase.elastic_net.fit_elasticnetwould then do the correct model recreation.The relevant code for initializing a Binomial count model is in
GLM._setup_binomialwhich 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.BUG/REF: fix GLM `null`, init keywords closes statsmodels#4597, see s…
TST: GLM Binomial count fit_regularized, is one case of statsmodels#4599
TST: GLM Binomial count fit_constrained, is one case of statsmodels#4599
josef-pkt commentedon May 9, 2018
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
current SARIMAX unit tests accesses directly the data attributes
https://github.com/statsmodels/statsmodels/blob/master/statsmodels/tsa/statespace/tests/test_sarimax.py#L1055
another case to check will be MNLogit,
which also stores
data.orig_endog = endogIn 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
josef-pkt commentedon Sep 17, 2018
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
bashtage commentedon Jul 30, 2026
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.