Paul,
Inverting log(y) is just the beginning of the problem, after that you need to
teach predict.lm() that E(y |x) = exp(x'betahat + .5*sigmahat^2) and then
further
lessons are required to get it to understand how to adapt its confidence and
prediction bands? and then you need to generalize all of this to other
transformations. Quite a project!
Best,
Roger
Roger Koenker
rkoenker at illinois.edu
On May 17, 2013, at 12:21 PM, Paul Johnson wrote:
> This is an R formula handling question. It arose in class. We were working
> on the Animals data in the MASS package. In order to see a relationship,
> you need to log brain and body weight. It's a fun one for teaching
> regression, if you did not try it yet. There are outliers too!
>
> Students wanted to make a predicted value plot in the non-logged values of
> y, for comparison, and I wondered if I couldn't automate this somehow
for
> them.
>
> It made me wonder how R manages formulae and if a transformation like
> log(y) can be be mechanically inverted.
>
> So we have something concrete to talk about, suppose x and y are variables
> in dat, a person fits
>
> m1 <- lm(log(y) ~ log(x), data = dat)
>
> termplot shows log(y) on the vertical. What if I want y on the vertical?
> Similarly, predict gives values on the log(y) scale, there's no
argument
> like type = "untransformed".
>
> I want my solution to be a bit general, so that it would give back
> predicted y for formulae like
>
> sqrt(y)
>
> or
>
> exp(y)
>
> or
>
> log(y + d)
>
> or whatever other math people might throw in there.
>
> Here's what I can tell so far about R's insides. The formula
handler makes
> a list out of the formula, I can get that from the terms object that the
> model generates. The formula list has "~" as element 1, and
"log(x)"
> becomes element [[2]].
>
> Where in the R source code can I see how R "looks at" the symbol
log(y) and
> discerns that there is a variable y that needs to be logged? If I could
> understand that, and if R has a table of inverse functions, then maybe I
> could see what to do.
>
> If you have ideas, I'm very grateful if you share them.
>
> pj
> --
> Paul E. Johnson
> Professor, Political Science Assoc. Director
> 1541 Lilac Lane, Room 504 Center for Research Methods
> University of Kansas University of Kansas
> http://pj.freefaculty.org http://quant.ku.edu
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.