Hello! I am getting the following errors when running optim() [I tried optim() with 3 different methods as you can see]: Error in optim(c(0.66, 0.999, 0.064), pe, NULL, method = "L-BFGS-B") : objective function in optim evaluates to length 6 not 1> out <- optim( c(0.66, 0.999, 0.064), pe, NULL, method = "Nelder-Mead")Error in optim(c(0.66, 0.999, 0.064), pe, NULL, method = "Nelder-Mead") : objective function in optim evaluates to length 6 not 1> out <- optim( c(0.66, 0.999, 0.064), pe, NULL, method = "CG")Error in optim(c(0.66, 0.999, 0.064), pe, NULL, method = "CG") : objective function in optim evaluates to length 6 not 1 I don't know what the error message should mean to me. Particulars for the problem are listed below. Thanks in advance for any help!! Stu> y <-c(0.111111111111111,0.0921052631578947,0.0564516129032258,0.0608108108108108 ,0.0128205128205128,0.0136078431372549)> x3 <- c(3600,169200,185400,255600,271800,342000)> x2 <- c(1,3,4,5,6,7)> x1 <-c(0,0.978723404255319,0.087378640776699,0.549295774647887,0.0596026490066225 ,0.61578947368421)> observs <- data.frame(y, x1, x2, x3)> attach(observs)> pe <- function(c) c[1]*x1*x2^c[2]*x3^c[3]> out <- optim(c(0.66, 0.999, 0.064), pe, NULL, method = "L-BFGS-B")[[alternative HTML version deleted]]
Hello, On 6/16/09, Stu @ AGS <stu at agstechnet.com> wrote:> Error in optim(c(0.66, 0.999, 0.064), pe, NULL, method = "L-BFGS-B") : > > objective function in optim evaluates to length 6 not 1 ><skip>> > > pe <- function(c) c[1]*x1*x2^c[2]*x3^c[3] >I would suspect a matrix multiplication issue. In order to minimise your function optim expects the result of your specific function to be a scalar. Are you sure that after all the multiplications pe results in a vector of dimension 1x1? Liviu
Thanks for your response! No, my basic equation does not use matrices at all. It takes scalar values and returns a scalar. What I am trying to accomplish is to find the "best-fit" coefficients to the equation as follows: y ~ c1 * x1 * x2^c2 * x3^c3 where y, x1, x2, and x3 are observed data and c1, c2, and c3 are regression coefficients. I thought the correct way to use optim() to find the coefficients was to put the data observations into a data.frame object, then call optim() Maybe I am not using optim() correctly? My observed data are (as vectors where each row of observations are located at the same index): y <- c(0.111111111111111, 0.0921052631578947, 0.0564516129032258, 0.0608108108108108, 0.0128205128205128, 0.0136078431372549) x3 <- c(3600, 169200, 185400, 255600, 271800, 342000) x2 <- c(1, 3, 4, 5, 6, 7) x1 <- c(0, 0.978723404255319, 0.087378640776699, 0.549295774647887, 0.0596026490066225, 0.61578947368421) Thank you again, Stu> -----Original Message----- > From: Liviu Andronic [mailto:landronimirc at gmail.com] > Sent: Tuesday, June 16, 2009 2:35 AM > To: Stu @ AGS > Cc: r-help at r-project.org > Subject: Re: [R] Trouble with optim on a specific problem > > Hello, > > On 6/16/09, Stu @ AGS <stu at agstechnet.com> wrote: > > Error in optim(c(0.66, 0.999, 0.064), pe, NULL, method = "L-BFGS-B") > : > > > > objective function in optim evaluates to length 6 not 1 > > > <skip> > > > > > pe <- function(c) c[1]*x1*x2^c[2]*x3^c[3] > > > I would suspect a matrix multiplication issue. In order to minimise > your function optim expects the result of your specific function to be > a scalar. Are you sure that after all the multiplications pe results > in a vector of dimension 1x1? > Liviu