以下代码是查找广义beta分布的3个参数。
beta1 = function(a,b,t) { beta(a+(1/t),b) }
beta2 = function(a,b,t) { beta(a+(2/t),b) }
geb11 = function(a,b,t) { beta2(a,b,t)/beta(a,b) }
geb12 = function(a,b,t) { (beta1(a,b,t)-beta2(a,b,t))/beta(a,b) }
geb22 = function(a,b,t) { 1 + (beta2(a,b,t)-2*beta1(a,b,t))/beta(a,b) }
gbetloglik = function(v) {
a = v[1]; b = v[2]; t = v[3]
loglik = n1*log(geb11(a,b,t)) + n3*log(geb12(a,b,t)) + n2*log(geb22(a,b,t))
return(-loglik)
}
n1 = 127; n2 = 111; n3 = 262
abt = optim(c(5,5,1),gbetloglik,lower=c(0.01,0.01,0.1),method="L-BFGS-B")$par
然后它会发出如下错误并发出许多警告。
Error in optim(c(2, 8, 1), gbetloglik, lower = c(0.01, 0.01, 0.1), method = "L-BFGS-B") :
L-BFGS-B needs finite values of 'fn'
警告信息
1: In beta(a + (2/t), b) : underflow occurred in 'beta'
2: In beta(a, b) : underflow occurred in 'beta'
3: In beta(a + (1/t), b) : underflow occurred in 'beta'
...
n1,n2和n3由一些随机数生成场景生成,并且(n1,n2,n3)决定是否发生错误。 (对于某些(n1,n2,n3),不会发生错误)
我不明白这个错误的原因是什么以及" beta' beta'装置
答案 0 :(得分:1)
由于警告消息表明计算测试版时存在下溢。
试试这个(我们添加了##行):
gbetloglik = function(v) {
a = v[1]; b = v[2]; t = v[3]
if (beta(a, b) == 0) return(1e6) ##
loglik = n1*log(geb11(a,b,t)) + n3*log(geb12(a,b,t)) + n2*log(geb22(a,b,t))
return(-loglik)
}