Skip to content

Gee - #1314

Closed
kshedden wants to merge 35 commits into
statsmodels:masterfrom
kshedden:gee
Closed

Gee#1314
kshedden wants to merge 35 commits into
statsmodels:masterfrom
kshedden:gee

Conversation

@kshedden

Copy link
Copy Markdown
Contributor

This PR addresses several small but important issues that I discovered when using GEE on a real project.

Here is a summary of the changes:

  • Fixed a bug that was causing the residuals to become permanently centered after one call to center_residuals (even for subsequent calls to resid).
  • Force the time variable to always be float64. Strange things were happening when it was passed in as an integer type. Also the default was an integer type.
  • Fixed a bug that was causing the parameters component of fit_history to contain only the final parameter estimate.
  • Fixed a bug that was causing the number of iterations in the results summary to always display as 3.
  • Have fit_history contain more useful information.
  • Fixed a bug that was causing the user-requested covariance type to be ignored.
  • Rename _cluster_list to cluster_array : the new name is more descriptive and lacks the leading underscore since this function is helpful for users.
  • Rename row_indices to group_indices (more descriptive).
  • Added convenience functions to access residuals and centered residuals split by group.
  • Added a few tests
  • Minor docstring fixes

Note: it looks like I accidentally made some of my dotplot commits to my gee branch. You can ignore those for this PR (or tell me how to switch them please).

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling c9efa98 on kshedden:gee into fe6e688 on statsmodels:master.

@kshedden

Copy link
Copy Markdown
Contributor Author

Update: the new test_missing test in test_gee.py failed on travis, although it runs fine locally. I don't know why. For now I commented it out.

@josef-pkt

Copy link
Copy Markdown
Member

The commits from the dotplots are easy to remove, and shouldn't cause any merge conflicts because they are completely unrelated to this PR.

You can try it yourself, or I rebase before the merge.
The easiest way I know (there might be other simpler ones) is to use interactive rebase, and just delete the lines of the extra commits from the interactive commit list.
see for example https://help.github.com/articles/interactive-rebase (which does the more difficult things)

(It's always good to make a backup branch first.)

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.

I think self.time = np.asarray(self.time, np.float64) would do the same thing. But, do we have only numpy arrays at this stage?

The if part is essentially redundant, since astype(np.float64) doesn't create a copy if it's already the correct type.

(just checked some details: numpy 1.6.1 which I have open right now
x.astype(x.dtype) creates a new array with the same memory (a new view),
np.asarray(x, x.dtype) is a no-op and returns x
)

@josef-pkt

Copy link
Copy Markdown
Member

Kerby,
can you also add a few sentences to the release announcement?
statsmodels / docs / source / release / version0.6.rst

Do we have somewhere a few paragraphs to explain the idea behind GEE?

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling af67e11 on kshedden:gee into fe6e688 on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling af67e11 on kshedden:gee into fe6e688 on statsmodels:master.

@kshedden

Copy link
Copy Markdown
Contributor Author

I added some text for the release announcement. Feel free to edit or
comment.

On Thu, Jan 16, 2014 at 10:50 PM, Josef Perktold
[email protected]:

Kerby,
can you also add a few sentences to the release announcement?
statsmodels / docs / source / release / version0.6.rst

Do we have somewhere a few paragraphs to explain the idea behind GEE?

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/1314#issuecomment-32474713
.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 0457af1 on kshedden:gee into fe6e688 on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 3f62eff on kshedden:gee into fe6e688 on statsmodels:master.

@jseabold

Copy link
Copy Markdown
Member

+1 on making the conventions more visible (and to any pull requests that help bring these in line).

@josef-pkt

Copy link
Copy Markdown
Member

Thanks for the comments, I will open a PR with more naming_conventions which will be open for "debate".

Looks like scale as the "variance scaling factor" or "conditional variance" dominates, (I always though of it as the sqrt of it as in location-scale families, y = (x - loc) / scale as in the scipy.stats.distributions.)

open: cov_xxx (also for robust in #1335)
It's a bit trickier, because the default cov/cov_params could be any of them, and in some models, like GEE we have to store covariance_naive or cov_nonrobust independently from cov_default/cov/cov_params.
Currently each model that has robust cov (RLM, GEE, OLS, QuantReg) uses a different pattern.
I will collect these in a new issue, because I don't find anymore where I wrote the initial summary and proposal.

vcv I think that's a difficult to remember abbreviation, acronym. At least in Stata I always have to check how to specify robust covariances.

@bashtage

Copy link
Copy Markdown
Member

In the long-run, conventions about most things, not only naming, but also PEP, will help produce a more useful (and fixable) package. Conventions - naming, code, directory – need relatively long explanations, but perhaps a Conventions section should be added to the top.

@kshedden

Copy link
Copy Markdown
Contributor Author

I've continued to refine this branch beyond the initial PR. This is mainly to fix issues that came up when preparing examples (7 are now posted to the examples wiki).

The main changes are:

  • Changed the scale parameter so that it gives the variance rather than the inverse variance.
  • Major speedup of dependence_structures.Nested using vectorization.
  • Add dep_data as an argument to GEE. This is used by dependence structures like Nested that need additional information. Previously I was passing these as arguments to the covstruct constructor, but then they don't get processed properly for missing values.

@josef-pkt

Copy link
Copy Markdown
Member

Previously I was passing these as arguments to the covstruct constructor, but then they don't get processed properly for missing values.

I ran into a similar problem with extra data arrays for robust standard errors (#1220). I ignored it for now and I don't want to require them in model.__init__ in this case. I think we need to get access to the dropped or valid mask from the data description so we can delete rows in any extra arrays that we have separately from those in the model.__init__.

@kshedden

Copy link
Copy Markdown
Contributor Author

Having that mask would be helpful.

In this case, I think it is just as well to have these arrays as a keyword
argument to GEE.

On Wed, Jan 29, 2014 at 12:21 PM, Josef Perktold
[email protected]:

Previously I was passing these as arguments to the covstruct constructor,
but then they don't get processed properly for missing values.

I ran into a similar problem with extra data arrays for robust standard
errors. I ignored it for now and I don't want to require them in
model.init in this case. I think we need to get access to the dropped
or valid mask from the data description so we can delete rows in any extra
arrays that we have separately from those in the model.init.

Reply to this email directly or view it on GitHubhttps://github.com//pull/1314#issuecomment-33555377
.

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.

should we make it cov_struct? maybe a bit easier 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.

Yes, I think that's better.

On Sat, Feb 22, 2014 at 3:59 AM, Josef Perktold [email protected]:

In docs/source/release/version0.6.rst:

+.. code-block:: python
+
+import numpy as np
+import pandas as pd
+from statsmodels.genmod.generalized_estimating_equations import GEE
+from statsmodels.genmod.dependence_structures import Independence
+from statsmodels.genmod.families import Poisson
+
+data_url = "http://vincentarelbundock.github.io/Rdatasets/csv/MASS/epil.csv"
+data = pd.read_csv(data_url)
+
+fam = Poisson()
+ind = Independence()
+md1 = GEE.from_formula("y ~ age + trt + base", data, groups=data["subject"],\

  •                   covstruct=ind, family=fam)
    

should we make it cov_struct? maybe a bit easier to read

Reply to this email directly or view it on GitHubhttps://github.com//pull/1314/files#r9959553
.

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 made these changes in my branch.

@josef-pkt

Copy link
Copy Markdown
Member

Looks good, I like the release announcement part.

I didn't see any problems except for maybe minor adjustments, and standardizing naming conventions before 0.6 is released.

I can rebase and merge this.

@josef-pkt

Copy link
Copy Markdown
Member

for rebase
drop commits before Fixed bug that led to wrong covariance estimator being displayed in s… … 1114f50

drop :
Work on tests f0acd0b
Remove dotplot files from gee branch 9349421

@josef-pkt josef-pkt mentioned this pull request Feb 22, 2014
@josef-pkt josef-pkt closed this in 4932a9e Feb 22, 2014
@josef-pkt

Copy link
Copy Markdown
Member

I merged the rebased version in #1419
no changes to code

Thanks Kerby for improving and maintaining GEE

@josef-pkt

Copy link
Copy Markdown
Member

I can cherrypick and merge the additional changesets
kshedden/statsmodels@a2b802b...gee

Kerby, after I merge this round of changes you can update your master, the other gee changes are already in statsmodels.master.

I will try to merge a few more PR, including dotplot, in the next few days. Then you have less of a problem with multiple open branches and PRs

@kshedden

Copy link
Copy Markdown
Contributor Author

Thanks for your help getting this merged (and all your other work on the
project).

Here are some ideas for future work on GEE (probably for 0.7):

  • AIC -- there is one for GEE but it looks tricky and I don't understand it
  • Crossed dependence -- complements the existing nested covariance,
    essentially allows for random effects due to multiple discrete factors that
    are crossed rather than nested. This isn't too hard to implement but I
    haven't gotten around to it.
  • Additional serial dependence structures (beyond AR1 which we currently
    have)
  • Additional variances structures for discrete data
  • More (statistically) efficient variance structure updates using GLS
    instead of OLS (especially in nested and crossed covariances)
  • Maximum likelihood versions of dependence structure updates.
  • Convenience functions for graphing and sensitivity analyses
  • Additional GLM families (particularly Gamma and negative binomial); Gamma
    should already be there but has not been tested
  • Refactor and extend tests -- split out R tests from smoke tests, and add
    tests against stata
  • More examples

On Sat, Feb 22, 2014 at 9:38 AM, Josef Perktold [email protected]:

I merged the rebased version in #1419#1419
no changes to code

Thanks Kerby for improving and maintaining GEE

Reply to this email directly or view it on GitHubhttps://github.com//pull/1314#issuecomment-35790888
.

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.

6 participants