Thank you, Ravi - definitely better! On Wed, Mar 30, 2011 at 2:06 PM, Ravi Varadhan <rvarad...@jhmi.edu> wrote: > Try this: > > pred <- pred/1e06 > > DV <- DV/1e03 > > opt1 <- optim(fn=my.function, par=1.0) > opt2 <- optim(fn=my.function, par=1.0, method="BFGS") > opt3 <- optim(fn=my.function, par=1.0, method="L-BFGS-B", lower=0, upper=1) > opt1 > opt2 > opt3 > > Ravi. > > ------------------------------------------------------- > Ravi Varadhan, Ph.D. > Assistant Professor, > Division of Geriatric Medicine and Gerontology School of Medicine Johns > Hopkins University > > Ph. (410) 502-2619 > email: rvarad...@jhmi.edu > > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of Dimitri Liakhovitski > Sent: Wednesday, March 30, 2011 12:45 PM > To: r-help > Subject: [R] optim and optimize are not finding the right parameter > > 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@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. > >
-- Dimitri Liakhovitski Ninah Consulting www.ninah.com ______________________________________________ R-help@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.