How to plot transformed regression back on original scale?

Viewed 3206

I would like to plot the line from a linear model where the response has been log transformed back on the original scale of the data. So the result should be a curved line on the original scale, where it would be a straight line on the log transformed scale. See code

# Data
 dat <- data.frame(c(1,2,3,4,5,6,7,8,9), c(5, 2.5, 1.25, .75, .375, .1625, 
 .08, .04, .02)
colnames(dat) <- c("X", "Y")                  
plot(dat$X, log(dat$Y))

# Model
mod <- lm(log(dat$Y)~dat$X)
summary(mod)

 # transformed
 plot(dat$X, log(dat$Y), pch = 16)
 abline(mod)

# back transformed
plot(dat$X, dat$Y)
# What do I do here to plot the curved regression line?

I know I have done this before but cannot for the life of me remember how :(. Thanks!

2 Answers
# back transformed
plot(dat$X, dat$Y)
predicted <- predict(mod, type="r")    
lines(dat$X, exp(predicted), col = "blue")

enter image description here

If ggplot is okay:

library(ggplot2)
ggplot(dat, aes(X,Y)) + geom_smooth() + geom_point() 
Related