# [R] R: optim

Douglas Bates dmbates at gmail.com
Tue Sep 6 14:35:44 CEST 2005

On 9/6/05, Clark Allan <Allan at stats.uct.ac.za> wrote:
> hi all
>
> i dont understand the error message that is produced by the optim
> function. can anybody help???
>
> ie:
> [[1]]\$message
> [1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH"
>
> can anyone help?

That code indicates that the optimizer has declared convergence
because the relative reduction in the objective function in successive
iterates is below a tolerance.  As documented in ?optim, a convergence
code of 0 indicates success

...
convergence: An integer code. '0' indicates successful convergence.
Error codes are
...

This may be counter-intuitive but it does make sense to shell
programmers.  The idea is that there is only one way you can succeed
but there are many different ways of failing so you use the nonzero
codes to indicate the types of failure and the zero code, which we
usually read as FALSE in a logical context, to indicate success.

>
>
>
> ###########################################################################
>
> SK.FIT(XDATA=a,XDATAname="a",PHI1=1,v=5,vlo=2,vhi=300,phi2lo=.01)
> [[1]]
> [[1]]\$par
> [1]  -0.01377906   0.83859445   0.34675230 300.00000000
>
> [[1]]\$value
> [1] 90.59185
>
> [[1]]\$counts
>       53       53
>
> [[1]]\$convergence
> [1] 0
>
> [[1]]\$message
> [1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH"
>
> #################################################################################
>
>
>
> i ghave included the function used in the optim call:
>
> SKEWMLE=function(l,DATA=XDATA,...)
>         {
>                 #alpha = l[1]
>                 #beta = l[2]
>                 #phi2 = l[3]
>                 #v= l[4]
>                 phi1=PHI1
>
>                 DATA<-as.matrix(DATA)
>
>                 fnew<-function(x,y,l,...)
>                 {
>                         #when we do not estimate phi1
>                         t1=(1+((y-l[1]-l[2]*x)^2)/(l[4]*l[3]^2))^(-0.5*(1+l[4]))
>                         t2=(1+(x^2)/l[4])^(-0.5*(1+l[4]))
>                         t3=2*((gamma(0.5*(1+l[4]))/(gamma(0.5*l[4])*sqrt(l[4]*pi)))^2)/l[3]
>
>                         t1*t2*t3
>                 }
>
>                 a<-double(length(DATA))
>                 y=DATA
>                 a=apply(y,1,function(q)
> log(integrate(fnew,lower=0,upper=Inf,y=q,l=l)\$value))
>                 -sum(a)
>         }
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help