Skip to content

Covariate effect plot - #2023

Closed
kshedden wants to merge 15 commits into
statsmodels:masterfrom
kshedden:covariate_effect_plot2
Closed

kshedden wants to merge 15 commits into
statsmodels:masterfrom
kshedden:covariate_effect_plot2

Conversation

@kshedden

@kshedden kshedden commented Oct 2, 2014

Copy link
Copy Markdown
Contributor

This PR adds a plotting method to the base results class. It should be useful for any regression model that has endog and exog and that implements the predict method. The plot displays the fitted mean response against the value of one "focus" covariate, holding the other "control" covariates fixed at given values. There are two different ways to specify how the "control" covariates are fixed.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.02%) when pulling 6a16149 on kshedden:covariate_effect_plot2 into cfebe9a on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

general comment:

I think plot functions like this covariate_effect_plot should go into graphics.
This is what regression_plots is intended for.

@kshedden

kshedden commented Oct 2, 2014

Copy link
Copy Markdown
Contributor Author

@kshedden

kshedden commented Oct 2, 2014

Copy link
Copy Markdown
Contributor Author

Moving the backend plotting code to graphics makes sense, but would we
still have a method in the model results to create the plot? Otherwise the
graphical methods are not easily discoverable.

I already have some plotting code in MixedLM and GEE which are very
specific to those models. It seemed more natural to include it with the
model results in those cases.

On Thu, Oct 2, 2014 at 4:06 PM, Josef Perktold [email protected]
wrote:

general comment:

I think plot functions like this covariate_effect_plot should go into
graphics.
This is what regression_plots is intended for.

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

@josef-pkt

Copy link
Copy Markdown
Member

Moving the backend plotting code to graphics makes sense, but would we
still have a method in the model results to create the plot? Otherwise the
graphical methods are not easily discoverable.

We have an ancient open issue for adding the regression plots to the results instance, IIRC.
Just nobody ever did it, and I never tried to figure out which ones to attach and how to attach them.

There is also some model specific code for VAR models in tsa. I think it's fine for model specific plots to keep them next to the models. (But a lot of model specific code covers an increasing number of models over time, so "model specific" might not always be the same.)

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.02%) when pulling 1886384 on kshedden:covariate_effect_plot2 into cfebe9a on statsmodels:master.

@kshedden

kshedden commented Oct 3, 2014

Copy link
Copy Markdown
Contributor Author

I am tempted to remove the code that computes summaries and only accept an actual exog matrix or dataframe. The summary calculations would probably be better placed in an expanded predict method. For example, stata's predict lets you fix values for certain variables, with the others defaulting to the mean, max, etc. of the data used to do the fit.

@josef-pkt

Copy link
Copy Markdown
Member

Related: I thought about this or similar recently, #2015
The Margins in discrete have a at keyword, which is pretty much modeled after Stata's version.
As far as I have seen, our margins only handles derivatives but not the predicted values themselves.

Related to #2015, Stata changed from adjust to margins, and there was the related discussion about average treatment effect versus treatment effect at the average.
(The part I looked at in #2015 is to add confidence intervals for the mean prediction based on endpoint transformation of the linear prediction confidence interval.)

I'm not clear yet about the details, I went through ATE like calculations once for Poisson, i.e. for log-link.

All this here is only relevant for for models where predict is not a linear function of exog, is it?

@bashtage

bashtage commented Oct 3, 2014

Copy link
Copy Markdown
Member

Maybe the axes should be slightly higher than the histogram so it doesn't hit at the top.

@kshedden

kshedden commented Oct 3, 2014

Copy link
Copy Markdown
Contributor Author

Major refactoring after reconsidering the wisdom of performing the summarizing calculations here. Those calculations instead belong in a future extension of predict, or a new method with another name. So now I require the exog values for which mean curves are generated to be explicitly passed.

Some other changes:

  • More careful input processing so that this should work in a straightforward way for models
    constructed from matrices or from formulas.
  • Remove histogram code to a more generic helper function
  • Improve histogram plotting so that the histogram doesn't hit the upper border of the plot.

The updated test script results are here (the relevant plots for this PR start on page 9):

http://dept.stat.lsa.umich.edu/~kshedden/test_regressionplots.pdf

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) when pulling c8cd662 on kshedden:covariate_effect_plot2 into cfebe9a on statsmodels:master.

@kshedden

kshedden commented Oct 3, 2014

Copy link
Copy Markdown
Contributor Author

It would also be nice to auto-detect if focus_var is categorical and adjust the plot accordingly.

Also, once our predict methods consistently return confidence intervals we could have an option to add those to the plot.

@kshedden

kshedden commented Oct 4, 2014

Copy link
Copy Markdown
Contributor Author

Just a comment to clarify what this is useful for:

It is a plot for visualizing the fitted regression surface. It is not really a diagnostic plot, because it doesn't intend to identify any particular problem with the fit. It is especially useful in nonlinear models (like logistic or Poisson regression), where the effect of one covariate on the scale of the outcome variable depends on the levels of the other covariates.

It is seems to be somewhat similar to the existing function 'plot_regress_exog', but that one only works with OLS linear models.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) when pulling f50b969 on kshedden:covariate_effect_plot2 into cfebe9a on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.04%) when pulling 71eceb4 on kshedden:covariate_effect_plot2 into cfebe9a on statsmodels:master.

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.

Convention for methods that plot is to put plot_ first, so they're easily discoverable.

@jseabold jseabold added this to the 0.7 milestone Nov 21, 2014
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.3%) when pulling 7b06985 on kshedden:covariate_effect_plot2 into cfebe9a on statsmodels:master.

@kshedden

Copy link
Copy Markdown
Contributor Author

My thinking now is that we don't really need this. Instead, what we need is an enhanced "predict" method that allows a sequence of values for one predictor to be specified, and the other covariates are held fixed at default values (e.g. mean or a percentile). Then it would be easy enough to plot the results without needing a convenience function.

I will put that together as a separate PR unless people have other thoughts.

I would like to keep the "add_hist" from this PR.

@kshedden

kshedden commented Dec 7, 2014

Copy link
Copy Markdown
Contributor Author

I'm closing this. Will come back later with something analogous to Stata's 'margins'.

@kshedden kshedden closed this Dec 7, 2014
@kshedden
kshedden deleted the covariate_effect_plot2 branch February 5, 2015 19:47
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.

5 participants