Skip to content

optim bug (PR#9684)

6 messages · christina.merz at gmx.de, Bill Dunlap, Andrew Clausen +1 more

#
Full_Name: Christina Merz
Version: R version 2.5.0 (2007-04-23)
OS: mingw32
Submission from: (NULL) (213.70.209.132)



R> version
               _                           
platform       i386-pc-mingw32             
arch           i386                        
os             mingw32                     
system         i386, mingw32               
status                                     
major          2                           
minor          5.0                         
year           2007                        
month          04                          
day            23                          
svn rev        41293                       
language       R                           
version.string R version 2.5.0 (2007-04-23)

-----------------------------------------------------------------------------

R> sessionInfo()
R version 2.5.0 (2007-04-23) 
i386-pc-mingw32 

locale:
LC_COLLATE=German_Germany.1252;LC_CTYPE=German_Germany.1252;LC_MONETARY=German_Germany.1252;LC_NUMERIC=C;LC_TIME=German_Germany.1252

attached base packages:
[1] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
[7] "base"     

-------------------------------------------------------------------------------

'optim' does not accept arguments called 'u'. Here is an example:

R> fun<-function(x,u) (x-u)^2
R> optim(7,fn=fun,u=9)

Fehler in fn(par, ...) : Argument "u" fehlt (ohne Standardwert)
Zus?tzlich: Warning message:
bounds can only be used with method L-BFGS-B in: optim(7, fn = fun, u = 9)

while

R> fun<-function(x,y) (x-y)^2
R> optim(7,fn=fun,y=9)
$par
[1] 8.999854

$value
[1] 2.145767e-08

$counts
function gradient 
      28       NA 

$convergence
[1] 0

$message
NULL

Warning message:
one-diml optimization by Nelder-Mead is unreliable: use optimize in: optim(7, fn
= fun, y = 9) 


-Christina
#
This is not a bug, but as documented on the help page:

      ...: Further arguments to be passed to 'fn' and 'gr'. Beware of
           partial matching to earlier arguments.

You have partial matching to 'upper'.
On Mon, 14 May 2007, christina.merz at gmx.de wrote:

            

  
    
#
On Tue, 15 May 2007, Prof Brian Ripley wrote:

            
We have this problem in optim(), integrate(),
and probably other functions.  These functions
have a lot of arguments before the ..., using
up a lot of partial matching space.  Would you consider
adding another argument to the tail end of
their argument lists that would include the
auxillary arguments?

E.g.,
   optim <- function(par, fn, gr = NULL,
           method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN"),
           lower = -Inf, upper = Inf,
           control = list(), hessian = FALSE,
           ..., aux.args = list(...))
If the user did not supply aux.args this would
act like the old version.  If the user did supply
aux.args then the function could check that no
unrecognized arguments were given to optim.

optim might require 2 such arguments,
    aux.args.fn = list(...),  aux.args.gr=aux.args.fn
(The forces you to know that the objective function
is called 'fn', not integrate's 'f' or apply's 'FUN'.)
I don't know what the best name for such an argument
would be.  If we added it, it would nice to make it
the same in R and Splus.
----------------------------------------------------------------------------
Bill Dunlap
Insightful Corporation
bill at insightful dot com
360-428-8146

 "All statements in this message represent the opinions of the author and do
 not necessarily reflect Insightful Corporation policy or position."
#
There quite a lot of these: optimize, uniroot, nlm for example.

I don't think the problem is large enough to merit another argument.
It is tempting to move '...' up the argument list so that partial matching 
will not occur.  I am not sure how far you can go: ?optim has positional 
matching for 'method', and ?optimize has abbreviations for lower and 
upper.

In R you rarely need to pass additional arguments in programming as 
lexical scoping can be used to capture them.

I'll do some experimenting.
On Tue, 15 May 2007, Bill Dunlap wrote:

            

  
    
#
On Tue, May 15, 2007 at 07:02:56PM +0100, Prof Brian Ripley wrote:
You can also use currying, like this:

	ll <- function(data) function(params)
	{
		# compute whatever you want with data and params.
	}

Then you can call optim like this:

	optim(initial.param, ll(some.data))

Cheers,
Andrew
#
On Tue, 15 May 2007, Prof Brian Ripley wrote:

            
Overnight runs show that if we do this maximally, only a very few packages 
are affected

optim: SoPhy has 'meth', copula has 'hess', MeasurementError.cor (BioC)
   has unnamed 'method'
uniroot: distrDoc has 'low', 'up' in a vignette
optimize: qtlDesign, sde, waveslim have 'max'
nlm: none
integrate: none

Doubtless there are scripts that will be affected (but with an error and a 
clearcut error message), but unless I hear cogent reasons otherwise
it seems that the balance is well in favour of making the change.

(I have more than once been tempted to knock up a version of R that does 
not allow partial matching of arguments to see what breaks, but a few are 
entrenched like 'length.out' and 'along.with' in seq.default and 'all' in 
ls.  Seems someone is fond of 'env' in get/exists/assign.)