[R] Optim() and Instability

Berend Hasselman bhh at xs4all.nl
Sat Nov 14 19:18:43 CET 2015


> On 14 Nov 2015, at 17:02, Berend Hasselman <bhh at xs4all.nl> wrote:
> 
>> 
>> On 14 Nov 2015, at 16:15, Lorenzo Isella <lorenzo.isella at gmail.com> wrote:
>> 
>> Dear All,
>> I am using optim() for a relatively simple task: a linear model where
>> instead of minimizing the sum of the squared errors, I minimize the sum
>> of the squared relative errors.
>> However, I notice that the default algorithm is very sensitive to the
>> choice of the initial fit parameters, whereas I get much more stable
>> (and therefore better?) results with the BFGS algorithm.
>> I would like to have some feedback on this (perhaps I made a mistake
>> somewhere).
>> I provide a small self-contained example.
>> You can download a tiny data set from the link
>> 
>> https://www.dropbox.com/s/tmbj3os4ev3d4y8/data-instability.csv?dl=0
>> 
>> whereas I paste the script I am using at the end of the email.
>> Any feedback is really appreciated.
>> Many thanks
>> 
> 
> The initial parameter values for the percentage error variant are not very good.
> If you print min.perc_error(data,par_ini2) you can see that.
> 
> Try
> 
> par_ini2 <- c(1e-4,1e-4,1e-4)
> 
> and you'll get results that are closer to each other.
> The rest is up to you.

Try this at the end of your script:

# Original
min.perc_error(data,par_ini2)

# Much better
par_ini3 <- c(1e-4,1e-4,1e-4)
min.perc_error(data,par_ini3)
mm_def3 <-optim(par = par_ini3
                  , min.perc_error, data = data)

mm_bfgs3 <-optim(par = par_ini3
                  , min.perc_error, data = data, method="BFGS")

print("fit parameters with the default algorithms and the second seed
")
print(mm_def3$par)
min.perc_error(data,mm_def3$par)
print("fit parameters with the BFGS algorithms and the second seed  ")
print(mm_bfgs3$par)
min.perc_error(data,mm_bfgs3$par)

and rejoice!

Berend



More information about the R-help mailing list