You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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?
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.)
(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.
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 )
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.
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)
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)
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.
Activity
josef-pkt commentedon Feb 23, 2018
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?
josef-pkt commentedon Feb 23, 2018
preliminary answer
weights for IRLS are computed here
https://github.com/statsmodels/statsmodels/blob/master/statsmodels/genmod/generalized_linear_model.py#L1124
muis available inresults.fittedvalues, i.e. the predicted mean for the response variable (which AFAIR includes offset and exposure, but needs checking for hown_trialsis handled in the Binomial case if it is not one, when it is internally rescaled to proportions.)josef-pkt commentedon Feb 23, 2018
(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.josef-pkt commentedon Feb 23, 2018
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
which is the same as the weights.
I.e. IRLS updating similar to Newton-Raphson uses the second derivative
(or something like that)
pinxterenm commentedon Feb 23, 2018
I wanted to use the weights to calculate the hat matrix
josef-pkt commentedon Feb 23, 2018
related question for Poisson
https://stackoverflow.com/questions/47686227/poisson-regression-in-statsmodels-and-r
josef-pkt commentedon Feb 23, 2018
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)
josef-pkt commentedon Feb 24, 2018
Here is a quick notebook using statsmodels master with the unadvertised
attach_wlsoption.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:
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)pinxterenm commentedon Feb 26, 2018
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.
josef-pkt commentedon Mar 4, 2018
Sorry, I lost track of this.
Are you using statsmodels 0.8?
The
attach_wls=Trueoption was only added recently (4 months ago) and is only available in master.pinxterenm commentedon Mar 5, 2018
I don't know, how can I make sure that I am using the master version?