# Is there a way to maximize/minimize a custom function in R?

I'm trying to minimize a custom function. It should accept five parameters and the data set and do all sorts of calculations, producing a single number as an output. I want to find a combination of five input parameters which yields smallest output of my function.

-

I wrote a post listing a few tutorials using optim.

Here is a quote of the relevant section:

• "The combination of the R function optim and a custom created objective function, such as a minus log-likelihood function provides a powerful tool for parameter estimation of custom models.
• -
Lately I've been playing with DEoptim as a nice "no start points necessary" optimizer. –  Mike Lawrence Jul 1 '11 at 4:05
A recent posting of John Myles White on the optim command in R may also be of interest. –  Andy W Jul 1 '11 at 12:48
@Mike agreed; DEoptim gives more robust alternative (DE = Differential Evolution) –  Abe Jul 1 '11 at 17:46

In addition to Jeromy Anglim's answer, I have some more links.

Next to optim there is another function in base R that allows for what you want: nlminb. Check ?nlminb and ?optim for examples of the usage.

There are a bunch of packages that can do optimizations. What I found most interesting were the packages optimx and, quite new, the neldermead package for different versions of the simplex algorithm.

Furthermore, you might want to have a look at the CRAN Task View on Optimization for more packages

Please note that my recommendations all assume that you have a deterministic function (i.e., no random noise). For functions that are not strictly deterministic (or too big) you would need to use algorithms such as simulated annealing or genetic algorithms. But the CRAN Task View should have what you need.

-