Marlene Mueller
2004-Jul-14 12:59 UTC
[R] constrOptim and function with additional parameters?
How can I use a function with some additional input parameters in constrOptim? For example, something like fr <- function(x,a) { ## Rosenbrock Banana function x1 <- x[1] x2 <- x[2] a * (x2 - x1 * x1)^2 + (1 - x1)^2 } where the optimum is to be found w.r.t. x. Calling optim(c(-1.2,1), fr, NULL, a=100) works as expected, but I fail to provide the a=100 in the constrained case:> constrOptim(c(-1.2,0.9), fr, NULL, ui=rbind(c(-1,0),c(0,-1)),ci=c(-1,-1),a=100) Error in f(theta) : Argument "a" is missing, with no default Is this a bug or is there a different solution that I miss here? TIA, Marlene
Dimitris Rizopoulos
2004-Jul-14 13:17 UTC
[R] constrOptim and function with additional parameters?
Hi Marlene, from the on-line help file of `constrOptim' you can see that the "..." argument is used for passing extra arguments to the `optim' function and not in the function being optimized under constraints. A simple solution would be to pass the value of the extra argument directly to the function definition e.g., `fr(x, a=100)'. Otherwise you could create a new version of `constrOptim' that passes the "..." to the function being optimized and `optim' I hope this helps. Best, Dimitris ---- Dimitris Rizopoulos Doctoral Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Marlene Mueller" <Marlene.Mueller at gmx.de> To: <R-help at stat.math.ethz.ch> Sent: Wednesday, July 14, 2004 2:59 PM Subject: [R] constrOptim and function with additional parameters?> > How can I use a function with some additional input parameters > in constrOptim? For example, something like > > fr <- function(x,a) { ## Rosenbrock Banana function > x1 <- x[1] > x2 <- x[2] > a * (x2 - x1 * x1)^2 + (1 - x1)^2 > } > > where the optimum is to be found w.r.t. x. Calling > optim(c(-1.2,1), fr, NULL, a=100) works as expected, but I fail > to provide the a=100 in the constrained case: > > > constrOptim(c(-1.2,0.9), fr, NULL, ui=rbind(c(-1,0),c(0,-1)), > ci=c(-1,-1),a=100) > Error in f(theta) : Argument "a" is missing, with no default > > Is this a bug or is there a different solution that I miss here? > > TIA, Marlene > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html
Duncan Murdoch
2004-Jul-14 13:20 UTC
[R] constrOptim and function with additional parameters?
On Wed, 14 Jul 2004 14:59:01 +0200 (MEST), "Marlene Mueller" <Marlene.Mueller at gmx.de> wrote :>How can I use a function with some additional input parameters >in constrOptim? For example, something like > >fr <- function(x,a) { ## Rosenbrock Banana function > x1 <- x[1] > x2 <- x[2] > a * (x2 - x1 * x1)^2 + (1 - x1)^2 >} > >where the optimum is to be found w.r.t. x. Calling >optim(c(-1.2,1), fr, NULL, a=100) works as expected, but I fail >to provide the a=100 in the constrained case: > >> constrOptim(c(-1.2,0.9), fr, NULL, ui=rbind(c(-1,0),c(0,-1)), >ci=c(-1,-1),a=100) >Error in f(theta) : Argument "a" is missing, with no default > >Is this a bug or is there a different solution that I miss here?I can't spot why your use of constrOptim isn't working, but you should be able to workaround it by doing something like this: applyDefaults <- function(fn, ...) { function(x) fn(x, ...) } constrOptim(c(-1.2,0.9), applyDefaults(fr, a=100), NULL, ui=rbind(c(-1,0),c(0,-1)),ci=c(-1,-1)) The applyDefaults function creates a new function which evaluates the old one with some of the parameters set to fixed values. Duncan Murdoch
Reasonably Related Threads
- constrOptim doesn´t send arguments to optim!(?)
- Re: [R] constrOptim and function with additional parameters? (PR#7088)
- Re: [R] constrOptim and function with additional parameters? (PR#7089)
- bug in '...' of constrOptim (PR#14071)
- How to capture console output in a numeric format