# How to plot the contribution of each regression coefficient in a model, with R?

I've fit a model with lm(), and I now want to analyze it to see where it's overfit, etc... I'm imagining a plot that has the index of each observation on the x-axis, and the corresponding responses plotted as points on the y-axis. In (vertical) line with each point is a set of colored lines stacked on top of each other. Each vertical, colored line corresponds to a predictor in the model, and it's height is the coefficient on that predictor applied the data point it's in line with.

Is there already a function that does this? I don't want to reinvent the wheel (particularly because parsing the output of lm looks unpleasant).

-
Are you sure you are using "vertical" correctly in every instance? The description sounds like an ordinary (x,y) scatterplot on which are superimposed loads of (necessarily overlapping) vertical lines: a real visual mess. (Surely I misunderstand, so would you mind elaborating a little?) Second, lm produces a fixed set of coefficients, so what precisely do you mean by "the coefficient on that predictor applied [to] the data point"? –  whuber May 30 '12 at 21:04
Yes, quite sure. Stacking the bars means they never have to overlap. Peter's answer below pretty much got it. I meant the same thing I mean by "contribution": if the model is y(x) = a_1*x_1 + a_2*x_2 + C, I'm calling a_1*x_1 the contribution of the coefficient a_1 applied to the data point x. Hopefully that clears things up. The high-level idea is to visualize how the fitted value was arrived at by the model. Feel free to edit the question if there's some stats lingo I'm abusing or should be using. –  Quantitative Historian May 30 '12 at 23:05
See predict with type=terms for the "contribution" you're talking about, and perhaps look into component-plus-residual plots, which is a more common way to look at these contributions. See crPlots in the car package. –  Aaron May 31 '12 at 16:07
Thanks for the tips Aaron! –  Quantitative Historian Jun 4 '12 at 16:58

This sounds like just a stacked bar chart. I don't see how you handle the situation when the "contribution" made by a predictor and its coefficient is negative. But you might get something like the not very elegant but workable below. It returns warnings for when it is trying to plot something negative. Perhaps in your case this doesn't happen.

The code uses ggplot2 0.8.9 - I think melt() changes in the latest implementation but I don't have it installed at work:

library(ggplot2)
X1 <- rnorm(100)
X2 <- rnorm(100,5,3)
Y <- 4 + 5*X1 + 3*X2 + rnorm(100)
mod <- lm(Y ~ X1 + X2)
tmp <- data.frame(t(coef(mod) * t(cbind(1, X1, X2))))
names(tmp) <- c("Intercept", "X1", "X2")
qplot(x=as.factor(1:100), fill=variable, weight=value, geom="bar", data=melt(tmp)) +
geom_point(aes(x=1:100, y=predict(mod))


-
Thanks Peter, this is roughly what I'm looking for. To handle negatives, I'd like to plot the bar extending downward from 0 (but I'm not asking you to code this for me, unless you feel like it :). I'm also looking for a solution that handles interactions, which I think will significantly complicate the function, but I'll accept your answer unless someone hops in with a cookie-cutter solution in the next day or so. –  Quantitative Historian May 30 '12 at 22:56
Thinking about it, you could add some transparency (with the alpha= argument) so you can see through the negatives to the bars they are obscuring. I can't say I love this plot as a triumph of data visualisation, however. Perhaps it would look better with real data; and the order of the data would make a big difference to how it looks too. Maybe you could post your final solution. –  Peter Ellis May 30 '12 at 23:43
(+1), This is basically what I envisioned when I first read the question. Perhaps the negative contributions can be added above the predicted values (like they are pushing the point downward). But I agree with Peter's point as well, this seems like a limited approach, but it doesn't hurt to try I suppose! –  Andy W May 31 '12 at 12:29
I'll post my solution when I've got it. Some real data should make it more comprehensible, especially if I manage to sort the contribution bars so that the lowest variance ones are towards the bottom (that way a bias term will show up as a horizontal bar across the bottom). –  Quantitative Historian Jun 4 '12 at 16:57