[R] Help fix last line of my optimization code

S Ellison Stephen.Ellison at lgcgroup.com
Fri Jul 1 14:20:33 CEST 2011


You need to read the help page for optimize. Again, if you already have ;-)

The error message 
>Error in optimize(llik, init.params = F) : element 1 is empty;
>    the part of the args list of 'min' being evaluated was:
>    (interval)####
is telling you that the parameter (interval) required by optimise in the absence of a specified min and max is empty.

And if you look at your code you will see that that is because you have not provided it....

There are other problems:
Optimize requires a function taking _one_ parameter to optimise and an interval to optimise over, or alternatively a maximum and a minimum. You have provided no interval, max or min (which is why it went looking for interval) and you have fed it a function taking two parameters, both of which you want optimised. You have also given it initial parameters (?) as a named parameter which it can't recognise - it's not one of the named arguments to optimize - so optimize will try to pass that to the function being optimised. That will fail if it ever gets there because _that_ doesn't use a parameter called init.params either. I'm not clear what you wanted init.params to do but at a rough guess you had in mind a function for which init.params is the initial parameter vector and if FALSE is guessed at. optmize() is not that function.

Perhaps you meant to use optim - which does two-parameter optimisation - but you _will_ need to specify a starting vector as that doesn't take an init.params argument either.

As an alternative, you could use something like nls which takes a self-starting function argument which generats its own initial values, but you'll have to create the self-starting function yourself (see ?selfStart)



> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Edward Bowora
> Sent: 01 July 2011 01:46
> To: r-help
> Subject: [R] Help fix last line of my optimization code
> 
> Hi
> 
> I need help figure out how to fix my code.
> 
> When I call into R
> >optimize(llik,init.params=F)
>  I get this error message
> ####Error in optimize(llik, init.params = F) : element 1 is empty;
>    the part of the args list of 'min' being evaluated was:
>    (interval)####
> 
> 
> My data and my code looks like below.
> 
> 
> R_j		R_m
> 0.002		0.026567296
> 0.01		0.003194435
> .		.
> .		.
> .		.
> .		.
> 0.0006	0.010281122
> 
> a=read.table("D:/ff.txt",header=T)
> attach(a)
> llik=function(R_j,R_m)
> #The parameters al_j, au_j, b_j ,
> and sigma_j need to be estimated and there are no initial 
> estimates to them.
> if(R_j< 0)
> {
>  
> LF=sum[log(1/(2*pi*(sigma_j^2)))-(1/(2*(sigma_j^2))*(R_j+al_j-
> b_j*R_m))^2]
> }else if(R_j>0)
> {
>  
> LF=sum[log(1/(2*pi*(sigma_j^2)))-(1/(2*(sigma_j^2))*(R_j+au_j-
> b_j*R_m))^2]
> }else
> {
>  
> LF=sum[(log(pnorm((au_j-b_j*R_m)/sigma_j)-pnorm((al_j-b_j*R_m)
> /sigma_j)))]
> }
> optimize(llik,init.params=F)
> Error in optimize(llik, init.params = F) : element 1 is empty;
>    the part of the args list of 'min' being evaluated was:
>    (interval)
> 
> Thank you
> 
> Edward
> 
> ______________________________________________
> 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.
> *******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list