Skip to content

Regression graphics for GLM and GEE - #2018

Closed
kshedden wants to merge 23 commits into
statsmodels:masterfrom
kshedden:glm-added-variable-plot
Closed

kshedden wants to merge 23 commits into
statsmodels:masterfrom
kshedden:glm-added-variable-plot

Conversation

@kshedden

Copy link
Copy Markdown
Contributor

This PR adds three diagnostic plots for GLM and GEE. Most of the code is shared between GLM and GEE.

I discovered a few missing things in GEEResults (family, various types of residuals) that are needed to make the plots work generically between GLM and GEE, so I added them.

The idea behind the CERES plot is a bit complicated. I believe that the implementation is correct, but the example in the test script doesn't highlight the technique well (it is an example where theoretically you don't need the CERES plot, the partial residual plot is adequate).

One possible extension is to allow the "focus" variable to be more than a single variable, e.g. a set of basis functions or indicators derived from a common underlying variable. I haven't figured out a clean way to do that yet, so am leaving it out for now (the approach has been defined in the literature, but implementing it involves figuring out which variables should be grouped)

The plots generated by the test script are here:

http://dept.stat.lsa.umich.edu/~kshedden/test_genmod_added_variable_plot.pdf
http://dept.stat.lsa.umich.edu/~kshedden/test_genmod_partial_residual_plot.pdf
http://dept.stat.lsa.umich.edu/~kshedden/test_genmod_ceres_plot.pdf

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) when pulling fc8ef95 on kshedden:glm-added-variable-plot into cfebe9a on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) when pulling abc8d17 on kshedden:glm-added-variable-plot into cfebe9a on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) when pulling ea1b0fa on kshedden:glm-added-variable-plot into cfebe9a on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) when pulling bf8f5a4 on kshedden:glm-added-variable-plot into cfebe9a on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) when pulling 250bf56 on kshedden:glm-added-variable-plot into cfebe9a on statsmodels:master.

@kshedden

kshedden commented Oct 4, 2014

Copy link
Copy Markdown
Contributor Author

Major refactoring: moved all plotting code to regressionplots.py, moved method wrappers from genmod up to base.

Added variable plots should work for any regression model with fitted values and residuals.

Partial regression and ceres plots will currently work for GLM, GEE, OLS, WLS, and GLS. It needs a link function derivative. The idea could be extended to any single index model with a smooth link.

@josef-pkt

Copy link
Copy Markdown
Member

about the single link (I'm still too distracted for the rest)

similar issue is when users transform the y, e.g. endog = log(y) in linear regression. We have an open issue about getting the prediction, properties for the original y. Without being able to guess the transformation, users would have to specify which "link" they used.
(The main part that I looked at for this is again expectation of the transformed variable versus transformation of the expected variable. E(f(endog)) versus f(E(endog))

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.07%) when pulling adec4e4 on kshedden:glm-added-variable-plot into cfebe9a on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.04%) when pulling adec4e4 on kshedden:glm-added-variable-plot into cfebe9a on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.04%) when pulling adec4e4 on kshedden:glm-added-variable-plot into cfebe9a on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) when pulling d6a0b35 on kshedden:glm-added-variable-plot into cfebe9a on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) when pulling c54a3f7 on kshedden:glm-added-variable-plot into cfebe9a on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) when pulling af06ff2 on kshedden:glm-added-variable-plot into cfebe9a on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) when pulling af06ff2 on kshedden:glm-added-variable-plot into cfebe9a on statsmodels:master.

@kshedden
kshedden force-pushed the glm-added-variable-plot branch from af06ff2 to 677294b Compare October 18, 2014 18:09
Comment thread statsmodels/base/model.py Outdated

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.

plot_ first convention for methods.

@jseabold

Copy link
Copy Markdown
Member

I'm slightly wary of adding these to the top-level Result. It's almost certain they're going to show up for models they're not appropriate for this way no?

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) when pulling a4603e5 on kshedden:glm-added-variable-plot into 7473965 on statsmodels:master.

@kshedden

Copy link
Copy Markdown
Contributor Author

I see what you mean.

Added variable plot should be immediately applicable to GLM, GEE, OLS, discrete, and Cox models. Basically all it needs is some way of calculating residuals of the dependent variable.

Partial residuals and CERES should work with GLM, GEE, and linear models. I'm not sure if there is a way to extend it to Cox models.

Of course these things would be available from the functions in regressiongraphics, but it would be nice to have class methods.

Would it make sense to have a RegressionModel subclass (of Model and Results), or mixin so that things that only make sense when there is a regression relationship can be easily attached to the model and results classes?

@josef-pkt

Copy link
Copy Markdown
Member

I still have mixed feelings about the location for this. A Mixin might be a good solution (and we should get started with those anyway).

Changing the class hierarchy requires more thought, and cannot just be added to a graphics PR. (I prefer to wait until we have a few more different model types before refactoring the class structure.)

The other issue is that this needs unit tests for all major models where this is included. For example, fittedvalues in discrete still returns the linear prediction instead of the conditional expectation of endog. (I usually prefer to start with a narrower set of models until we know how it will work across all main models.)

On the other hand this (or something like it) is applicable to a large number of classes.

@kshedden

Copy link
Copy Markdown
Contributor Author

How about I remove all the attachments to model classes and results, and we can just focus on the functions in regressiongraphics for now?

@josef-pkt

Copy link
Copy Markdown
Member

That would be much easier for merging this PR because we don't have to worry about every model that inherits it.

We still want to add it to all relevant models, but we could also do it incrementally or for a larger group of models in a later PR.

(Related similar issue: I think we can get score_tests pretty generally, but for my implementation I focus on OLS, discrete and GLM and don't think about whether it works for RLM and Quantile Regression for example.)

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.

does fig still need to be closed after save?
I guess it should still be open.

@josef-pkt

Copy link
Copy Markdown
Member

@kshedden given that you have the unit tests for the GLM method.
Instead of dropping it completely from the classes, you could add it to GLM as plot method and we continue from there to expand with adding as methods.

It will be a simple cut and paste later when we add it to a wider class or Mixin.

@kshedden

Copy link
Copy Markdown
Contributor Author

I moved everything out of base and into GLM and GEE. Tests run fine locally, but travis doesn't seem to be running after my latest commit.

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.

leftovers ?

@kshedden
kshedden force-pushed the glm-added-variable-plot branch from 1a0d2cb to c52fb1a Compare November 23, 2014 16:55
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) when pulling c52fb1a on kshedden:glm-added-variable-plot into 3a65a8f on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

@kshedden Do you know what the problem was with TravisCI? or why it went away?
The only potentially relevant change in the second to last commit that I can see is the deletion of importing GEEResults and GLMResults but that was protected inside a function.

@kshedden

Copy link
Copy Markdown
Contributor Author

I think my master had gotten messed up. I did a hard reset of my master
and rebased on it, then the problem went away.

On Sun, Nov 23, 2014 at 6:38 PM, Josef Perktold [email protected]
wrote:

@kshedden https://github.com/kshedden Do you know what the problem was
with TravisCI? or why it went away?
The only potentially relevant change in the second to last commit that I
can see is the deletion of importing GEEResults and GLMResults but that was
protected inside a function.

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

@josef-pkt

Copy link
Copy Markdown
Member

OK, I will check the git history more carefully when I do a interactive rebase to squash before merging.

Related: I opened a SUMMary issue, #2107, for refactorings/additions that we need in discrete models to make extensions like this easier. I ran into most of these things already before, and will need them also for some other planned enhancements (fit_reparameterized, added variable/omitted variable score tests and similar, link.derivative for prediction standard errors, ...).

@josef-pkt

Copy link
Copy Markdown
Member

just as update: I checked out the branch and looked at the pdf file created by the test run.
The plots look very informative, either good example or good plots, or both :)

I hope to finish reviewing tomorrow (which is already today local time).

Comment thread statsmodels/genmod/tests/test_glm.py Outdated

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.

my guess is that we need to close the figure here also, since it will have been created with the matplotlib global registry.
I was just looking at the matplotlib testing facilities, There is a cleanup decorator but I haven't figured out enough yet whether and how to use it.
closing should be enough in our case, I think.

fig = result.plot_ceres_residuals(1)
fig.close()

@josef-pkt

Copy link
Copy Markdown
Member

Should the plots get a default title? I think that would be more informative for default users.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) when pulling 927043e on kshedden:glm-added-variable-plot into 3a65a8f on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

merged in #2018 after rebase and squashing all commits into one.

I didn't review all the details (again). I would expect some follow up changes when it is more used.

@kshedden
kshedden deleted the glm-added-variable-plot branch January 25, 2018 19:12
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.

4 participants