r - optim() function error in parameter estimation -
the following code find 3 parameters of generalized beta distribution.
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) { = 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
then gives error follows many warnings.
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'
the warning messages
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 generated random number generation scenarios, , (n1,n2,n3) decides whether error occurs. (for (n1,n2,n3) error not occur)
i don't understand reason of error , "underflow in 'beta' means.
as warning message indicates there underflow when computing beta.
try (where have added ## line):
gbetloglik = function(v) { = 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) }
Comments
Post a Comment