Skip to content

GLM function not returning weights #4267

Open

Description

@pinxterenm

I have run into a problem using the GLM function with Binomial family. I used the code below to create an instance of the glm:

logit_instance = sm.GLM(default_array, predictors_matrix, family=sm.families.Binomial())
result = logit_instance.fit()

From this result I would like to get the weights of the final iteration. According to documentation, this should be accessible using result.weights, but this does not work. Also the dir(results) does not show 'weights' or anything similar as an attribute of result.

Is there some way to resolve this issue?

Activity

  1. josef-pkt commented on Feb 23, 2018

    @josef-pkt
    Member

    AFAIK, we don't have it.

    see #3743, second comment refers to #4021 with a hidden, unverified option to store the last WLS results that is only available with method='irls' but not yet if a scipy optimizer is used.

    However, weights in the IRLS fit should just be the inverse variance for each observation which is available.

    (I'm just recovering from a power outage and can look more later. Related, I was just reading up on outlier and influence diagnostics for GLM where we need the projection or hat matrix which also needs those weights.)

    just out of interest: What are you using the weights for?

  2. josef-pkt commented on Feb 23, 2018

    @josef-pkt
    Member

    preliminary answer
    weights for IRLS are computed here
    https://github.com/statsmodels/statsmodels/blob/master/statsmodels/genmod/generalized_linear_model.py#L1124

    mu is available in results.fittedvalues, i.e. the predicted mean for the response variable (which AFAIR includes offset and exposure, but needs checking for how n_trials is handled in the Binomial case if it is not one, when it is internally rescaled to proportions.)

  3. josef-pkt commented on Feb 23, 2018

    @josef-pkt
    Member

    (I'm getting confused or I'm thinking the wrong way, IRLS weights are the variance and not the inverse variance in Pregibon 1981 logistic regression diagnostics, i.e. n p (1 - p) which I think is for the count and not for the proportion. This needs a test case.)

    edit
    I'm not thinking the wrong way, I just forgot about that we also have family.link.deriv(mu) to account for the nonlinear mean function, and where the product with the variance simplifies when we have canonical links.

  4. josef-pkt commented on Feb 23, 2018

    @josef-pkt
    Member

    aside: score_factor also uses the same expression as IRLS weight but multiplied by response residual.
    (except again unclear/varying definition of y in Binomial n_trials > 1 )

    edit That's not correct
    weights uses 1. / (self.link.deriv(mu)**2 * self.variance(mu))
    score_factor uses 1. / (self.link.deriv(mu) * self.variance(mu))

    link.deriv is not squared in score_factor

    I'm still confused how this matches up, why does weights use the squared link.deriv?
    However, wls_endog in irls has self.family.link.deriv(mu) * (self.endog-mu) so it compensates again for one inverse link.deriv.
    (I never really tried hard enough to understand how they derived the IRLS updating step.)

    motivation: I'm looking at Pregibon 1981 Logistic Regression Diagnostics, and I wanted to see how the hat matrix and similar should be defined without having the IRLS computation available.

    edit 2: eim hessian factor is

    eim_factor = 1 / (self.family.link.deriv(mu)**2 *
                              self.family.variance(mu))
    eim_factor *= self.iweights * self.n_trials
    

    which is the same as the weights.
    I.e. IRLS updating similar to Newton-Raphson uses the second derivative
    (or something like that)

  5. pinxterenm commented on Feb 23, 2018

    @pinxterenm
    Author

    I wanted to use the weights to calculate the hat matrix

  6. josef-pkt commented on Feb 23, 2018

    @josef-pkt
    Member

    about hat matrix:
    there are two versions one symmetric with W^(1/2) in front and end and the one for the projection on the original which, IIRC, uses W at the end end no W in front.
    Pregibon 1981 uses the symmetric hat matrix (which is the same as using the transformed wexog in WLS)

    (computational aside: GLM currently doesn't use short cut computation sfor the canonical link cases, results are always computed from the general case)

  7. josef-pkt commented on Feb 24, 2018

    @josef-pkt
    Member

    Here is a quick notebook using statsmodels master with the unadvertised attach_wls option.

    https://gist.github.com/josef-pkt/7230fd4f1aab94e4f08b1cfa5b213f90
    I used plain binary Binomial, so I didn't have to worry about n_trials, which will need more checking.

    A few observations:

    • OLSInfluence does not seem to work with WLS, or it uses a different definition (impact on original, not on weighted observations). AFAIR, I didn't have reference numbers for WLS from other packages to decide on and verify the WLS case, and didn't have any clear references for that case (that's why the name is OLSInfluence).
    • Adding weights and hat_matrix_diag looks like 5 lines of code and 100 or more lines of unit tests for the extras, like n_trials, offset, exposure, user weights, checking scale, ...
    • without those extras, the different ways of computing irls weights and hat_matrix_diag agree with each other

    edit
    gist updated with adding an outlier and an influential point, and getting some influence statistics, like
    plt.plot(rslt2.resid_pearson**2 * h_diag / (1 - h_diag)**2)

  8. pinxterenm commented on Feb 26, 2018

    @pinxterenm
    Author

    Thank you for the help, I tried adding the attach_wls option in the following way:

    logit_instance = sm.GLM(default_array, predictors_matrix, family=sm.families.Binomial())
    result = logit_instance.fit(disp = False,attach_wls=True)

    However, the result.results_wls did not work (attribute did not exist). What am I doing wrong here? Because it seems to be working fine in the example you sent.

  9. josef-pkt commented on Mar 4, 2018

    @josef-pkt
    Member

    Sorry, I lost track of this.

    Are you using statsmodels 0.8?
    The attach_wls=True option was only added recently (4 months ago) and is only available in master.

  10. pinxterenm commented on Mar 5, 2018

    @pinxterenm
    Author

    I don't know, how can I make sure that I am using the master version?

  11. added
    type-possible-bug
    on Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-possible-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions