Learn R Programming

sitar (version 1.0.4)

ifun: Invert an expression defining a data transformation

Description

Enables a transformed variable to be back-transformed, e.g. for plotting purposes, by inverting the transforming expression.

Usage

ifun(fun)

Arguments

fun
a language object defining the expression to be inverted.

Value

  • A list of length two:
  • fnthe inverse function with argument x which applied to eval(fun) returns get(varname).
  • varnamethe name of the variable in fun.

Details

The expression fun involves a single mention of a single variable named varname such that fun = f(g(h((get(varname))))), where each function f(.) involves up to one function of varname and up to one numerical expression (possibly including pi). The number of such functions is in principle unlimited.

ifun then returns h^-1(g^-1(f^-1((eval(fun))))), with any numerical expressions evaluated, which ensures that the expression is invertible if and only if the individual functions are invertible.

When modelling with transformed variables, with the transformations included in the model call, ifun enables the x and/or y variables to be automatically back-transformed to their original scales for plotting, simply by inverting the x and y transformations in the call. plot.sitar uses ifun for this purpose, via the arguments xfun and yfun.

See Also

plot.sitar

Examples

Run this code
## simplest case
(ifun(quote(age)))

(age <- 1:9)

## more complex case
## transform age to log age since conception
(fun <- quote(log(age + 0.75)))

## create inverse function
(ifun(fun))

##  transform age
(transformed.age <- eval(fun))

## ifun back-transforms transformed.age to age
(ifun(fun)$fn(transformed.age))

## ludicrously complex case including trigonometry etc
(fun <- quote((exp(sin(pi * log10(age + 0.75)/2) - 1)^4)))
(ifun(fun))
(transformed.age <- eval(fun))

## ta da!
(ifun(fun)$fn(transformed.age))

Run the code above in your browser using DataLab