[R] optim

Uwe Ligges ligges at statistik.uni-dortmund.de
Sun Nov 2 18:20:18 CET 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?
> 
> 
> 
>  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


The Error message tells you: you forgot to specify a value for (alpha 
and) bbeta. I guess you want to optimize over all three arguments of 
logexp()? In that case you have to vectorize them into one argument.

But there are SEVERAL other errors in your function!!!

The line
  psi[i] <- omega+alpha*x+bbeta*psi[i-1]
is not sensible given that length(x)>1 ...

The initialization of psi should take place within logexp() using, e.g., 
psi <- numeric(length(x))

I think you don't want to return "logexp", but a single value (that one 
you are goiung to minimize here.

Please read "An Introduction to R" and look into an appropriate book 
(like "Modern Applied Statistics with S" or "S Programming", both by 
W.N. Venables and B.D. Ripley) in order to see how to solve your problem!


BTW: Messages re "teste" are extremely annoying!

Uwe Ligges




More information about the R-help mailing list