Skip to content

ENH: add scale="mle" or profile-mle to GLM #7526

Open

Description

@josef-pkt

previous PR and discussion #2353

In #7525 it looks like scale estimated by profile-MLE is better in the gamma case than the default pearson_chi2 based scale estimate.

I think we can add "profile-mle" similar to chi2 and deviance scale estimator.

proposal:

We stick with one parameter LEF, and do not estimate full mle (no joint estimation of scale and mean parameters).
"profile-mle" stays as auxiliary estimation, mean parameters are estimated independently of scale.
This means we are imposing orthogonality in estimation and inference for mean parameters.
(this is different from discussion in #2353 based on quick skimming)

e.g. in my example in #7525, I used

from scipy import optimize
om = optimize.minimize(lambda x: -results.model.loglike(results.params, scale=x), 10)

This will work for families that have a proper scale/dispersion parameter like gamma and gaussian.
MLE scale for Poisson and Binomial is fixed at 1.

Tweedie is more difficult because we don't have full loglike.
Quasi-loglike might be an option.

Adding quasi-loglike for Poisson and similar doesn't make much sense, given that we have robust cov_type (HC, excess dispersion) for inference, and there is no proper distribution for quasi-poisson.
My focus in #7525 was to get a better scale estimate to have a better predictive distribution, and not better inference for mean parameters.

Maybe: Do we want something like REML scale estimate, e.g. degrees of freedom correction?
I don't know how this would work for distributions like gamma.

Aside:
Another option would be to add some minimum distance estimator for the scale, to match a gof statistic for the predictive distribution directly. #7412 That might be less sensitive to heavy tails and outliers.

Activity

  1. josef-pkt commented on Jun 30, 2021

    @josef-pkt
    MemberAuthor

    implementation:
    looks like we have the needed pieces

    estimate_scale(self, mu) uses mu and does not need params
    and we have loglike_mu function that can take mu as given loglike_mu(self, mu, scale=1.)

    om2 = optimize.minimize(lambda x: -results.model.loglike_mu(results.mu, scale=x), 10)
    

    has same scale estimate as minimizing loglike with fixed params in gamma example

    _fit_gradient has scale estimate after optimization

    mu = self.predict(rslt.params)
    scale = self.estimate_scale(mu)
    

    _fit_irls is messier because it uses estimate_scale several times during optimization and not explicitly at the end

  2. josef-pkt commented on Oct 2, 2021

    @josef-pkt
    MemberAuthor

    I'm running something similar in #7778 estimating Gamma model with mean and scale by full MLE.
    Scale estimate of full MLE GammaHet model agrees with the profile estimate computed like above.

    However, default ("X2") scale estimate in GLM gamma family is around 3 times the mle scale.

  3. josef-pkt commented on Oct 2, 2021

    @josef-pkt
    MemberAuthor

    R library MASS has a function to get the mle estimate based GLM gamma
    I can get the same results as they have in the doc example

    > library(MASS)
    > clotting <- data.frame(
    +     u = c(5,10,15,20,30,40,60,80,100),
    +     lot1 = c(118,58,42,35,27,25,21,19,18),
    +     lot2 = c(69,35,26,21,18,16,13,12,12))
    > clot1 <- glm(lot1 ~ log(u), data = clotting, family = Gamma)
    > gamma.shape(clot1)
                   
    Alpha: 538.1315
    SE:    253.5991
    > gamma.dispersion(clot1)
    [1] 0.001858282
    > clot1
    
    Call:  glm(formula = lot1 ~ log(u), family = Gamma, data = clotting)
    
    Coefficients:
    (Intercept)       log(u)  
       -0.01655      0.01534  
    
    Degrees of Freedom: 8 Total (i.e. Null);  7 Residual
    Null Deviance:      3.513 
    Residual Deviance: 0.01673      AIC: 37.99
    

    mine:

    results = res_r
    
    optimize.minimize(lambda x: -results.model.loglike(results.params, scale=x), res_r.scale/2)
          fun: 15.994961758909994
     hess_inv: array([[1.00347748e-06]])
          jac: array([0.00665283])
      message: 'Desired error not necessarily achieved due to precision loss.'
         nfev: 72
          nit: 6
         njev: 31
       status: 2
      success: False
            x: array([0.00185828])
    
    res_r.scale
    0.0024460362420932933
    

    problem is optimization needs a good starting value, or I get nans (negative dispersion)

    reparameterizing seems to work better

    method="trust-constr" doesn't need jacobian
    newton methods, other trustxxx require jacobian.

    o = optimize.minimize(lambda x: -results.model.loglike(results.params, scale=np.exp(x)), 
                          np.log(res_r.scale), method="trust-constr")
    o
             cg_niter: 14
         cg_stop_cond: 2
               constr: []
          constr_nfev: []
          constr_nhev: []
          constr_njev: []
       constr_penalty: 1.0
     constr_violation: 0
       execution_time: 0.020026206970214844
                  fun: 15.994961759131002
                 grad: array([-4.85323256e-06])
                  jac: []
      lagrangian_grad: array([-4.85323256e-06])
              message: '`xtol` termination condition is satisfied.'
               method: 'equality_constrained_sqp'
                 nfev: 18
                 nhev: 0
                  nit: 15
                niter: 15
                 njev: 9
           optimality: 4.853232560003585e-06
               status: 2
              success: True
            tr_radius: 1.288808847585388e-09
                    v: []
                    x: array([-6.28809307])
    np.exp(o.x)
    array([0.0018583])
    
  4. josef-pkt commented on Oct 2, 2021

    @josef-pkt
    MemberAuthor

    This needs more work and reading than what I want to do for #7778. I just wanted a unit test case against GLM-gamma.

    some links (that I didn't really read, partial skimming of a few parts)
    https://stats.stackexchange.com/questions/58497/using-r-for-glm-with-gamma-distribution

    https://www.groups.ma.tum.de/fileadmin/w00ccg/statistics/czado/lec8.pdf
    http://civil.colorado.edu/~balajir/CVEN6833/lectures/GammaGLM-01.pdf

    question related to get_distribution (separate issues).
    MLE scale would provide a better fit and a better gof test for predictive distribution
    (assuming gamma is correctly specified. Standard glm might be more robust to misspecification similar to WLS/GLSHet versus MLE)
    https://stackoverflow.com/questions/60215085/calculating-scale-dispersion-of-gamma-glm-using-statsmodels

    maybe also: mgcv, gamma with scale heteroscedasticity model for penalized splines GAM
    https://rdrr.io/cran/mgcv/man/gammals.html

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