# [R] Optim function & Hessian matrix

David Winsemius dwinsemius at comcast.net
Wed Nov 20 08:05:11 CET 2013

```This is the code that was attached. I arrived in the copy sent to my email address:

#------------------

L1=function(X){
B1=X
C1=X
B2=X
C2=X
mu=X

S=-(B1*(C1^x)*((C1^v)-1)/log(C1))
+(d2*log(B1*(C1^(x+v))))
-(B2*(C2^y)*((C2^v)-1)/log(C2))
+(d1*log(B2*(C2^(y+v))))
-(v*mu)
+(d3*log(mu))

return(sum(S))
}

L2=function(X){
B3=X
C3=X
sum(-(B3*(C3^(x+u1)-C3^(x))) + h1*log(B3*C3^(x+u1)))
}

L3=function(X){
B4=X
C4=X
sum(-(B4*(C4^(y+u2)-C4^(y))) + h2*log(B4*C4^(y+u2)))
}

L=function(X){
B1=X
C1=X
B2=X
C2=X
mu=X
B3=X
C3=X
B4=X
C4=X

L=L1(c(B1,C1,B2,C2,mu))
+L2(c(B3,C3))
+L3(c(B4,C4))
-L
}

Sol=nlminb(c(7.741*10^(-7),1.155,2.422*10^(-5),1.0889,.001307,2.51*10^(-5),1.0712,3.7654*10^(-4),1.0561),L,
lower=c(7.000*10^(-7),1.055,2.122*10^(-5),1.0689,.001107,2.13*10^(-5),1.0088,3.5437*10^(-4),1.0612),
upper=c(1.0299*10^(-6),1.214,2.822*10^(-5),1.0989,.015407,2.77*10^(-5),1.1888,3.8737*10^(-4),1.0812),control=list(iter.max=1500))

par=Sol\$par

optim(par,L,method="L-BFGS-B",
lower=c(7.000*10^(-7),1.055,2.122*10^(-5),1.0689,.001107,2.13*10^(-5),1.0088,3.5437*10^(-4),1.0612),
upper=c(1.0299*10^(-6),1.214,2.822*10^(-5),1.0989,.015407,2.77*10^(-5),1.0888,3.8737*10^(-4),1.0712),control=list(maxit=1500,pgtol=10^(-7)),hessian=TRUE)

--
David.

On Nov 19, 2013, at 12:09 PM, sofeira taajobian wrote:

> Dear  R Users
> Hi,
>
>
> I have very emergency problems in my programming about  finding  MLE
> with optim command. I reproduced it  with real data. I guess that my
> function object in optim is very sensitive because it has power
> function .
> Then optim give me lower or initial values for estimates with these
> warnings for Hessian matrix computation:
> 1: In log(B2 * (C2^(y + v))) : NaNs produced
> 2: In log(B3 * C3^(x + u1)) : NaNs produced
> 3: In log(B4 * C4^(y + u2)) : NaNs produced
> 4: In log(B1 * (C1^(x + v))) : NaNs produced
> But I have  in result a hessian matrix with only first (2*2) block and
> other values are zero.
> It would not be the problem of code lead to this.  plz check the code
> and data, I attach them with this email. hope it can reduce some
> workload as copying and pasting.
>
>
> what may lead to this and any possible way to solve it? any suggestion
> are  appreciated. Plz help me as soon as possible because I don’t have
> enough time.
> <data4.txt>______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help