Skip to content

TST: unit test for _init_keys recreating models #7569

Open

Description

@josef-pkt

#1732 FAQ-D issue

I almost forgot about adding _init_keys to a new model (Beta regression)

We could use python inspect to check whether keywords in the model__init__ signature are also in _init_keys for a generic unit test helper function.

AFAIK, we currently only have unit tests for internal usage, e.g. fit_zeros, fit_constrained, some llnull, and cases where we need to recreate a model like (IIRC) imputation.

(I'm not familiar enough with inspect module to do it right away.)

Activity

  1. josef-pkt commented on Jul 10, 2021

    @josef-pkt
    MemberAuthor

    I currently don't find where offset and exposure are added to init_keys
    e.g. a GLM example

    results.model._init_keys
    ['offset', 'exposure', 'freq_weights', 'var_weights', 'family']
    results.model
    <statsmodels.genmod.generalized_linear_model.GLM at 0x4dddb893d0>
    

    base.model.Model.__init__ adds all keys
    self._init_keys = list(kwargs.keys())

    but GenericLikelihoodModel.__init__ does not transmit kwds/kwargs to super

    super(GenericLikelihoodModel, self).__init__(endog, exog,
                                                         missing=missing,
                                                         hasconst=hasconst)
    
  2. josef-pkt commented on Jul 10, 2021

    @josef-pkt
    MemberAuthor

    check missing_idx, formula and design_info in _init_keys
    e.g. #7543 (comment)

    formula was added to Model.__init__ kwargs to support unpickling
    https://github.com/statsmodels/statsmodels/blame/main/statsmodels/base/model.py#L191

    However, formula and design_info would not be correct for recreating a model with a transformed exog or a null model.

    My guess is that in the current use cases (at least my use cases), formula info is not used.
    e.g.

    • llnull just computes llf for auxiliary model
    • fit_constrained uses the constrained model internally and only params and cov_params are used for the user facing model.

    results methods, post-estimation that use formula info:

    • predict for transformations, creating exog
    • pairwise t-test, wald_test_terms, AFAIR those use design_info directly if available
    • anova_lm for linear model uses terms based on design_info
    • t_test, wald_test only use param_names, so I guess are independent of formula
    • other ???
    • for bootstrap: we might need design_info, but should not use missing_idx

    cases I don't know
    imputation, mediation (Kerby's)

    If we want to support different use cases, then we might have to drop keywords before recreating a model
    e.g. drop missing_idx, formula, and design_info in fit_constrained (if we want to make full use of the constrained model)
    drop missing_idx for resampling observations from model.exog model.endog if they already have the missing obs dropped.

    GenericZeroInflated model already has _null_drop_keys
    self._null_drop_keys = ['exog_infl'] (if exog_infl is None, then a constant, ones, is used)
    which is used by DiscreteResults.llnull

    for key in getattr(model, '_null_drop_keys', []):
                del kwds[key]
    
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