[R] optim and optimize are not finding the right parameter

Bert Gunter gunter.berton at gene.com
Wed Mar 30 19:03:43 CEST 2011


Not sure it's the case here, but numeric optimizers are well-known to
be subject to scaling issues.

-- Bert

On Wed, Mar 30, 2011 at 9:45 AM, Dimitri Liakhovitski
<dimitri.liakhovitski at gmail.com> wrote:
> Dear all,
>
> I have a function that predicts DV based on one predictor pred:
>
> pred<-c(0,3000000,7800000,15600000,23400000,131200000)
> DV<-c(0,500,1000,1400,1700,1900)
>
> ## I define Function 1 that computes the predicted value based on pred
> values and parameters a and b:
> calc_DV_pred <- function(a,b) {
>  DV_pred <- rep(0,(length(pred)))
>  for(i in 1:length(DV_pred)){
>   DV_pred[i] <- a * (1- exp( (0-b)*pred[i] ))
>  }
>  return(DV_pred)
> }
>
> ## I define Function 2 that computes the sum of squared deviations for
> predicted DV from actual DV:
> my.function <- function(param){
>  b<-param
>  a<-max(DV)
>  mysum <- sum((DV - calc_DV_pred(a,b))^2)
>  return(mysum)
> }
>
> If I test my function for parameter b =0.0000001, then I get a very good fit:
> pred<-c(0,3000000,7800000,15600000,23400000,131200000)
> DV<-c(0,500,1000,1400,1700,1900)
> test<-my.function(0.0000001)
> (test) # I get 11,336.81
>
> However, when I try to optimize my.function with optim - trying to
> find the value of b that minimizes the output of my.function - I get a
> wrong solution:
> opt1 <- optim(fn=my.function,par=c(b=0.00000001),
>     method="L-BFGS-B", lower=0,upper=1)
> (opt1)
> test2<-my.function(opt1$par) # is much larger than the value of "test" above.
>
> # And getting the same - wrong - result with optimize:
> opt2 <- optimize(f=my.function,interval=c(0,0.1))
> test3<-my.function(opt2$minimum)
>
> What am I doing wrong? Thanks a lot for your recomendations!
>
> --
> Dimitri Liakhovitski
> Ninah Consulting
> www.ninah.com
>
> ______________________________________________
> 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.
>



-- 
"Men by nature long to get on to the ultimate truths, and will often
be impatient with elementary studies or fight shy of them. If it were
possible to reach the ultimate truths without the elementary studies
usually prefixed to them, these would not be preparatory studies but
superfluous diversions."

-- Maimonides (1135-1204)

Bert Gunter
Genentech Nonclinical Biostatistics



More information about the R-help mailing list