2
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
}
}
When running the program, in addition to not generating the n*samples I need, it shows 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 don’t know what else to do to make it work.
How about showing the text that appears in one of the 17 warnings of when you run
warnings()
?– Tomás Barcellos
Try to explain better what your code does and go segmenting its parts instead of putting in a big code like that. You happen to be dealing with nonlinear optimization?
– TheBiro
If the problem is only generate the samples, without estimating their parameters later, there is a lot of code there. It seems to me that the function
ree
is sufficient to generate the samples. Much of it comes from the comment##### FUNCAO DENSIDADE DE PROBABILIDADE
does not concern the generation of random numbers, but rather the estimation of the parameters of the probability distribution for the generated samples.– Marcus Nunes