[R] Possible bug in optimize (related to naming the arguments)

Ege Rubak rub@k @end|ng |rom m@th@@@u@dk
Thu May 7 16:00:00 CEST 2020


Dear Søren,

I suspect that the good R souls wouldn't consider this a bug, but a
logical consequence of the R language design. It is of course a valid
question whether this should be explicitly mentioned in documentation.

If I recall correctly about function evaluation: First all named
arguments are found by partial matching, so in your example you are
really providing the value 1/3 for the argument `interval` which is a
user mistake. The exact same things happens for the apply family, e.g.

> myfun <- function(x, F) x-F
> sapply(1:3, myfun, F = 1)
Error in match.fun(FUN) : '1' is not a function, character or symbol

It works fine if we provide a valid value for the argument `FUN` rather
than the constant value `FUN = 1`:

> sapply(1:3, FUN = myfun, F = 1)
[1] 0 1 2

(I realize no sane person would use the argument name `F` in this case,
but you get the point.)

Best,
Ege

On Thu, 2020-05-07 at 13:42 +0000, Søren Højsgaard wrote:
> Dear all,
> 
> I am wondering if there is a minor bug in the optimimize function;
> please see below:
> 
> 
> ---
> 
> 
> > ## example taken from optimize documentation
> > f <- function (x, a) (x - a)^2
> > xmin <- optimize(f, c(0, 1), tol = 0.0001, a = 1/3)
> > xmin
> 
> $minimum
> [1] 0.3333333
> 
> $objective
> [1] 0
> 
> > ## if we change argument a to j things still work fine
> > f2 <- function (x, j) (x - j)^2
> > xmin2 <- optimize(f2, c(0, 1), tol = 0.0001, j = 1/3)
> > xmin2
> 
> $minimum
> [1] 0.3333333
> 
> $objective
> [1] 0
> 
> > ## if we change argument a to i things fail
> > f3 <- function (x, i) (x - i)^2
> > xmin3 <- optimize(f3, c(0, 1), tol = 0.0001, i = 1/3)
> 
> Error in optimize(f3, c(0, 1), tol = 1e-04, i = 1/3) :
>   'xmin' not less than 'xmax'
> > xmin3
> 
> $minimum
> [1] 0.3333333
> 
> $objective
> [1] 0
> 
> > ##Same here
> > xmin3 <- optimize(f3, lower=0, upper=1, tol = 0.0001, i = 1/3)
> 
> Error in f(arg, ...) (from #1) : argument "i" is missing, with no
> default
> > xmin3
> 
> $minimum
> [1] 0.3333333
> 
> $objective
> [1] 0
> 
> > ## a workaround is
> > xmin3 <- optimize(f3, interval=c(0, 1), tol = 0.0001, i = 1/3)
> > xmin3
> 
> $minimum
> [1] 0.3333333
> 
> $objective
> [1] 0
> 
> ---
> 
> the problem is, I guess, due to the keyword 'interval' gets mixed
> up with 'i'.
> 
> Has anyone experienced that?
> 
> Best regards
> S�ren
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-- 
Ege Rubak, Associate Professor,
Department of Mathematical Sciences, Aalborg University
Skjernvej 4A, 9220 Aalborg East, Denmark
Phone: (+45)99408861
Mobile: (+45)30230252
Email: rubak using math.aau.dk


More information about the R-help mailing list