I'm looking for an example of a simple R script that impliments a contrained nonlinear function using nlm or optim. I'm not exactly sure how to impliment the constraints within the objective function that is passed to nlm/optim. obj.func <- function( p ) { x(p) <- unconstrained obj function value if( constraint1 > something ) { obj.func <- x(p) } else { obj.func <- some super huge number } } p <- c(0.1,2.4, 5) nlm( obj.func, p, and a bunch of other arguments ) Any suggestions would be very helpful... Jeff. -- Jeff D. Hamann Forest Informatics, Inc. PO Box 1421 Corvallis, Oregon 97339-1421 phone 541-754-1428 fax 541-752-0288 jeff.hamann at forestinformatics.com http://www.forestinformatics.com
You want the penalty for the constraint to depend on how much the constraint is broken -- unlike your sample code. Page 336 of S Poetry has an example. Patrick Burns Burns Statistics patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Jeff D. Hamann wrote:>I'm looking for an example of a simple R script that impliments a >contrained nonlinear function using nlm or optim. I'm not exactly sure how >to impliment the constraints within the objective function that is passed >to nlm/optim. > >obj.func <- function( p ) { > > x(p) <- unconstrained obj function value > > if( constraint1 > something ) { > obj.func <- x(p) > } else { > obj.func <- some super huge number > } > >} > >p <- c(0.1,2.4, 5) > >nlm( obj.func, p, and a bunch of other arguments ) > >Any suggestions would be very helpful... > >Jeff. > > > > >
My guess is that approach will not work particularly well with nlm(), which expects the objective function to be smooth. You might have better luck using the Nelder-Mead method in optim(). -roger Jeff D. Hamann wrote:> I'm looking for an example of a simple R script that impliments a > contrained nonlinear function using nlm or optim. I'm not exactly sure how > to impliment the constraints within the objective function that is passed > to nlm/optim. > > obj.func <- function( p ) { > > x(p) <- unconstrained obj function value > > if( constraint1 > something ) { > obj.func <- x(p) > } else { > obj.func <- some super huge number > } > > } > > p <- c(0.1,2.4, 5) > > nlm( obj.func, p, and a bunch of other arguments ) > > Any suggestions would be very helpful... > > Jeff. > > >