Re: [R] constrOptim and function with additional parameters?

From: Marlene Mueller <Marlene.Mueller_at_gmx.de>
Date: Thu 15 Jul 2004 - 02:53:33 EST

OK, I got the point why the help refers to the arguments of 'optim' only. (Of course, I have read this but did'nt believe it ... ;-))

Thanks to all of you for the clarification and special thanks to Duncan Murdoch for the workaround, which is very close to what I want. I am doing a lot of constrained regression in the moment, so including additional parameters (=the data) is an issue for me.

Best regards, Marlene

> This is why the `...' argument is passed to `optim' and not to the
> function being optimized, as it also referred in the help file of
> `constrOptim'
>
> > constrOptim
> function (theta, f, grad, ui, ci, mu = 1e-04, control = list(),
> method = if (is.null(grad)) "Nelder-Mead" else "BFGS",
> outer.iterations = 100,
> outer.eps = 1e-05, ...)
>
> ...
>
> a <- optim(theta.old, fun, gradient, control = control,
> method = method, ...)
>
> ...
> }
>
>
> 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: "Roger D. Peng" <rpeng@jhsph.edu>
> To: "Duncan Murdoch" <dmurdoch@pair.com>
> Cc: <R-help@stat.math.ethz.ch>; "Marlene Mueller"
> <Marlene.Mueller@gmx.de>
> Sent: Wednesday, July 14, 2004 4:01 PM
> Subject: Re: [R] constrOptim and function with additional parameters?
>
>
> > Actually, I think this is a bug. Take a look at this part of

> constrOptim:
> >
> > > constrOptim
> > function (theta, f, grad, ui, ci, mu = 1e-04, control = list(),
> > method = if (is.null(grad)) "Nelder-Mead" else "BFGS",
> > outer.iterations = 10
> > 0,
> > outer.eps = 1e-05, ...)
> > {
> > if (!is.null(control$fnscale) && control$fnscale < 0)
> > mu <- -mu
> > [...]
> > obj <- f(theta)
> > ^^^^^^^^^^^^^^^
> > r <- R(theta, theta)
> > for (i in 1:outer.iterations) {
> > obj.old <- obj
> > r.old <- r
> > [...]
> > }
> >
> > So the object function `f' is called on the starting value `theta'
> but
> > the `...' is not passed through.
> >
> > -roger
> >
> > Duncan Murdoch wrote:
> > > On Wed, 14 Jul 2004 14:59:01 +0200 (MEST), "Marlene Mueller"
> > > <Marlene.Mueller@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
> > >
> > > ______________________________________________
> > > R-help@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
> > >
> >
> > ______________________________________________
> > R-help@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
>
> ______________________________________________
> R-help@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
>



R-help@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 Received on Thu Jul 15 03:10:47 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:54:58 EST