Suppose, I have a function and want to optimize it. But if I use optim() which gives warnings(). How can I avoid these warnings of NaN?
myfun<-function(par, x){
f<- sum(x)*length(x)+sum(log(gamma(par))*x)+1
return(-f)
}
optim(0.1, myfun, x=c(1,5,4,7,8,5,6,5,45,8))
$par
[1] 4.203895e-46
$value
[1] -10762.39
$counts
function gradient
502 NA
$convergence
[1] 1
$message
NULL
There were 50 or more warnings (use warnings() to see the first 50)
warnings()
2: In log(gamma(par)) : NaNs produced
3: In log(gamma(par)) : NaNs produced
4: In log(gamma(par)) : NaNs produced