[R] optim ???

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Nov 22 17:47:15 CET 2001


On Thu, 22 Nov 2001, kjetil halvorsen wrote:

> Hola!
>
> santes<-c(210,169,187,160,167,176,185,206,173,146,174,201,198,148,154)
> sdespues<-c(201,165,166,157,147,145,168,180,147,136,151,168,179,129,131)
> dantes<-c(130,122,124,104,112,101,121,124,115,102,98,119,106,107,100)
> ddespues<-c(125,121,121,106,101,85,98,105,103,98,90,98,110,103,82)
> presion<-data.frame( santes, sdespues, dantes, ddespues)
>
>
> logvero <- function(mu,delta,s2ksi,s2eps,beta,s2eta,x,z)
>   {
>     n <- length(x)
>     sigma <- matrix(c(s2ksi+s2eps,beta*s2ksi-s2eps,beta*s2ksi-s2eps,
>                     beta^2*s2ksi+s2eta+2*s2eps),2,2)
>     xz <- sum((x-mu)*(z-delta))
>     A <- matrix(c(sum((x-mu)^2),xz,xz,sum((z-delta)^2)),2,2)
>     A <- (1/2)*solve(sigma) %*% A
>     logdet <- (-1)*n*log(det(sigma))
>     logdet-sum(diag(A))
>   }
>
> optim(par=c(mu=mean(santes),delta=mean(sdespues-santes),s2ksi=422,s2eps=1,s2eta=75),
>        fn=function(mu,delta,s2ksi,s2eps,s2eta)
>            logvero(mu,delta,s2ksi,s2eps,0,s2eta,santes,sdespues-santes),
>            control=list(trace=1, fnscale=-1),
>             method="BFGS")
>
>
> gives the result:
>
> Error in as.vector(data) : Argument "s2ksi" is missing, with no default
>
>
> What is happening?

You are not using optim correctly.

     par: Initial values for the parameters to be optimized over.

      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.

     ...: Further arguments to be passed to `fn' and `gr'.

I think you wanted


optim(par=mean(santes),
        fn=function(mu,delta,s2ksi,s2eps,s2eta)
            logvero(mu,delta,s2ksi,s2eps,0,s2eta,santes,sdespues-santes),
            control=list(trace=1, fnscale=-1),
             method="BFGS",
            delta=mean(sdespues-santes), s2ksi=422, s2eps=1, s2eta = 75)

or something like that.  Otherwise fn needs to a function of one
parameter only.


-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list