Dear all, I'd like tweaking the ... arguments that one user can pass in my function for fitting a model. More precisely, my objective function is (really) problematic to optimize using the "optim" function. Consequently, I'd like to add in the "control" argument of the latter function a "ndeps = rep(something, #par)" and/or "parscale = something" if the user has not specified it already. Do you know a way to deal with this point? In advance, thanks. Mathieu -- Institute of Mathematics Ecole Polytechnique F?d?rale de Lausanne STAT-IMA-FSB-EPFL, Station 8 CH-1015 Lausanne Switzerland http://stat.epfl.ch/ Tel: + 41 (0)21 693 7907
On 05/07/2008 1:55 PM, Mathieu Ribatet wrote:> Dear all, > > I'd like tweaking the ... arguments that one user can pass in my > function for fitting a model. More precisely, my objective function is > (really) problematic to optimize using the "optim" function. > Consequently, I'd like to add in the "control" argument of the latter > function a "ndeps = rep(something, #par)" and/or "parscale = something" > if the user has not specified it already. > > Do you know a way to deal with this point? > In advance, thanks.It's generally not necessary to edit the ... args, but one way to do it is like this: dots <- list(...) # do some editing # instead of optim(a, b, ...), now you do do.call(optim, c(list(a, b), dots)) In the particular example you gave, I wouldn't do this; I'd give control as an arg to your function, and just edit that. For example, myoptim <- function(control = list(), ...) { if (is.null(control$ndeps)) control$ndeps <- rep(something, par) if (is.null(control$parscale)) control$parscale <- something optim(control=control, ...) } Duncan Murdoch
Mathieu Ribatet wrote:> Dear all, > > I'd like tweaking the ... arguments that one user can pass in my > function for fitting a model. More precisely, my objective function is > (really) problematic to optimize using the "optim" function. > Consequently, I'd like to add in the "control" argument of the latter > function a "ndeps = rep(something, #par)" and/or "parscale = something" > if the user has not specified it already. > > Do you know a way to deal with this point? > In advance, thanks. > > MathieuPackage lmom, recently added to CRAN, contains a function 'pelp' that does exactly what you describe. Relevant parts of its code: # Get the user-supplied arguments for the optimization functions dotargs <- list(...) # If user didn't supply a "control" argument, create one if (is.null(dotargs$control)) dotargs<-c(dotargs,list(control=list())) # If user didn't provide "ndeps" or "abstol" elements of the "control" # argument, set them to our own defaults if (is.null(dotargs$control$ndeps)) dotargs$control <- c(dotargs$control,list(ndeps=rep(acc,nshape))) if (method!="L-BFGS-B" && is.null(dotargs$control$abstol)) dotargs$control <- c(dotargs$control,abstol=acc^2) # Call the optimization function, optim() opt <- do.call(optim,c(list(par=start[shape.pos]),fn=critfn, method=method,dotargs)) J. R. M. Hosking
Bengoechea Bartolomé Enrique (SIES 73)
2008-Jul-07 10:39 UTC
[Rd] Editing the "..." argument
The 'modifyList' function on package Utils allows to do that in a very compact way: do.call("optim", modifyList(list(<your default values>), list(...))) Regards, Enrique Mathieu Ribatet wrote:> Dear all, > > I'd like tweaking the ... arguments that one user can pass in my > function for fitting a model. More precisely, my objective function is > (really) problematic to optimize using the "optim" function. > Consequently, I'd like to add in the "control" argument of the latter > function a "ndeps = rep(something, #par)" and/or "parscale = something" > if the user has not specified it already. > > Do you know a way to deal with this point? > In advance, thanks. > > Mathieu