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


enter image description here


enter image description here

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)

enter image description here

Does it make sense to do this? Is there a name for what I'm trying to do?

  • $\begingroup$ I think it's reasonable - I do it fairly frequently. Don't know of a name for it $\endgroup$
    – hadley
    May 14 '11 at 14:19
  • 1
    $\begingroup$ 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? $\endgroup$
    – cardinal
    May 14 '11 at 15:49
  • $\begingroup$ 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. $\endgroup$
    – user88
    May 14 '11 at 16:38
  • 1
    $\begingroup$ @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". $\endgroup$
    – TMOD
    May 14 '11 at 23:07
  • $\begingroup$ @mbq, yes, fair point. $\endgroup$
    – 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.

  • $\begingroup$ Note that lprice and lcarat are log transformed. $\endgroup$
    – cardinal
    May 14 '11 at 13:22
  • $\begingroup$ (The downvote is not mine.) $\endgroup$
    – cardinal
    May 14 '11 at 15:39
  • $\begingroup$ @cardinal Good point -- I mindlessly copied the variable names. However with log-log axes this makes even less sense ;-) $\endgroup$
    – user88
    May 14 '11 at 16:18
  • 2
    $\begingroup$ if you take the difference, then you get log(price/carat) $\endgroup$ May 14 '11 at 23:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.