Søren Højsgaard
2020-May-07 13:42 UTC
[R] Possible bug in optimize (related to naming the arguments)
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]]
Ege Rubak
2020-May-07 14:00 UTC
[R] Possible bug in optimize (related to naming the arguments)
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 at 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 at math.aau.dk
Peter Dalgaard
2020-May-07 14:14 UTC
[R] Possible bug in optimize (related to naming the arguments)
Partial matching is a feature regretted by its designer, but is not really possible to remove at this point. One early change in R relative to S was to require full match of anything after "...", but as you have noticed, that does not apply for interval= So, "don't do that, then" -pd> 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' >>> 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 at 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 at math.aau.dk > ______________________________________________ > R-help at 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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com