# How do I display detrended data from a linear regression?

I'm just having a look through Hadley's very excellent book about his ggplot2 R package.

He has some code to remove a linear trend in the diamonds dataset, like so:

d <- subset(diamonds, carat < 2.5 & rbinom(nrow(diamonds), 1, 0.2) == 1)
d$lcarat <- log10(d$carat)
d$lprice <- log10(d$price)

detrend <- lm(lprice ~ lcarat, data = d)
d\$lprice2 <- resid(detrend)

qplot(lcarat, lprice, data = d)
qplot(lcarat, lprice2, data = d)


Produces these graphs Detrended... I'd like to see what the actual values of lprice would be without the effect of lcarat. Plotting residuals vs lcarat shows the right shape, but the points are shifted toward y = 0 (look at the range of the y-axis units).

To get what I want, does it make sense to simply plot residuals + mean(lprice)? i.e. shift the previous graph up by mean(lprice).

qplot(lcarat, lprice2 + mean(lprice), data = d) Does it make sense to do this? Is there a name for what I'm trying to do?

• I think it's reasonable - I do it fairly frequently. Don't know of a name for it May 14 '11 at 14:19
• What exactly are you trying to accomplish? This strikes me as a little odd and, in particular, (highly) prone to misinterpretation. How would you appropriately label your axes of the last plot to make it easier to interpret? May 14 '11 at 15:49
• A side note -- if you plot a log-log graph, it is better to use log axes. Otherwise you suggest a linear relation between price and weight, which is in fact exponential.
– user88
May 14 '11 at 16:38
• @cardinal, what sort of misinterpretation do you mean? As for y-axis labels, the 3rd graph to me is "log(price) detrended" while the 2nd graph is simply "residuals".
– TMOD
May 14 '11 at 23:07
• @mbq, yes, fair point.
– TMOD
May 14 '11 at 23:08

As for me, it is terribly confusing, especially while you can do much simpler thing -- calculate price/carat to get a price of one carat, which would be way easier to interpret.
• Note that lprice and lcarat are log transformed. May 14 '11 at 13:22