Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine which method is dispatched for a particular function call

Tags:

r

I'm trying to understand some code I didn't write (plot.gam in mgcv), and there's a call to a plot() function with some strange parameters that I don't recognize (e.g., "P"). I'd like to figure out which plot method is being dispatched on this call. findMethod() and similar functions don't help (I think plot is S3). I tried the debug library, but that doesn't let you "step into" a function call (and neither does the base debug functions).

Is there any way to monitor all function calls and their associated method dispatches in R? Or perhaps a function to which I can pass a string containing the actual function call (not just the signature) that will tell me what method gets dispatched?

like image 533
dan Avatar asked Aug 25 '11 18:08

dan


2 Answers

In plot.gam() we note that plot() is called on x$smooth[[i]], which is an object of class:

class(x$smooth[[i]])
[1] "tprs.smooth" "mgcv.smooth"

There is a plot() method for class "mgcv.smooth" and it is this that is being used for the plot in the general case. ?plot.gam mentions that this is the default method used for most smooths, but there are specific methods for certain types of smooth supported by gam() (from Details section of ?plot.gam:

For smooth terms ‘plot.gam’ actually calls plot method functions
depending on the class of the smooth. Currently random effect and
Markov random field smooths have special methods, the rest use the
defaults described below.

For some reason, methods() is not finding these methods, but they do exist:

> mgcv:::plot.mgcv.smooth
function (x, P = NULL, data = NULL, label = "", se1.mult = 1, 
    se2.mult = 2, partial.resids = FALSE, rug = TRUE, se = TRUE, 
    scale = -1, n = 100, n2 = 40, pers = FALSE, theta = 30, phi = 30, 
    jit = FALSE, xlab = NULL, ylab = NULL, main = NULL, ylim = NULL, 
    xlim = NULL, too.far = 0.1, shade = FALSE, shade.col = "gray80", 
    shift = 0, trans = I, by.resids = FALSE, scheme = NULL, ...) 
{
....

This may be related to a bug in methods() that meant plot.function was not shown in the list and my current R might not incorporate that fix. This method should be shown normally, and the general advice in such situations would be to identify the class of object (as I showed above) and then use methods() and similar functions (e.g. showMethods()) to identify if specific methods available for the class(es) of the object returned.

like image 139
Gavin Simpson Avatar answered Oct 08 '22 15:10

Gavin Simpson


For S3 classes, methods("plot") will give all the methods defined for a particular function. As an S3 dispatch, the one called will be based on the class of the first argument. Looking at plot.gam, I assume that the part you are asking about starts plot(x$smooth[[i]]), so you need to see what the class of x$smooth[[i]] is (where x is the object gam object) to determine what plot method will be called.

I don't know of a way to do this automatically.

like image 43
Brian Diggs Avatar answered Oct 08 '22 13:10

Brian Diggs