Wrapper functions in GNU R

December 26, 2012

(This article was first published on R snippets, and kindly contributed to R-bloggers)

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 }

print(optimize(f, c(0, 1), tol = 0.0001, a = 1/3))

which produces:

[1] 0.3333333

[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) {
    (x a) ^ 2

print(optimize(f, c(0, 1), tol = 0.0001, a = 1/3))

The call produces the following output:

[1] 0.381966
[1] 0.618034
[1] 0.236068
[1] 0.3333333
[1] 0.3333
[1] 0.3333667
[1] 0.3333333
[1] 0.3333333

[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) {
    calls <- NULL
    return(list(f = function(x, …) {
                    calls <<- c(calls, x)
                    ifelse(minimum,f(x, …), f(x, …))
                getter = function() { return(calls) },
                reset = function() { calls <<- NULL }))
wf <- wrap.f(function (x, a) { (x a) ^ 2 })
print(optimize(wf$f, c(0, 1), tol = 0.0001, a = 1/3))


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:

[1] 0
[1] 0.3819660 0.6180340 0.2360680 0.3333333 0.3333000 0.3333667 0.3333333


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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)