[R] optimization setup

ctu at bigred.unl.edu ctu at bigred.unl.edu
Fri May 30 19:56:48 CEST 2008


I just wonder that if you know theta and x. Why can't you specify  
these in your functions. such as
f1<-function(theta1){1+2+theta1[1]}  (Let me assume theta[1]=1, theta[2]=2)
f2<-function(theta1){f1(theta1)*exp(3)*theta1[2]} (again, I assume x=3)
f3<-function(theta1){f1(theta1)-f2(theta1)}
optim(par=c(1.1, 2.1), f3)

If you know the exact values of theta you should specify in advance  
otherwise optim will try to get the estimated theta[1] and theata[2].

In this case, you might need to use loop for your x vector.

If I am wrong please correct me!
Thanks
Chunhao




Quoting Paul Smith <phhs80 at gmail.com>:

> On Fri, May 30, 2008 at 12:04 PM, threshold <r.kozarski at gmail.com> 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
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list