Site icon R-bloggers

Wrapper functions in GNU R

[This article was first published on R snippets, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Recently I have been working with GNU R optimization routines a lot. Function optim has a nice trace option that allows to monitor optimization progress. Another standard function optimize has no such feature but it is possible to add outside tracking to it.

Let us start with an example from documentation:

f <- function (x, a) { (x a) ^ 2 }< o:p>
print(optimize(f, c(0, 1), tol = 0.0001, a = 1/3))

which produces:

$minimum< o:p>
[1] 0.3333333< o:p>

$objective< o:p>
[1] 0

We only get the final result of the optimization. The simple way to add tracing is by inserting print statement inside optimized function like this:

f <- function (x, a) {< o:p>
    print(x)< o:p>
    (x a) ^ 2< o:p>
}< o:p>
print(optimize(f, c(0, 1), tol = 0.0001, a = 1/3))

The call produces the following output:

[1] 0.381966< o:p>
[1] 0.618034< o:p>
[1] 0.236068< o:p>
[1] 0.3333333< o:p>
[1] 0.3333< o:p>
[1] 0.3333667< o:p>
[1] 0.3333333< o:p>
$minimum< o:p>
[1] 0.3333333< o:p>

$objective< o:p>
[1] 0

However, GNU R allows for a much fancier method that at the same time shows some nice features of the language itself. We use original function but add a wrapper around it:

wrap.f <- function(f, minimum = TRUE) {< o:p>
    calls <- NULL< o:p>
    return(list(f = function(x, …) {< o:p>
                    calls <<- c(calls, x)< o:p>
                    ifelse(minimum,f(x, …), f(x, …))< o:p>
                },< o:p>
                getter = function() { return(calls) },< o:p>
                reset = function() { calls <<- NULL }))< o:p>
}< o:p>
wf <- wrap.f(function (x, a) { (x a) ^ 2 })< o:p>
print(optimize(wf$f, c(0, 1), tol = 0.0001, a = 1/3))< o:p>
print(wf$getter())< o:p>
wf$reset()< o:p>
print(wf$getter())

The wrap.f function allows for tracking of calls of its first argument and to change optimization direction (this is possible through optimize function, but I add this as an example what can be achieved using this method). It holds internal variable calls which keeps track of calls to f function. It can be accessed using getter function and reset using reset function. The last three lines of code present the effect of resetting calls variable. Here is the output from the code:

$objective< o:p>
[1] 0< o:p>

[1] 0.3819660 0.6180340 0.2360680 0.3333333 0.3333000 0.3333667 0.3333333< o:p>
NULL

The wrapping method presented here can be used as a replacement for S4 classes in simple situations. We use the environment created by wrap.f function to store attributes and return methods that can access and modify them.

To leave a comment for the author, please follow the link and comment on their blog: R snippets.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.