On 7 May 2020, at 16:00 , Ege Rubak <rubak at math.aau.dk> wrote:
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'
$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
$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]]