Visualization of regression coefficients (in R)

July 2, 2010
By

(This article was first published on R-statistics blog, and kindly contributed to R-bloggers)

Update (07.07.10): The function in this post has a more mature version in the “arm” package. See at the end of this post for more details. /> * * * *

Imagine you want to give a presentation or report of your latest findings running some sort of regression analysis. How would you do it?

This was exactly the question Wincent Rong-gui HUANG has recently asked href="http://r.789695.n4.nabble.com/Visualization-of-coefficients-tt2276010.html#none">on the R mailing list.

One person, Bernd Weiss, responded by linking to the chapter “ href="http://tables2graphs.com/doku.php?id=04_regression_coefficients">Plotting Regression Coefficients” on an interesting online book (I have never heard of before) called “ href="http://tables2graphs.com/doku.php">Using Graphs Instead of Tables” (I should add this link to the href="http://www.r-statistics.com/2009/10/free-statistics-e-books-for-download/">free statistics e-books list…)

Letter in the conversation, href="http://statmath.wu.ac.at/~zeileis/">Achim Zeileis, has surprised us (well, me) saying the following

I’ve thought about adding a plot() method for the coeftest() function in the href="http://cran.r-project.org/web/packages/lmtest/index.html">“lmtest” package. Essentially, it relies on a coef() and a vcov() method being available – and that a central limit theorem holds. For releasing it as a general function in the package the code is still too raw, but maybe it’s useful for someone on the list. Hence, I’ve included it below.

(I allowed myself to add some bolds in the text)

So for the convenience of all of us, I uploaded Achim’s code in a file for easy access. Here is an example of how to use it:

class="wp_syntax"> class="code">
source("http://www.r-statistics.com/wp-content/uploads/2010/07/coefplot.r.txt")
 
data("Mroz", package = "car")
fm <- glm(lfp ~ ., data = Mroz, family = binomial)
coefplot(fm, parm = -1)

Here is the resulting graph: /> href="http://www.r-bloggers.com/wp-content/uploads/2010/07/regression-coefficient-plot.png"> src="http://www.r-bloggers.com/wp-content/uploads/2010/07/regression-coefficient-plot.png" alt="" title="regression coefficient plot" width="550" class="alignright size-full wp-image-437" />

I hope Achim will get around to improve the function so he might think it worthy of joining his href="http://cran.r-project.org/web/packages/lmtest/index.html">“lmtest” package. I am glad he shared his code for the rest of us to have something to work with in the meantime src="http://www.r-bloggers.com/wp-content/uploads/2010/07/icon_smile.gif" alt=':)' class='wp-smiley' />

* * *

Update (07.07.10): /> Thanks to a comment by David Atkins, I found out there is a more mature version of this function (called coefplot) inside the {arm} package. This version offers many features, one of which is the ability to easily stack several confidence intervals one on top of the other.

It works for baysglm, glm, lm, polr objects and a default method is available which takes pre-computed coefficients and associated standard errors from any suitable model.

Example: /> (Notice that the Poisson model in comparison with the binomial models does not make much sense, but is enough to illustrate the use of the function)

class="wp_syntax"> class="code">
library("arm")
data("Mroz", package = "car")
M1<-      glm(lfp ~ ., data = Mroz, family = binomial)
M2<- bayesglm(lfp ~ ., data = Mroz, family = binomial)
M3<-      glm(lfp ~ ., data = Mroz, family = binomial(probit))
coefplot(M2, xlim=c(-2, 6),            intercept=TRUE)
coefplot(M1, add=TRUE, col.pts="red",  intercept=TRUE)
coefplot(M3, add=TRUE, col.pts="blue", intercept=TRUE, offset=0.2)

(hat tip goes to Allan Engelhardt for help improving the code, and for Achim Zeileis in extending and improving the narration for the example)

Resulting plot

href="http://www.r-bloggers.com/wp-content/uploads/2010/07/coeff-visualization-3.png"> src="http://www.r-bloggers.com/wp-content/uploads/2010/07/coeff-visualization-3.png" alt="" title="coeff visualization 3" width="550" class="alignright size-full wp-image-471" />

* * * /> Lastly, another method worth mentioning is the Nomogram, implemented by Frank Harrell’a href="http://biostat.mc.vanderbilt.edu/wiki/Main/Rrms">rms package.

To leave a comment for the author, please follow the link and comment on his blog: R-statistics blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , , , , , , ,

Comments are closed.

Top 3 Posts from the past 2 days

Top 9 articles of the week

  1. Scatterplots
  2. In-depth introduction to machine learning in 15 hours of expert videos
  3. The Single Most Important Skill for a Data Scientist
  4. Installing R packages
  5. Illustrated Guide to ROC and AUC
  6. Network analysis with igraph
  7. Using apply, sapply, lapply in R
  8. R vs Python: Survival Analysis with Plotly
  9. KDD Cup 2015: The story of how I built hundreds of predictive models….And got so close, yet so far away from 1st place!

Sponsors