假设,我有一个功能并想优化它。但如果我使用optim()
which 给出warnings()
. 我怎样才能避免这些警告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