I'm trying to generate 500 samples of size 50 each, but it's not working. The program follows below.
require("gsl")
n=50
amostra = 500
alpha = 2
beta = 3
X=NULL
X<-matrix(0,nrow=n,ncol=amostra)
##### FUNCAO PARA GERAR UMA AMOSTRA
ree = function(n, alpha, beta){
u = runif(n, 0, 1)
x = -(alpha + beta * lambert_Wm1((-1 + u) * (alpha + beta) * exp(-
(alpha + beta) / beta) / beta) + beta) / alpha / beta
return(x)
}
##### FUNCAO DENSIDADE DE PROBABILIDADE
fx = function(x,t) { (t[1] ^ 2) * exp(-t[1] * x) * (1 + t[2] * x) / (t[1] + t[2]) }
##### FUNCAO LOG VEROSSIMILHANCA
l = function(t) { log( prod(fx(dados,t)) )}
lista_estimativas = NULL
lista_sd = NULL
obs = NULL
repeat {
dados = ree(n, alpha, beta)
media = mean(dados)
res = optim(c(1,1), fn=l, method="BFGS", control=list(fnscale = -1), hessian = TRUE) #calcular o máximo da função
estimate = res$par
est = estimate[1]
est2 = estimate[2]
sd=sqrt(diag(solve(-res$hessian)))
if( (!is.nan(sd[1]) & !is.nan(sd[2])) & (est >= 1/media & est <= 2/media) & est2 >0 ){
lista_estimativas = c(lista_estimativas, res$par[1], res$par[2])
lista_sd = c(lista_sd, sd)
obs = c(obs,dados)
}
if(length(obs) == n*amostra){
break
}
}
While running the program, in addition to not generating the n * samples I need, it displays the following error:
Error in optim(c(1, 1), fn = l, method = "BFGS", control = list(fnscale = -1), :
non-finite finite-difference value [1]
Além disso: There were 17 warnings (use warnings() to see them)
I do not know what else to do.