ifun  R Documentation 
Given a transformed variable and the expression used to transform it, ifun
creates
a function containing the inverse expression that will backtransform the variable.
ifun(expr, verbose = FALSE)
expr 
a singlevariable call or quoted expression to be inverted.
The variable's name in 
verbose 
a logical controlling printing of the intermediate functions

ifun
returns the inverting function such that
ifun(expr)(eval(expr)) = varname
, where
expr
can include any of the invertible functions in the Math
and Ops
groups, plus identity
and I
.
To illustrate its use, consider variants of the sitar
model
height ~ age
where age
and/or height
are transformed,
e.g. height ~ log(age)
or log(height) ~ sqrt(age)
. Each model
is of the form y ~ x
but the units of x
and y
vary.
The models are compared by plotting the fitted curves in their original units,
by first applying suitable functions to backtransform x
and y
.
For example with log(age)
, where expr = quote(log(age))
,
the function ifun = function(x) exp(x)
backtransforms
eval(expr)
to give age
. See the first example.
ifun
generalises this process for increasingly complex expr
, as
the next two examples show.
The final example shows ifun
in action with plot.sitar
,
which uses ifun
as the default function for arguments xfun
and
yfun
 they are used to backtransform x
and y
using the
values of expr
for x
and y
extracted from the model's
sitar
call.
Structuring expr
suitably ensures it can be inverted  it should
contain a single mention of a single variable (varname
here),
and possibly functions such as f(.)
, g(.)
, h(.)
etc
such that expr
= f(g(h((varname))))
. The number of such functions
is in principle unlimited. ifun
returns function(x)
h^{1}(g^{1}(f^{1}((x))))
,
which ensures that
expr
is invertible so long as the individual functions are invertible.
The required inverting function, with single argument x
. Its
"varname"
attribute contains varname
as a character string.
Tim Cole tim.cole@ucl.ac.uk
plot.sitar
## for best effect run all the code
## define varname variable
(age < 1:9)
## simple case  age transformed to log(age)
(expr < quote(log(age)))
## transformed age
eval(expr)
## inverting function, with "varname" attribute set to "age"
ifun(expr)
## inverted transformed age identical to age
all.equal(age, ifun(expr)(eval(expr)))
## more complex case  age transformed to log age since conception
(expr < quote(log(age + 0.75)))
## inverting function
ifun(expr)
## inverted transformed age identical to age
all.equal(age, ifun(expr)(eval(expr)))
## ludicrously complex case involving exp, log10, ^, pi and trigonometry
(expr < quote((exp(sin(pi * log10(age + 0.75)/2)  1)^4)))
## inverting function, showing intermediate stages
ifun(expr, verbose=TRUE)
## identical to original
all.equal(age, ifun(expr)(eval(expr)))
## example of plot.sitar backtransforming transformed x and y in sitar models
## fit sitar models
m1 < sitar(x=age, y=height^2, id=id, data=heights, df=6)
m2 < update(m1, x=log(age+0.75), y=height)
## default plot options for xfun & yfun backtransform x & y to original scales
## xfun=ifun(x$call.sitar$x)
## yfun=ifun(x$call.sitar$y)
## compare mean curves for the two models where x & y are on the original scales
plot(m1, 'd', las=1)
lines(m2, 'd', col=2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.