[R] Questions in R about optim( ) and optimize( )

Prof J C Nash (U30A) nashjc at uottawa.ca
Thu May 22 18:45:56 CEST 2014


You need to provide reproducible examples if you want to get readers to
actually give you answers.

The two issues both come up from time to time and generally relate to
how the objective function is set up, though sometimes to options in the
call. However, "generally" really isn't good enough.

JN



> Date: Wed, 21 May 2014 20:37:10 +0000
> From: "Gao, Vicky" <VGao at panagora.com>
> To: "'r-help at r-project.org'" <r-help at r-project.org>
> Subject: [R] Questions in R about optim( ) and optimize( )
> Message-ID:
> 	<E6231A6C907F1E4EBDC773DF7F92327CDFE6D6 at exmailbox1.panagora.com>
> Content-Type: text/plain
> 
> Hello,
> 
> I would be really thankful if I could bother you with two questions.
> 
> 1.  I ran into some problems when I used the code "optim()" in my work. I used it to optimize a function-fn(x,y), where x is a two-dimensional parameter.  For y I want to pass the value later when I use optim(), because it needs to be inside of a loop, which changes the value of y every time.
> 
> "For (point in 100:500){
>       ....
>       optim(c(0,0.5),fn,p=point)
>       ....
>    }"
> 
> But it always turned to be "Error in fn(par, ...) : argument "p" is missing, with no default".  I'm confused about this, is that caused by optim() has some specific requirements towards the objective function?
> 
> 2.  I'm also confused about "optimize()" actually.  My code is like: "optimize(f,c(0,0.99),b=1.0,p=point)". I have nothing wrong when I run it, but the result is not accurate enough. Like I got the $minimum as 0.39 from the interval as above (0,0.99), however, I got 0.87 when I change it to (0.2,0.99), 0.96 for (0.3, 0.99). And the only thing I changed is the interval value, so I'm not sure what I should do to improve it.
> 
> As above, could you help me with these two problems? I would really appreciate that if you could give me some information.
> 
> Thank you very much! I look forward for your reply soon.
> 
> Vicky
> 
> 
> 	[[alternative HTML version deleted]]



More information about the R-help mailing list