Re: [R] optimization setup

From: Paul Smith <>
Date: Fri, 30 May 2008 18:23:43 +0100

On Fri, May 30, 2008 at 12:04 PM, threshold <> wrote:
> Thanks for all your replies and sorry for a negligence in my examples. They
> are very simplified to reflect very roughly the structure of the case I deal
> with, which is too complicated to be quoted here.
> What I deal with is:
> 1) 'theta' which is vector with length 2, being known to me (eg.
> theta=c(1,2))
> 2) 'x' which represents my empirical data (I know as well),
> 3) 'theta1' is a vector with length 2 I don't know, which suppose to
> minimize my objective function f3 (see below)
> Here come the structure of the functions:
> f1<-function(theta, theta1)
> {theta[1]+theta[2]+theta1[1]}
> f2<-function(theta, x, theta1)
> {f1(theta, theta1)*exp(x)*theta1[2]}
> function to be optimized with respect to theta1:
> f3<-function(theta1)
> {f1(theta, theta1)-f2(theta,x,theta1)}
> Again, I know vector 'theta' and 'x', and I look for (vector) 'theta1' which
> minimize f3
> Question are:
> 1) whether, given the case I deal with, the functions I provided are
> specified correctly. If not what is the correct form?
> 2) how should I write my optim function with starting values for theta1
> equal to 1.1 and 2.1. What I did was:
> optim(par=c(1.1, 2.1), f3) but it did not work well with error message:
> Error in f1(theta, theta1) : argument "theta1" is missing, with no default

If x is a vector, then your function f2 is not a real function; f2 returns a vector (and not a scalar). Optim does not optimize vectorial functions.

Paul mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Mon 02 Jun 2008 - 03:46:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 02 Jun 2008 - 04:30:36 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive