[R] loop in optim

John C Nash nashjc at uottawa.ca
Thu Jul 7 13:40:05 CEST 2011

2 comments below.

On 07/07/2011 06:00 AM, r-help-request at r-project.org wrote:
> Date: Wed, 6 Jul 2011 20:39:19 -0700 (PDT)
> From: EdBo <n.bowora at gmail.com>
> To: r-help at r-project.org
> Subject: Re: [R] loop in optim
> Message-ID: <1310009959045-3650592.post at n4.nabble.com>
> Content-Type: text/plain; charset=us-ascii
> I have one last theoretical question, I did not adjust my code prior so that
> it maximise the likehood function. I googled that to make optim maximise you
> multiply fn by -1.
> In my code, would that be the same as saying "-sum" on the "sum" part of my
> code (see below)?
> llik = function(x) 
>    { 
>     al_j=x[1]; au_j=x[2]; sigma_j=x[3];  b_j=x[4]
>     sum(na.rm=T,
>         ifelse(a$R_j< 0, log(1 / ( sqrt(2*pi) * sigma_j) )-

The optimx package has a "maximize" control because we felt that the fnscale approach,
while perfectly correct, is not comfortable for users and is not standard across other
optimization tools. Note that this package is undergoing a fairly extensive overhaul at
the moment (the development version is on R-forge in the project 'optimizer') to include
some safeguards on functions that return NaN etc. as well as a number of other changes --
hopefully improvements.

A second comment on this looping: Why do you not use the parameters from the last
estimation as the starting parameters for the next? Unless you are expecting very extreme
changes over the moving window of data, this should appreciably speed up the optimization.

John Nash

More information about the R-help mailing list