Show yourself (look “under the hood” of a function in R)

Open source software has many virtues. Being free is not the least of which. However, open source comes with “ABSOLUTELY NO WARRANTY” and with no power comes no responsibility (I wonder..). Since no one is paying, by definition it is your sole responsibility to make sure the code does what it is supposed to be doing. Thus, looking “under the hood” of a function written by someone else is can be of service. There are more reasons to examine the actual underlying code.

A particularly advantageous feature of open source is that it is also by definition well.. open. So, we can learn from others and copy-paste good programming habits. There are a lot of experienced astute programmers out there. Looking “under the hood” of a function written by one of those guys\girls is a good start.

Another fine byproduct of having a close look at functions is that you can help debug them. It can sometimes happen that you realize some lines are redundant, or can be rewritten more efficiently. That is well, but there is no need to alarm the package maintainer for that. However, it likewise happens that something goes wrong with the function, mostly in some marginal cases and the returned value is flatly wrong. That IS a reason to fire a quick but orderly email to the maintainer of the package. Don’t be timid, make sure you are correct about your claim and he\she will thank you.

Yet another good reason to check the insides of a function is that it helps to understand the statistics\econometrics behind it. This is decidedly important for students who struggle with papers’ math notation. Inspecting how the method is de facto executed can help discard the “sort of” in the “I sort of understand” feel you sometimes get after reading an unfriendly paper or a textbook. Specifically, the exact dimension of the objects, how likelihood is constructed, where optimization happens or which algorithm is used. One can even dive to see how numerical algorithms arrive at their solution; i.e. is it gradient based algo or is it combinatorial optimization, though I freely confess I shy away from this latter addition.

With all this solid motivation to access at the actual body of functions, how do we do that?

In MATLAB, you can type in the console edit “name of function”, for example edit regress.

In R there is the usual parallel, but also some oddities to be aware of. Most functions written in R can be accessed in a similar manner to MATLAB. Take for example the Diebold-Mariano test, using the library forecast, all you need to do is to print the name of the function and the function itself is printed on your screen:

You can get a better look. Typing page(dm.test) or edit(dm.test) as in MATLAB will open the function in a separate window.

Sometimes we need to work a little harder. For example the function vcov returns variance-covariance matrix of model coefficients. But trying to print this function will not give us what we want:

The reason is that for some functions, it is important to know what is the kind of input provided. R is smart enough such that it recognize the kind of input you have provided without additional effort:

You don’t need to explain that lm0 is a linear model. But, since there are other models which require different treatment, when you wish to see what vcov looks like, you need to ask the right question, what does it look like for which kind of input? Here above, we use linear model, but it can also be a different model, say ARMA model or a generalized additive model (GAM). When a function is written differently for different inputs, we say it has different methods to deal with the different inputs. What we can do is to have a look which methods are available, and then we can choose to see the function for the method which is relevant for us:

You can see the function vcov takes as an input model object. Those objects are written differently, so vcov needs to adjust itself, which it does.

As a concrete example for this kind of functionality, what I sometimes do when I take issue with some specific section inside a function is to change the script and redefine it. Say the dm.test function is sitting inside a loop, and I know I use quadratic loss function only. The ‘power’ argument is there to accommodate different loss functions, but for me specifically it is redundant if I only use quadratic loss. The line d <- c(abs(e1))^power - c(abs(e2))^power slows down the function for no reason and is better written as d <- (e1^2 - e2^2) removing the power argument altogether and some clutter with it. Redefine the new function new function as mydm.test and use that one within your loops.

May you find this kind or practice useful.

One comment on “Show yourself (look “under the hood” of a function in R)”

  1. I, and several other StackOverflow R users, have written a very through answer to the question, “How do I view the source code for a function?”.

    In addition to what you’ve described, we also show how to find source code for S4 methods, functions that call compiled code (in a base, recommended, or other package), and compiled code built into the R interpreter.

Leave a Reply

Your email address will not be published. Required fields are marked *