ifun: Invert an expression defining a data transformation

View source: R/ifun.R

ifunR Documentation

Invert an expression defining a data transformation

Description

Given a transformed variable and the expression used to transform it, ifun creates a function containing the inverse expression that will back-transform the variable.

Usage

ifun(expr, verbose = FALSE)

Arguments

expr

a single-variable call or quoted expression to be inverted. The variable's name in expr is referred to here as varname.

verbose

a logical controlling printing of the intermediate functions f(.), g(.), h(.) etc (see 'Details').

Details

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 back-transform x and y. For example with log(age), where expr = quote(log(age)), the function ifun = function(x) exp(x) back-transforms 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 back-transform 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.

Value

The required inverting function, with single argument x. Its "varname" attribute contains varname as a character string.

Author(s)

Tim Cole tim.cole@ucl.ac.uk

See Also

plot.sitar

Examples

## 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)

sitar documentation built on July 9, 2023, 6:51 p.m.