[R] functions with function inputs and optimisation
Uwe Ligges
ligges at statistik.tu-dortmund.de
Sat May 11 16:57:09 CEST 2013
On 10.05.2013 08:19, Sachinthaka Abeywardana wrote:
> Hi all,
>
> Would be great if you could help me get my head around the "further
> arguemnts" in the optim function. Let's say we have f and g as shown:
>
> f<-function(x,a,b){(x-a)^2+b}
>
> optim(100,fn=f,gr=NULL,2,5) #the NULL is annoying
>
> g<-function(x,a){2*(x-a)} optim(100,fn=f,gr=g,2,5)
>
>
> 1. How does optim know that 2,5 both go into f whereas g only accepts the
> first of those arguments. WHAT IF for some random reason I wanted there to
> be no common parameters, what then? so for example the input for g to be 3
> s.t. f(x,2,5) and g(x,3)
Call with names arguements, i.e.:
optim(100, fn=f, gr=g, a=2, b=5)
>
> 2. Is there a way for me to skip writing gr=NULL the first time I implement
> this? Its quite important for me as I want to implement optimx where the
> "further arguments" are the last ones, and I do not want to go setting each
> parameter to some sort of default. basically I want to say:
>
> optim(100,fn=f,further_args=c(2,5))
OK, then redeinfe f and g to accept exactly 1 further args that has to
be split up to a/b f and g themselvers.
Uwe Ligges
> Thanks in advance,
> Sachin
>
> [[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