Solved – How to display detrended data from a linear regression

data visualization

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

Unadjusted…

enter image description here

Detrended…

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?

Best Answer

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.

Related Question