[R] Optim function with multivariate inputs

Uwe Ligges ligges at statistik.tu-dortmund.de
Sun Aug 28 17:43:03 CEST 2011



On 25.08.2011 19:02, Noah Silverman wrote:
> Hi,
>
>
> I have  function that I want to optimize.  Am playing with the optim() function in R
>
> Two issues:
>
> 1) I can't seem to get it to work with a function that takes multiple inputs.
>
> Dummy Example:
>
> myFunc<- function(A,B,D,D){

A really dummy example, since you must not have to equally named 
arguments in a function definition.


> 	# Do stuff
> 	return E
> }
>
>> myFunc(1,2,3,4)
> [1] 12
> # works fine from command line
>
>> optim( par=c(1,2,3,4), fn=myFunc)
> Error in A+B  : 'B' is missing


Make it one argument, e.g. by a wrapper:

wrapper <- function(x) myFunc(A=x[1], B=x[2], ........)


>
> 2) For SOME of the inputs, I want to fix the step size to integers.  For another input, I  would also like to limit the range of possible values for each input

If you want box constrains, see the L-BFGS-B method in optim().

You cannot fix the step size to integers (except doing naste tricks like 
rounding) in optim(). For these things, take a look into the 
Optimization Task View on CRAN which lists packages for such tasks.



> The help file in R isn't very clear on either of the above issues.

What is not clear? It mentions box constrains and it does not mention 
how to fix the step sizes to integers. Do you expect that help pages 
explain what features the functions do not have?

Uwe Ligges



> Any suggestions?
>
> Thanks!
>
>
> --
> Noah Silverman
> UCLA Department of Statistics
> 8117 Math Sciences Building #8208
> Los Angeles, CA 90095
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list