Yeah, you're right about that, but it appears that's not what is
generating this error. See below:
#NEGATIVE BINOMIAL LOG-LIKELIHOOD FUNCTION
ll.negbin <- function(par, X, Y){
+ theta <- X%*%par[1:ncol(X)]
+ gamma <- par[(ncol(X)+1)] # [2]
+ sigma2 <- exp(gamma)+1
+ out <- sum(lgamma((theta/(sigma2 - 1)) + 1) - lgamma(Y+1) - lgamma
(theta/(sigma2 -1)) + Y*log((sigma2-1)/sigma2) - (theta/(sigma2 - 1))
*log(sigma2))
+ }
#OPTIMIZE
opt <- optim(c(0,0,0,0,0), ll.negbin, X = X, Y = Y, method =
"BFGS",
control = list(fnscale = -1), hessian = TRUE)
Error in optim(c(0, 0, 0, 0, 0), ll.negbin, X = X, Y = Y, method =
"BFGS", :
initial value in 'vmmin' is not finite
In addition: Warning messages:
1: In fn(par, ...) : value out of range in 'lgamma'
2: In fn(par, ...) : value out of range in 'lgamma'
3: In fn(par, ...) : value out of range in 'lgamma'
4: In fn(par, ...) : value out of range in 'lgamma'
5: In fn(par, ...) : value out of range in 'lgamma'
6: In fn(par, ...) : value out of range in 'lgamma'
7: In fn(par, ...) : value out of range in 'lgamma'
8: In fn(par, ...) : value out of range in 'lgamma'
x <- ll.negbin(c(0,0,0,0,0), X, Y);x
[1] -Inf
Thanks,
Keith
On Apr 17, 2008, at 7:54 AM, Martin Andersen wrote:
Keith,
the way you treat sigma2 is giving you trouble. According to the
notes,
sigma2 has to be >1, but your code allows sigma2<1. Try rerunning the
code, but where you wrote:
+ sigma2 <- exp(gamma)
try
+ sigma2 <- exp(gamma)+1
Martin
Keith Schnakenberg wrote:
Hello classmates,
I am running into a snag with my negative binomial log likelihood
function that I cannot seem to fix. My code and error are below:
#NEGATIVE BINOMIAL LOG-LIKELIHOOD FUNCTION
ll.negbin <- function(par, X, Y){
+ theta <- X%*%par[1:ncol(X)]
+ gamma <- par[(ncol(X)+1)]
+ sigma2 <- exp(gamma)
+ out <- sum(lgamma((theta/(sigma2 - 1)) + 1) - lgamma(Y+1) - lgamma
(theta/(sigma2 -1)) + Y*log((sigma2-1)/sigma2) - (theta/(sigma2 - 1))
*log(sigma2))
+ }
#OPTIMIZE
opt <- optim(c(0,0,0,0,0), ll.negbin, X = X, Y = Y, method =
"BFGS",
control = list(fnscale = -1), hessian = TRUE)
Error in optim(c(0, 0, 0, 0, 0), ll.negbin, X = X, Y = Y, method =
"BFGS", :
initial value in 'vmmin' is not finite
So there's some problem with my log likelihood function, but I'm not
sure what it is. To elaborate on the problem, here are the results
when I evaluate the function outside of optim:
test <- ll.negbin(par=c(0,0,0,0,0), X=X,
Y=Y);test
[1] NaN
What is my problem here?
Thank you,
Keith
_______________________________________________
gov2001-l mailing list
gov2001-l at
lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l
--
Martin Sparre Andersen, MPH
Harvard University
Program in Health Policy
anders4 at
fas.harvard.edu
martin.andersen at
aya.yale.edu
AIM: AndersenEPH
Community of Science:
http://myprofile.cos.com/msa25cu
_______________________________________________
gov2001-l mailing list
gov2001-l at
lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l