Ricardo Zorzetto Nicoliello Vencio
2003-Oct-29 16:41 UTC
[R] constrOptim doesn´t send arguments to optim!(?)
Hi, I think that there something wrong with the 'constrOptim' max/minimization function because she doesn?t send extra arguments to 'optim' call. Fact: When I use optim in a f(x,theta)-like function, everything goes ok. But using constrOptim with the same function leads to error... Proof: Make a small change in the 'Rosenbrock Banana function' (taken from the Examples section of those help pages) adding extra (useless) parameters: fr <- function(x,extra1,extra2) { ## Rosenbrock Banana function x1 <- x[1] x2 <- x[2] extra1*extra2*100 * (x2 - x1 * x1)^2 + (1 - x1)^2 } grr <- function(x,extra1,extra2) { ## Gradient of 'fr' x1 <- x[1] x2 <- x[2] extra1*extra2*c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1), 200 * (x2 - x1 * x1)) } now, call it: optim(c(-1.2,1), fr, grr, extra1=c(1,2), extra2=c(3,4)) ok! but now: constrOptim(c(-1.2,0.9), fr, grr, ui=rbind(c(-1,0),c(0,-1)), ci=c(-1,-1), extra1=c(1,2), extra2=c(3,4) ) return the error: Error in f(theta) : Argument "extra1" is missing, with no default But in 'constrOptim' help page says: ...: Other arguments passed to `optim' And in 'optim' help page says: ...: Further arguments to be passed to `fn' and `gr'. My guess is that when 'constrOptim' calls 'optim' she doen?t send the extra arguments to correct evaluation of functions non-f(x)-like such as f(x,theta)-like. But whow I (we?) can fix it ? Any ideas ?
I'm not sure this is necessarily a bug in constrOptim. I think the problem is that while `...' is in fact passed to optim(), the objective function is evaluated in constrOptim a few times before optim() is called and `...' is not passed in those cases. The easiest way to make this problem go away is use lexical scoping. For example: make.fr <- function(extra1, extra2) { function(x) { x1 <- x[1] x2 <- x[2] extra1 * extra2 * 100 * (x2-x1 * x1)^2 + (1-x1)^2 } } And then in R> f <- make.fr(c(1,2), c(3,4))Then call constrOptim using this new f(). You don't have to worry about passing extra parameters now. -roger Ricardo Zorzetto Nicoliello Vencio wrote:>Hi, > >I think that there something wrong with the 'constrOptim' max/minimization >function because she doesn?t send extra arguments to 'optim' call. > >Fact: When I use optim in a f(x,theta)-like function, everything goes ok. >But using constrOptim with the same function leads to error... > >Proof: Make a small change in the 'Rosenbrock Banana function' (taken from >the Examples section of those help pages) adding extra (useless) >parameters: > >fr <- function(x,extra1,extra2) { ## Rosenbrock Banana function > x1 <- x[1] > x2 <- x[2] > extra1*extra2*100 * (x2 - x1 * x1)^2 + (1 - x1)^2 >} >grr <- function(x,extra1,extra2) { ## Gradient of 'fr' > x1 <- x[1] > x2 <- x[2] > extra1*extra2*c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1), > 200 * (x2 - x1 * x1)) >} > > >now, call it: > >optim(c(-1.2,1), fr, grr, extra1=c(1,2), extra2=c(3,4)) > >ok! > >but now: > >constrOptim(c(-1.2,0.9), fr, grr, ui=rbind(c(-1,0),c(0,-1)), ci=c(-1,-1), >extra1=c(1,2), extra2=c(3,4) ) > >return the error: > >Error in f(theta) : Argument "extra1" is missing, with no default > >But in 'constrOptim' help page says: > >...: Other arguments passed to `optim' > >And in 'optim' help page says: > >...: Further arguments to be passed to `fn' and `gr'. > > >My guess is that when 'constrOptim' calls 'optim' she doen?t send the >extra arguments to correct evaluation of functions non-f(x)-like such as >f(x,theta)-like. > >But whow I (we?) can fix it ? Any ideas ? > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > >