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

Søren Højsgaard @orenh @end|ng |rom m@th@@@u@dk
Thu May 7 15:42:59 CEST 2020


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]]



More information about the R-help mailing list