Determine which method is dispatched for a particular function call
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.
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.