我在优化负二项式时遇到了一些问题。我总是得到同样的错误。我尝试使用积极的起始值,但这并没有解决问题
initial value in 'vmmin' is not finite
我做了什么:
我定义了对数似然
sum(lgamma((theta/(sigma2 - 1)) + 1) - lgamma(outcome+1) -lgamma(theta/(sigma2 -1)) + outcome*log((sigma2-1)/sigma2) - (theta/(sigma2 - 1)) *log(sigma2))
我将整个功能定义如下。这似乎运行:
ll.negbin <- function(par, covariates, outcome){
if(!all(covariates[,1] == 1))# here I add intercept{
covariates <- as.matrix(cbind(1,covariates))}
theta <- covariates%*%par[1:ncol(covariates)]
gamma <- par[(ncol(covariates)+1)]
sigma2 <- exp(gamma)+1
out <- sum(lgamma((theta/(sigma2 - 1)) + 1) - lgamma(outcome+1) -lgamma(theta/(sigma2 -1)) + outcome*log((sigma2-1)/sigma2) - (theta/(sigma2 - 1)) *log(sigma2))
}
使用optim优化:
opt.negbin <- optim(par = rep(0, ncol(data[,2:6]) + 1),# here you can put your in.var. inside
fn = ll.negbin,
covariates = data[,2:6], # here you can put your columns of in.var.(,2:6).
outcome = data$, # here you can put your dependent variable
control = list(fnscale = -1),
hessian = T,
method = "BFGS")