Given a transformed variable and the expression used to transform it,
a function containing the inverse expression that will back-transform the variable.
ifun(expr, verbose = FALSE)
a single-variable call or quoted expression to be inverted.
The variable's name in
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
To illustrate its use, consider variants of the
height ~ age where
height are transformed,
height ~ log(age) or
log(height) ~ sqrt(age). Each model
is of the form
y ~ x but the units of
The models are compared by plotting the fitted curves in their original units,
by first applying suitable functions to back-transform
For example with
expr = quote(log(age)),
ifun = function(x) exp(x) back-transforms
eval(expr) to give
age. See the first example.
ifun generalises this process for increasingly complex
the next two examples show.
The final example shows
ifun in action with
ifun as the default function for arguments
yfun - they are used to back-transform
y using the
y extracted from the model's
expr suitably ensures it can be inverted - it should
contain a single mention of a single variable (
and possibly functions such as
f(g(h((varname)))). The number of such functions
is in principle unlimited.
which ensures that
expr is invertible so long as the individual functions are invertible.
The required inverting function, with single argument
"varname" attribute contains
varname as a character string.
Tim Cole email@example.com
## 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 back-transforming 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 back-transform 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.