Learn R Programming

sitar (version 1.0.8)

ifun: Invert an expression defining a data transformation

Description

Enables a transformed variable to be back-transformed to its original scale, e.g. for plotting purposes, by creating a function to invert the transforming expression.

Usage

ifun(fun)

Arguments

fun
a language object defining the expression to be inverted, best quoted to avoid evaluation.

Value

A list of length two: A list of length two:

Details

ifun handles all the invertible functions in the 'Math' and 'Ops' groups.

To explain how ifun works, consider as an example variants of the sitar model height ~ age where age and/or height may be transformed, e.g. height ~ log(age) or log(height) ~ sqrt(age). Thus each model is of the form y ~ x but the units of x and y vary.

It is useful to be able to compare the models by plotting the fitted curves in the original units. This is done by applying suitable functions to invert the transformed variables prior to plotting. For example the function(x) exp(x) back-transforms log(age) and log(height). ifun automates this process by creating a function to back-transform the general expression fun(x) to x.

Structuring fun suitably ensures it can be inverted. It should contain a single mention of a single variable (named varname here), and possibly functions such as $f(.)$, $g(.)$, $h(.)$ etc, each of which involves (a function of) (a single mention of) varname and up to one numerical expression, such that fun = $f(g(h((varname))))$. The number of such functions is in principle unlimited.

ifun then returns $h^-1(g^-1(f^-1((eval(fun)))))$ as a function, with any numerical expressions evaluated, which means that fun is invertible so long as the individual functions are invertible. Note though that the function being invertible does not guarantee that variables can always be back-transformed, as for example the function may introduce NaNs.

Note that plot.sitar uses ifun as the default for arguments xfun and yfun, where the corresponding values of fun are extracted from the model's sitar call.

See Also

plot.sitar

Examples

Run this code

## define age
age <- 1:9

## simple case - transform age to log(age)
(fun <- quote(log(age)))

(transformed.age <- eval(fun))
(inverting.function <- ifun(fun)$fn)
(inverted.transformed.age <- inverting.function(transformed.age))

## is inverted transformed age identical to age?
all.equal(age, inverted.transformed.age)


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

(transformed.age <- eval(fun))
(inverting.function <- ifun(fun)$fn)
(inverted.transformed.age <- inverting.function(transformed.age))

## identical to original?
all.equal(age, inverted.transformed.age)


## ludicrously complex case involving exp, log10, ^, pi and trigonometry
(fun <- quote((exp(sin(pi * log10(age + 0.75)/2) - 1)^4)))

(transformed.age <- eval(fun))
(inverting.function <- ifun(fun)$fn)
(inverted.transformed.age <- inverting.function(transformed.age))

## identical to original?
all.equal(age, inverted.transformed.age)


## example of plot.sitar back-transforming transformed x and y in sitar models
m1 <- sitar(x=age, y=height, id=id, data=heights, df=6)
m2 <- update(m1, y=height^2)
m3 <- update(m1, x=log(age+0.75))

## default plot settings back-transform x and y to original scales
plot(m1, 'd')
lines(m2, 'd', col=2)
lines(m3, 'd', col=3)

Run the code above in your browser using DataLab