Skip to content

MAINT: GLM - #1734

Closed
kshedden wants to merge 9 commits into
statsmodels:masterfrom
kshedden:glm-maint
Closed

kshedden wants to merge 9 commits into
statsmodels:masterfrom
kshedden:glm-maint

Conversation

@kshedden

@kshedden kshedden commented Jun 4, 2014

Copy link
Copy Markdown
Contributor

Addresses (or partially addresses) several issues with GLM:

  • Allow use of maxiter==0 to evaluate the likelihood at a given point (closes GLM.fit maxiter=0 incorrect #1728). Previously maxiter==0 was doing one iteration (should be zero iterations). Added a test for this use case.
  • Rename eta -> lin_pred and Y -> endog in many places. Partially closes Naming: GLM replace eta and mu or alias #1685, but I didn't do anything with mu (naming issues with links, e.g. fitted and predict still stand).
  • Rename confusing keyword argument fittedvalues in the negative binomial log likelihood to lin_pred (which is what it really is).
  • Avoid making unneeded copy of offset (second part of code review: GLM #1720)

@josef-pkt

Copy link
Copy Markdown
Member

Thanks Kerby, I will go over it soon, but I think I merge my fit_constrained branch first which should make merge conflicts easier (I only expect them in a few spots.) I'm only planning to do a few more smaller changes, but then I would like or need to go back to reviewing other PRs.

Two extension questions, not for this or my PR:

Do you know what we need for robust covariance matrix of the parameter estimates? In the other models I needed the score for each observation and the Hessian, after that it is largely generic code.

influence: While reading the Stata manual for this I saw that they define the (diagonal of the) hat matrix as the hatmatrix of the WLS estimator for the linear predictor, where the weights are the family specific variance function.
I thought the influence measures are more difficult to calculate. Given the diagonal of the hatmatrix they calculate standardized residuals and Cooks distance, for example.

@josef-pkt

Copy link
Copy Markdown
Member

TravisCI failure is because of an extra array that is not removed before pickling the instance #1729

in generalized_linear_model.py on line 199, 200, we need to add the new array _offset_exposure

        self._data_attr.extend(['weights', 'pinv_wexog', 'mu', 'data_weights',
                                            '_offset_exposure'])

@kshedden

kshedden commented Jun 4, 2014

Copy link
Copy Markdown
Contributor Author

I added _offset_exposure as you noted above but it still fails on test_shrink_pickle. The length of the thing that you are checking is ~10,300, and you want it to be <10,000.

@kshedden

kshedden commented Jun 4, 2014

Copy link
Copy Markdown
Contributor Author

I don't have a lot of experience with robust covariance estimators. I think you are talking about what's on page 13 here:

http://www.stata.com/manuals13/p_robust.pdf

If so, then it appears that yes, you just need the Hessian, and the score for each subject.

Maybe we can add a 'score_obs' method to the base likelihood model class, and then have a default 'score' method that just calls this for every row of the data set. We could then if needed override the default score method for efficiency.

Then we could have a generic 'robust_covariance' method that would work for all models.

@kshedden

kshedden commented Jun 4, 2014

Copy link
Copy Markdown
Contributor Author

Regarding diagnostics, I think you referred to this page earlier in a discussion about GEE. It shows how diagnostics are calculated in SAS for GLM (and GEE). Everything seems to be a function of the data and residuals (i.e. we don't seem to need the explicit score or Hessian).

http://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_genmod_sect045.htm

@josef-pkt

Copy link
Copy Markdown
Member

about the pickle/remov_data failure: We have to adjust the test and can increase the size limit to 20000 (I was mostly guessing on the size requirement, and made it pretty strict when I added this.)
while all py 2 versions have just above 10,000, py 3.3 uses more
nose.proxy.AssertionError: pickle length not 14077 < 10000

@josef-pkt

Copy link
Copy Markdown
Member

(I had forgotten that the SAS documentation was one of my favorite readings.)

about diagnostic and hatmatrix: based on briefly looking at the formulas, I don't see how SAS and Stata match up. So I'm not trying to quickly replicate Stata if it's not clear it is the only or right choice. new issue specific for GLM diagnostics #1737

about robust cov: I think I know now roughly how to add this if it's just generic MLE assumptions, and can look into this again later, new issue #1738

related: having score_obs/scores/estimating equations explicitly in GLM will also make it easier to add other things like score tests, or alternative optimizers.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) when pulling 9897b75 on kshedden:glm-maint into 8f7d124 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

general comment: If we do cleanups, then it would be useful to fix pep-8 issues in the changed lines, e.g. spaces after comma and around operators.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

general taste issue:
In pure math, I find using y and x locally improves readability, y = endog # alias
here missing whitespace makes it difficult to read

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can revert the y->endog changes in family. Just give me some guidance on
how to proceed with renamings.

On Mon, Jun 9, 2014 at 3:05 AM, Josef Perktold [email protected]
wrote:

In statsmodels/genmod/families/family.py:

         return -2 * np.sum(one * np.log(mu+1e-200) + (1-one) * np.log(1-mu+1e-200))

     else:
  •        return 2_np.sum(self.n_(Y_np.log(Y/mu+1e-200)+(1-Y)_np.log((1-Y)/(1-mu)+1e-200)))
    
  •        return 2_np.sum(self.n_(endog_np.log(endog/mu+1e-200)+(1-endog)_np.log((1-endog)/(1-mu)+1e-200)))
    

general taste issue:
In pure math, I find using y and x locally improves readability, y =
endog # alias
here missing whitespace makes it difficult to read

—
Reply to this email directly or view it on GitHub
https://github.com/statsmodels/statsmodels/pull/1734/files#r13525584.

@kshedden

kshedden commented Jun 9, 2014

Copy link
Copy Markdown
Contributor Author

I saw a bunch of pep-8 issues and was tempted to change them, but didn't
want to overdo it. I can go back through and do this if appropriate.

On Mon, Jun 9, 2014 at 2:59 AM, Josef Perktold [email protected]
wrote:

general comment: If we do cleanups, then it would be useful to fix pep-8
issues in the changed lines, e.g. spaces after comma and around operators.

—
Reply to this email directly or view it on GitHub
#1734 (comment)
.

@josef-pkt

Copy link
Copy Markdown
Member

about fixing pep-8:
Mixing pure cleanup changes (like whitespace) with substantial changes in one commit make reviewing and searching through the git history very difficult.
However, if we already have to change a line or lines, then we should clean them at the same time. It improves readability and it doesn't hurt finding the changes. And, we have one commit less for those lines because we don't need to clean them up later, which means shorter git history if we are looking for a specific change.

about the renamings:
I like eta -> linpred a lot and I expect that it will save me time when I go through this again.
I also like the endog rename overall. If you add whitespace, then the formulas should be easier to read also with full endog.

in general, based on my reviewing and bughunting experience:
It's important to have good informative names in the public parts and across function, methods or classes. However, I don't have a problem scrolling up a few lines to find the definition of an alias or temporary variable.

Readability for math or the actual calculations in a function can improve if we have short names. So, I'm often using the full names in short expressions, or when a name shows up only once.
But for longer expressions I don't have anything against one or 3 letter names, especially when subexpressions/temporary variables don't have a meaning, or there is a mnemonic xtxi for (X'X)^{-1}.

y or endog in families could go either way, but for example in discrete_models we don't alias endog, except for removing self (Poisson)

        XB = np.dot(self.exog, params) + offset + exposure
        endog = self.endog
        return -np.exp(XB) +  endog*XB - gammaln(endog+1)

but this is less nice

        X = self.exog
        L = np.exp(np.dot(X,params) + offset + exposure)
        return (self.endog - L)[:,None] * X

@josef-pkt

Copy link
Copy Markdown
Member

I'm going to rebase and merge this.

genmod needs a pep-8 cleanup, and we need to check formulas in the documentation to see whether using endog in them looks reasonable.

I will rebase and merge #1781 after this is in

@kshedden

Copy link
Copy Markdown
Contributor Author

I was going to continue this PR based on some of your earlier comments.
But I may not get to it for a few weeks. Maybe it's better if you feel
comfortable with it is to rebase and modify as you see appropriate, and
I'll work on a new PR with PEP-8 and other style changes later.

On Thu, Jun 26, 2014 at 3:43 AM, Josef Perktold [email protected]
wrote:

I'm going to rebase and merge this.

genmod needs a pep-8 cleanup, and we need to check formulas in the
documentation to see whether using endog in them looks reasonable.

I will rebase and merge #1781
#1781 after this is in

—
Reply to this email directly or view it on GitHub
#1734 (comment)
.

@josef-pkt

Copy link
Copy Markdown
Member

I'm going to merge this today, after rebase

@kshedden do you still have anything in this branch that you didn't push yet to github?

@kshedden

kshedden commented Jul 9, 2014

Copy link
Copy Markdown
Contributor Author

Thanks for checking. No, there are no local changes.

On Wed, Jul 9, 2014 at 10:19 AM, Josef Perktold [email protected]
wrote:

I'm going to merge this today, after rebase

@kshedden https://github.com/kshedden do you still have anything in
this branch that you didn't push yet to github?

—
Reply to this email directly or view it on GitHub
#1734 (comment)
.

@josef-pkt josef-pkt closed this in 9217656 Jul 9, 2014
@josef-pkt josef-pkt added this to the 0.6 milestone Aug 24, 2014
@josef-pkt josef-pkt mentioned this pull request Aug 24, 2014
11 of 25 tasks
PierreBdR pushed a commit to PierreBdR/statsmodels that referenced this pull request Sep 2, 2014
@kshedden
kshedden deleted the glm-maint branch January 25, 2018 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Naming: GLM replace eta and mu or alias GLM.fit maxiter=0 incorrect

3 participants