Skip to content

SUMM/FAQ: review of GLM families #3377

Closed

Description

@lorentzenchr

I have reviewed the file genmod.families.family.py and list the findings below. Some issues are already known. Sometimes a precise definition is missing.

General issues:

  • Is deviance and resid_dev scaled or unscaled (SAS parlance), i.e. with or without the factor 1/scale? (I would include 1/scale as it is mostly implemented.) This has consequences for GLM.estimate_scale(..).
  • Do distributions with scale=1 (Poisson, Binomial, Neg. Binomial) use scale in deviance and resid_dev nevertheless? (loglike should be implemented with scale==1, otherwise it would not be a valid log-likelihood!)
  • Precise definition of Anscombe residuals. Usually, it is r_A=(A(y)-A(mu))/(A'(mu) sqrt(v(mu))), where mu=E[Y], Var[Y]=scale v(mu) and A(x)=int_0^x v(m)^(-1/3) dm. To be comparable to resid_dev and in case of var_weights, one might consider r_A=(A(y)-A(mu))/(A'(mu) sqrt(Var[y])) with Var[y] = scale/w v(mu).

Issues (often the docstring needs according changes):

Activity

  1. josef-pkt commented on Jan 16, 2017

    @josef-pkt
    Member

    @lorentzenchr Thanks for the careful review, That's very very helpful.

    Most of the issues look good to me, for some I don't know the details.
    I was looking a bit at your comments for the scale. You found several bugs if the scale is not the default 1..

    two thoughts

    • resid_pearson or deviance are used for the scale calculation. Scaled deviance would essentially just be 1 with the estimated scale and not a fixed prior or external scale. So we should find some usecases where we want to deviate from the default. (var_weights will enter those functions in the same way as scale, except the weights are observation specific.) (compare_lr_test)
    • unit tests: I was thinking how we could unit test non-default scale, given that it is currently not used.
      • cross-checks for consistency: e.g. sum of squared resid_dev should be deviance, which would catch the missing sqrt. deviance with appropriate scale should be 1. ... ? deviance can be calculated from the loglike.
      • new fully supported usecases: Quasi-GLMs

    I think we understand GLM now well enough to add compare_lr_test #2641 (comment) #30 with loglike and deviance option including over/under dispersion.

    one specific issue:

    Binomial: self.n should be an ancillary parameter

    (The number of trials n is not used as a parameter, but it is part of the data like exposure, i.e. we never try to estimate n. In that sense it belongs to __init__ and treated as fixed for a model not as an argument to any of the methods.)
    After realizing this issue is about the family: I have no idea why it was implemented this way. I think there were some issues and, maybe incomplete, refactorings related to equivalent representation of Binomial.
    (Aside: Handling it through weights as in R's glm is confusing, and I saw many questions on stats.stackexchange for what weights in R's glm are really doing and what they can be used for. I.e. it's confusing for users that are not very familiar with GLM.)

  2. lorentzenchr commented on Jan 16, 2017

    @lorentzenchr
    ContributorAuthor
    • About scale in deviance: I think that scale should be included in deviance etc. This means, that GLM.estimate_scale must call self.family.deviance(..., scale=1). For the future of scale, see SUMM/FAQ-D: Refactor scale handling, especially GLM, RLM #3359.
    • About lr-test: As long as two models that only differ in their betas (i.e. in the linear predictor), they can easily be compared. In particular, the smaller model should inherit the scale estimate from the larger model for a lr-test.
    • Binomial(n,p): Just to make the point clear. The trials n are external parameters (from the data) that must be given in the same way as alpha must be given for Negative Binomial. Otherwise, the two distributions are not members of the exponential dispersion model/family - as far as I understand GLMs.
    • Documentation: In order to avoid questions and confusion, one should provide a good documentation (with precise definitions what has been implemented) and give good example notebooks. A good example for mandatory docu is var_weights once they are implemented. (This is a small promotions for PR DOC: Improve documentation about glm #2959 #3364 😄. Apologies also for my normative language.)

    By the way: Would it be better to give one PR for every point of the list or just one PR say for all scale related issues of the list?

  3. josef-pkt commented on Jan 16, 2017

    @josef-pkt
    Member

    Would it be better to give one PR for every point of the list or just one PR say for all scale related issues of the list

    The best would be to separate "obvious" things from ambiguous or potentially controversial parts. For example a bug-fix and documentation PR can be merged easily with standard review. Refactorings where we clearly know what we want, are also fine and easy to merge, subject to backwards compatibility and similar. (e.g. #3359 don't use scale as model attribute.)
    General refactorings might be more controversial and require more review/decisions especially without immediate use case. (I have a tendency to postpone those decisions until we know what we really need in the next expansion to new cases. That is, it's often difficult to evaluate changes based on general principles and fictitious use cases.)
    Example weights: we initially had freq_weights in the resid_xxx, then removed them again, and I'm still not sure where all the weights "wiring" is supposed to go.

    Another area where status-quo won so far: Theoretically, the score, score_obs and similar have scale in GLM and OLS. However, since they just drop out of the estimating equations, we keep them out. I.e. are we implementing full MLE or estimating equations? The outcome is the same but the former would have an extra scale factor in there. status-quo means that there is currently no consistent policy, and whether it's MLE-score or estimating equation is model/implementation specific.

    Apologies also for my normative language.

    Not a problem with me, although I consider any "must" just strongly in favor of one side of any trade-offs. And again, I appreciate the discussion and review (besides PRs), because sometimes those designs and decisions are difficult to figure out all by myself with little or no feedback.

  4. josef-pkt commented on Jan 16, 2017

    @josef-pkt
    Member

    (parking something I just remembered before running out the door)

    Related to var_weights, we also have the family variance function, and there are some issues or ideas to manipulate or combine them for the actual estimation. (I had forgotten the variance function that is already implemented in the var_weights discussion (var_function is just a function of mu, but we could combine it with a second term, similar to the n_i in Binomial or 1/sqrt(n_i) if endog is in fraction or proportion.

  5. josef-pkt commented on Jan 17, 2017

    @josef-pkt
    Member

    I'm reading through large parts of the technical documentation for GLM for SAS and SPSS again, not the latest version because I printed them a few years ago. Especially the SPSS manual has a nice formula collection, and quite a bit of computational details.

    To incorporating scale: We can clean this up but for backwards compatibility we cannot change the defaults without having a way to prevent users from getting the wrong or unintended numbers.
    In the context of weights, I thought about adding a new method for the resid_xxx that is not a cached attribute so we can use arguments for the options which kind of residuals, which weighting and which scaling, to return. Today I thought the full word residuals should be a good name for the new method.
    Otherwise, we would get a proliferation of resid__xxx_scaled and wresid_xxx_scaled.

  6. jbrockmendel commented on May 20, 2017

    @jbrockmendel
    Contributor

    Related to #3231 which adds more 1e-200 terms.

    Tweedie._clean uses a lower bound of 0 where the other _clean methods use FLOAT_EPS. Is there a reason for the difference? It also looks like Tweedie doesn't actually use its _clean, but calls np.clip directly in deviance and resid_dev. Changing the _clean method and using it instead of np.clip does not appear to break any tests.

  7. josef-pkt commented on May 20, 2017

    @josef-pkt
    Member

    In general I think we are moving to using clean instead of some floating adjustments like 1e-200.
    The problem is when we need different kinds of "cleaning" or different kinds at different places. (e.g. cases of 0 * log(0) should or could switch to a newish scipy.special function, if it's available in our minimum scipy version)

    tweedie might be a special case because the behavior is different for different power p, it has poisson and gamma as special cases. In your deviance link, the clipping is only needed in one interval of p, so we would have endog >= 0 for some p and endog > 0 for other p.

    One reason the 1e-200 have grown into the code is that mu should also be in the interior, but some datasets might result in perfect prediction or perfect separation where mu approaches or is equal to the boundary value, So, we might have for example log (0 / 0) which should be well defined.

    using clean would be the preferred solution, but still needs case by case checking.

    Please add comments if you look at specific cases, confirming that something looks correct for special or boundary cases is also useful for review, besides pointing out what doesn't look "good".

  8. lorentzenchr commented on Aug 20, 2017

    @lorentzenchr
    ContributorAuthor

    I consider this issue as solved. Still open are the Gaussian log-likelihood, which has its own issue, and Binomial, which is caused by internally rescaling y to [0,1] (a probability).

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