I am simulating values of the Birnbaum-Saunders distribution with alpha and beta parameters and then try to estimate these values using the optimum function, although the simulation converges, I am getting the error below, how to correct this error:
n<-1000
z<-rnorm(n,0,1)
alpha=2
beta=2
t<-beta*((alpha*z/2)+sqrt((alpha*z/2)^2+1))^2
remove(par,alpha,beta)
Loglik<-function(par){
alpha=par[1]
beta=par[2]
ll<-sum(log(t+beta))-n*log(2*alpha)-(n/2)*(log(2*pi*beta))-(3/2)*sum(log(t))-((1/(2*alpha^2))*sum((t/beta)+(beta/t)-2))
return(-ll)
}
alpha_0=1
beta_0=5
start=c(alpha_0,beta_0)
optim(start,fn=Loglik,method="BFGS",hessian=T)$par
Result:
[1] 2.018785 2.133996
There were 25 warnings (use warnings() to see them)
The errors are:
> warnings()
Mensagens de aviso:
1: In log(t + beta) : NaNs produced
2: In log(2 * pi * beta) : NaNs produced
3: In log(t + beta) : NaNs produced
I've checked my verisimilitude, which is correct, can not find the error in the code?