Methods package is now attached by default
On Tue, 21 Jan 2003, Roger Peng wrote:
I was looking at the examples in the `optim' help page and I think one of
them does not quite work. In particular, this example:
## "wild" function , global minimum at about -15.81515
fw <- function (x)
10*sin(0.3*x)*sin(1.3*x^2) + 0.00001*x^4 + 0.2*x+80
plot(fw, -50, 50, n=1000, main = "optim() minimising `wild function'")
While it executes, I think it produces the wrong plot. The call to plot
indicates that it should be plotted from x values of -50 to 50 but the
resulting plot goes from 0 to 50. However, if I call
plot.function(fw, -50, 50, n = 1000,
main = "optim() minimising `wild function'")
then that plot appears to be correct. I figured this had something to do
with the method dispatching on functions.
Not quite, because it doesn't use method dispatching -- it calls
plot.function explicitly.
I think it's a thinko in editing. plot() went from
plot<- function (x, ...)
{
if (is.null(attr(x, "class")) && is.function(x)) {
if ("ylab" %in% names(list(...)))
plot.function(x, ...)
else plot.function(x, ylab = paste(deparse(substitute(x)),
"(x)"), ...)
}
else UseMethod("plot")
}
to
plot<-function (x, y, ...)
{
if (is.null(attr(x, "class")) && is.function(x)) {
if ("ylab" %in% names(list(...)))
plot.function(x, ...)
else plot.function(x, ylab = paste(deparse(substitute(x)),
"(x)"), ...)
}
else UseMethod("plot")
}
but the extra argument `y' never got passed down to plot.function. It
should be something like
function (x, y, ...)
{
if (is.null(attr(x, "class")) && is.function(x)) {
if ("ylab" %in% names(list(...)))
plot.function(x,y, ...)
else plot.function(x, y, ylab = paste(deparse(substitute(x)),
"(x)"), ...)
}
else UseMethod("plot")
}
-thomas