3
I am simulating values of the Birnbaum-Saunders distribution with alpha and beta parameters and later try to estimate these values using the Optim function, although the simulation converges, I am getting the error below, how to proceed 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
Upshot:
[1] 2.018785 2.133996
There were 25 warnings (use warnings() to see them)
The mistakes 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 already checked my verisimilitude, which is correct, I can’t find the error in the code?
@fsblajinha Good, the mathematical solution seems the best way out. You can put as answer, even though the question is yours!
– Molx