# Re: [R] optimization setup

From: threshold <r.kozarski_at_gmail.com>
Date: Fri, 30 May 2008 04:04:29 -0700 (PDT)

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

Thank you for your help and time
robert

Ray Brownrigg-2 wrote:
>
>
> Did you copy-paste that error message?
>
> I get:
>

```>> optim(par=c(1,1), f3)
```

```>>
```

>
> You seem to be confusing your formal and actual parameters.
>
> When you invoke f3 (within optim()), theta is not defined within f3().
>
> Ray
>
> On Fri, 30 May 2008, threshold wrote:
```>> Hi, thanks for your replay the previous post of mine. Let me ask you one
>> more question similar to the previous one, based on a naive example:
>>
>> 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)}
>> optim(par=c(1,1), f3)
>>
>> If I do so I get:
>> Error in f1(theta, theta1) : argument "theta1" is missing, with no
>> default
>> What I did wrong? I guess I miss something basic...
>>
>> Ray Brownrigg-2 wrote:
>> > On Wed, 23 Apr 2008, threshold wrote:
>> >> Hi, here comes my problem, say I have the following functions (example
>> >> case) #------------------------------------------------------------
>> >> function1 <- function (x, theta)
>> >> {a <- theta[1] ( 1 - exp(-theta[2]) ) * theta[3] )
>> >>  b <- x * theta[1] / theta[3]^2
>> >>  return( list( a = a, b = b )) }
>> >> #-----------------------------------------------------------
>> >> function2<-function (x, theta)
>> >> {P <- function1(x, theta)
>> >>   c <- P\$a * x * exp(-theta[2])
>> >>   d <- P\$b * exp(x)
>> >>   q <- theta[1] / theta[3]
>> >>   res <- c + d + q; res}
>> >>
>> >> # Function to be optimized
>> >> function3 <- function(theta1,theta2,theta3) {
>> >> n <- length(data)
>> >> -sum( function2(x = data[2:n], theta = c(theta1, theta2, theta3) ))}
>> >> # 'data' is my input ts class object
>> >>
>> #-----------------------------------------------------------------------
>> >>--- ------------
>> >>
>> >> Then I want to maximize function3 with respect to theta(s) (given some
>> >> starting values)
>> >>
>> >> fit<-optim(par=c(theta1=1, theta2=1.2, theta3=.2), fn=function3)
>> >>
>> >> I get the following:
>> >> Error in function1(x, theta) :
>> >>   argument "theta2" is missing, with no default
>> >>
>> >> Where I made a mistake? I will appreciate any help ...
>> >>
>> >> r
>> >
>> > Your function to be optimised must be a function of a single parameter
>> > (which
>> > may be a vector).
>> >
>> > So you should have something like:
>> > # Function to be optimized
>> > function3 <- function(thetas) {
>> >   n <- length(data)
>> >   -sum( function2(x = data[2:n], theta = thetas))
>> > }
>> >
>> > [Not tested, your provided code has typos].
>> >
>> > Ray
>> >
>> > ______________________________________________
>> > R-help_at_r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
```

>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
```--
View this message in context: http://www.nabble.com/optimization-setup-tp16825410p17556348.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help