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

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Wed Mar 30 18:45:10 CEST 2011


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



More information about the R-help mailing list