0
$\begingroup$

Why is the back-transformation of the predicted values so different from the observed when the observed are log-transformed?

Sample data:

trt=c("a","a","a","a","a","a","a","b","b","b","b","b","b","b","c","c","c","c","c","c","c")
resp=c(1,2,3,1,2,3,1,10,20,30,10,20,30,10,100,200,300,100,200,300,100)
resp=(log(resp))
observed=cbind(by(resp,trt,mean))
colnames(observed)="obs"
data1=data.frame(trt,resp)

Models: log.transformed and untransformed

model.nolog=lm(resp~trt,data=data1)
model.log=lm(log(resp)~trt,data=data1)

Predicted means

library(predictmeans)
estimated.nolog=predictmeans(model.nolog,"trt",adj="tukey")[[1]]
estimated.log=exp(predictmeans(model.log,"trt",adj="tukey")[[1]])
compare=cbind(observed,"est.nolog"=estimated.nolog,"est.log"=estimated.log)
options(digits=2)
> compare
   obs est.nolog est.log
a 0.51      0.51     1.7
b 2.81      2.81    16.7
c 5.12      5.12   166.9
$\endgroup$
  • $\begingroup$ I made a mistake. Need to remove "resp=(log(resp))" and run the code. The outcome at the bottom will then show the problem I am refering to. $\endgroup$ – Cesar Gemeno Dec 11 '14 at 16:40
  • $\begingroup$ You are modeling very different things in both cases. This seems like you generally aren't sure how simple linear regression works which might be a better question for Cross Validated. Right now, this does not appear to be a specific programming question, your confusion comes from not understanding the math of the regression. $\endgroup$ – MrFlick Dec 11 '14 at 16:50
  • 1
    $\begingroup$ I am very confused by the output you show because you have the statement resp=(log(resp)) before you make the data frame, but the results of your fitted models don't jibe with this, and seem to be reversed from what they should be. I suggest typing all the statements again and recomputing, because what you're showing cannot have been produced by the statements shown-- at least in the order shown. $\endgroup$ – Russ Lenth Dec 12 '14 at 1:59
  • 1
    $\begingroup$ If what you're trying to get at is the difference between these two columns: i=c(1,8,15); data.frame(orig=fitted(lm(resp~trt))[i], explog=exp(fitted(lm(log(resp)~trt))[i])) then you should make that explicit in your question. You need to fix your code and ask a clear question. $\endgroup$ – Glen_b Dec 12 '14 at 3:06
5
$\begingroup$

My answer is you got mixed-up somewhere in the chain of calculations.

> trt = c("a","a","a","a","a","a","a",
          "b","b","b","b","b","b","b",
          "c","c","c","c","c","c","c")
> resp = c(1,2,3,1,2,3,1,
           10,20,30,10,20,30,10,
           100,200,300,100,200,300,100)

> model.nolog = lm(resp ~ trt)
> predict(model.nolog, newdata = data.frame(trt=c("a","b","c")))
         1          2          3 
  1.857143  18.571429 185.714286 

> model.log = lm(log(resp) ~ trt)
> exp(predict(model.log, newdata = data.frame(trt=c("a","b","c"))))
        1         2         3 
  1.66851  16.68510 166.85104 

The results differ because the transformation is nonlinear. But not by as much as you indicated.

$\endgroup$
  • $\begingroup$ My appologies for sending a mistake in the code. rvl got it right though (thanks) so perhaps it is not necessary to resubmit the question. The predicted from model.nolog are identical to the observed. The predicted from model.log are off by 11%. This is a lot. I think $\endgroup$ – Cesar Gemeno Dec 12 '14 at 11:55
  • $\begingroup$ rvl: I think you are right about the nonlinear relation between log and exp: backtransforming a log does not have to be the same as the original. However this is a counterintuitive concept. If I do exp(log(x)) I get x, so I was expecting the same with the model. I suppose there will be a correcting factor somewhere, but for publication purposes I may as well use the log-transformed predicted and dnot backtransform it. $\endgroup$ – Cesar Gemeno Dec 12 '14 at 12:47
  • $\begingroup$ The discrepancy is due to the fact that there are several observations being averaged. And it is a well-known result, called Jensen's inequality, that the mean is greater than the back-transformed mean of the logs. The latter is commonly called the geometric mean. $\endgroup$ – Russ Lenth Dec 12 '14 at 14:09

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.