"José Luis Aznarte M."
2007-Sep-12 08:32 UTC
[R] Passing parameters to 'optim' fn function
Hi again! I'm using the 'optim' method to fix the parameters of a model. I have written the function to be minimised and another function which returns the gradient of the error. Now my problem is that, in order to compute that gradient, it would be extremely convenient to be able to pass some parameters to the gradient function. I don't see how to do it given the fixed syntax of 'optim', which does not allow for parameters being passed to fn and gr: > optim(par, fn, gr = NULL, ...) Of course the first idea would be to "pack" the extra parameters in the vector 'par', but in that case the extra parameters would be optimized too. Does anyone have an idea on how to pass parameters to gr in optim? Thanks for your time!
Hi Use the dots argument at the end to pass in named parameters. This is documented as the last in the argument list under ?optim. Here's a toy example using x and p: > f <- function(x,p){sum(x^2)+p*sum(x)} > gr <- function(x,p){2*x+p*rep(1,length(x))} > optim(par=c(10,10),fn=f,gr=gr,p=13) $par [1] -6.498188 -6.499652 $value [1] -84.5 $counts function gradient 63 NA $convergence [1] 0 $message NULL HTH rksh On 12 Sep 2007, at 09:32, Jos? Luis Aznarte M. wrote:> Hi again! I'm using the 'optim' method to fix the parameters of a > model. I have written the function to be minimised and another > function > which returns the gradient of the error. Now my problem is that, in > order to compute that gradient, it would be extremely convenient to be > able to pass some parameters to the gradient function. I don't see how > to do it given the fixed syntax of 'optim', which does not allow for > parameters being passed to fn and gr: > >> optim(par, fn, gr = NULL, ...) > > Of course the first idea would be to "pack" the extra > parameters in > the vector 'par', but in that case the extra parameters would be > optimized too. > Does anyone have an idea on how to pass parameters to gr in optim? > Thanks for your time! > > ______________________________________________ > R-help at r-project.org mailing list > 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.-- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
you need to use the `...' argument; look at ?optim() for more info. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: ""Jos? Luis Aznarte M."" <aznarte at ugr.es> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, September 12, 2007 10:32 AM Subject: [R] Passing parameters to 'optim' fn function> Hi again! I'm using the 'optim' method to fix the parameters of a > model. I have written the function to be minimised and another > function > which returns the gradient of the error. Now my problem is that, in > order to compute that gradient, it would be extremely convenient to > be > able to pass some parameters to the gradient function. I don't see > how > to do it given the fixed syntax of 'optim', which does not allow for > parameters being passed to fn and gr: > > > optim(par, fn, gr = NULL, ...) > > Of course the first idea would be to "pack" the extra parameters > in > the vector 'par', but in that case the extra parameters would be > optimized too. > Does anyone have an idea on how to pass parameters to gr in > optim? > Thanks for your time! > > ______________________________________________ > R-help at r-project.org mailing list > 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Jos? Luis Aznarte M. wrote:> Hi again! I'm using the 'optim' method to fix the parameters of a > model. I have written the function to be minimised and another function > which returns the gradient of the error. Now my problem is that, in > order to compute that gradient, it would be extremely convenient to be > able to pass some parameters to the gradient function. I don't see how > to do it given the fixed syntax of 'optim', which does not allow for > parameters being passed to fn and gr: > > > optim(par, fn, gr = NULL, ...) > > Of course the first idea would be to "pack" the extra parameters in > the vector 'par', but in that case the extra parameters would be > optimized too. > Does anyone have an idea on how to pass parameters to gr in optim? > Thanks for your time! >You can put the parameters in the ... argument (watch out for matches to earlier optim args!), or into the environments of fn and gr. For example, makefn <- function(x) { fn <- function(mu) sum((x-mu)^2) gr <- function(mu) sum(2*(mu-x)) list(fn = fn, gr = gr) } x <- rnorm(10) fg <- makefn(x) optim(1, fg$fn, fg$gr) Duncan Murdoch
Possibly Parallel Threads
- R optim(method="L-BFGS-B"): unexpected behavior when working with parent environments
- R optim(method="L-BFGS-B"): unexpected behavior when working with parent environments
- R optim(method="L-BFGS-B"): unexpected behavior when working with parent environments
- R optim(method="L-BFGS-B"): unexpected behavior when working with parent environments
- optim