[R] optim

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Nov 2 18:29:32 CET 2003


On Sun, 2 Nov 2003, Savano wrote:

> Friends,
> 
> I wrote a log-likelihood fuction and optimized but the optim function 
> return a error.  The program and the error are below.
> 
> Can you help me?

The message is not from optim, but from your own function: use traceback() 
to find where errors come from.

Your error is not reading the help page which says

      fn: A function to be minimized (or maximized), with first
          argument the vector of parameters over which minimization is
          to take place. It should return a scalar result.

Since you treat psi as a vector, why define it as an array?

Please don't send the same message repeatedly, and don't send tests.


>   rm(list=ls(all=TRUE))
>  >
>  > duration<-read.table("C:/Documents and Settings/Savano/Meus 
> documentos/Savano/PUC/estudo/NYSE/durationadj.txt",header=T,sep="");
>  >
>  > x <- duration$ibm;
>  > psi <- array(NaN,c(length(x),1));
>  >
>  >     logexp <- function(omega,alpha,bbeta){
> +
> +                 (-sum(log(psi))+sum(x/psi)) #verossimilhança
> +
> +                 psi[1] <- omega/(1-bbeta)   #valor inicial de psi[1]
> +
> +                  for(i in 2:length(x)) {
> +                     psi[i] <- omega+alpha*x+bbeta*psi[i-1] #calculo de psi
> +                  }
> +                  return(logexp)
> +               }
>  >
>  >
>  >
>  > optim(c(0.1,0.2,0.3),logexp,method="BFGS")
> Error in fn(par, ...) : Argument "bbeta" is missing, with no default


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list