Skip to content

GenericLikelihoodModel and fittedvalues #37

Open

Description

@wesm

Original Launchpad bug 717510: https://bugs.launchpad.net/statsmodels/+bug/717510
Reported by: josef-pktd (joep).

GenericLikelihoodModel and its results do not define fittedvalues

Check what minimal methods a subclass has to implement, so we can have some fittedvalues. The meaning of fittedvalues is not obvious in some non-linear or non-normal models.

Is defining a predict method in the model subclass enough? Is there some generic support?

example Markov switching model with AR terms in each state: estimation with GenericLikelihoodModel seems to work fine although it's slow, but how do I get prediction, fitted values and residuals?

Activity

  1. vincentarelbundock commented on Jul 26, 2013

    @vincentarelbundock

    Given that this is for Generic LikelihoodModel, shouldn't we just accept that making prediction is model-specific and that this class won't provide a general solution to prediction? Close?

  2. vincentarelbundock commented on Jul 26, 2013

    @vincentarelbundock

    @josef-pkt you've done a refactor of this recently, no?

  3. josef-pkt commented on Jul 26, 2013

    @josef-pkt
    Member

    I think this is pretty much an open question. (In some cases I used geterrors as generic method rather than predict.)

    In my recent updates to GenericLikelihoodModel I worked mainly on getting the names and everything for t_test and summary to work, and didn't look at fittedvalues.

    Issues with a design label can be ignored most of the time, but they can be useful when we design new classes or intermediate classes, or when we think about refactoring existing classes. Many of them were random thoughts when I was working on something related and I didn't have a general answer.

    specific: predict method is model specific and a method of the model, but we have a generic predict on the results classes.
    fittedvalues is defined in the individual results classes, but it might still have a theoretically inconsistent definition across models (separate issue for discrete models). And I think it should be generic as fittedvalues = predict(...)

    separate issue are models that don't have a natural predict and fittedvalues (I thought), but as in GLM it might be just a conditional or unconditional expectation or endog, even if it is calculated in an indirect way.

  4. vincentarelbundock commented on Jul 27, 2013

    @vincentarelbundock

    Hmm, but that's the point. If predict is model specific, then it's weird to have that in a class that's meant to be inherited from.

    But yeah, I see the "design" tag and won't worry about those too much. I just subscribed to a bug triage service that sends me an email every day with a new statsmodels issue to look at. Fun.

  5. jseabold commented on Jul 27, 2013

    @jseabold
    Member

    Ha, that's awesome. Make it general and put it on github. Sounds like a hit.

  6. jseabold commented on Jul 27, 2013

    @jseabold
    Member

    Edit. I see subscribed (not wrote) now. Nevermind.

  7. vincentarelbundock commented on Jul 27, 2013

    @vincentarelbundock

    It's all those design "bugs" :P

  8. josef-pkt commented on Feb 27, 2014

    @josef-pkt
    Member

    I just ran into the missing fittedvalues/resid again with a new class to try out a model.

    For GMM I added some additional methods that produce a mean prediction default for the case when we have prediction or geterrors defined fittedvalues = predict = E(y| X) and resid = y - fittedvalues

    Either we need a subclass that handles this case more conveniently/automatically or we add them to the generic classes and they will be only meaningful depending on how predict (or GMM moment conditions are defined, in GMM I use subclasses)

    example for GenericLikelihoodModel in miscmodels.TModel (MLE with t-distributed errors)

    my current example is robust M-Estimators using the GenericLikelihoodModel as super class that has all the required features (and nloglike is a misnomer)

  9. josef-pkt commented on Mar 17, 2014

    @josef-pkt
    Member

    just a random thought

    attaching instance methods is messy and instance doesn't pickle
    how about monkey patching the class, something like

    if hasattr(self.__class__, 'predict'):
        self.__class__.fittedvalues = ... <unbound method> 
        self.__class__.resid = ...<unbound method> 
    

    maybe at to a initialize method that is called from Results.__init__

  10. josef-pkt commented on Sep 24, 2018

    @josef-pkt
    Member

    another case #4691 ProcessMLE which is MLE with within group parametric correlation
    inheriting from LikelihoodModelResults or GenericLikelihoodModelResults doesn't provide us fittedvalues and resid.

    impact, checking a few classes

    • statespace mlemodel defines fittedvalues based on forecast, and resid based on forecast error, both are precomuted
    • ARMAResult and, inheriting from it, ARIMAResults define and compute model specific fittedvalues and resid.
    • RLMResults, fittedvalues is directly computed and not through predict, resid uses a generic endog - fittedvalues
    • discrete: fittedvalues is still incorrectly defined
    • GLM
    • GMM: mixed usage
      • IVGMMResults has fittedvalues and resid with generic definition, this is results class for most IV based GMM models
      • IVRegressionResults for 2SLS inherits from RegressionResults with standard fittedvalues and resid
      • GMM/GMMResults the generic base version does not define predict and therefore fittedvalues and resid couldn't be defined.
        Qu: should we still implement it and raise NotImplementedError if GMM subclass doesn't define predict?
      • background GMM inherits from base.Model, GMMResults inherits from LikelihoodModelResults

    so it looks like a base Results class (either Results or LikelihoodModelResults) could add the generic definitions for fittedvalues and resid, which will fail with NotImplementedError if predict is not implemented in the Model class
    The only case where this would currently fail is generic GMM, where we would have extra methods/cached attributes in the results instances that raise.

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