Dimitri Liakhovitski
2011-Mar-30 16:45 UTC
[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
Bert Gunter
2011-Mar-30 17:03 UTC
[R] optim and optimize are not finding the right parameter
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
Ravi Varadhan
2011-Mar-30 18:06 UTC
[R] optim and optimize are not finding the right parameter
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: rvaradhan at jhmi.edu
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at 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 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.
Dimitri Liakhovitski
2011-Mar-30 18:25 UTC
[R] optim and optimize are not finding the right parameter
Thank you, Ravi - definitely better! On Wed, Mar 30, 2011 at 2:06 PM, Ravi Varadhan <rvaradhan at 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: rvaradhan at jhmi.edu > > -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at 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 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. > >-- Dimitri Liakhovitski Ninah Consulting www.ninah.com