优化错误:无法在初始参数下评估功能

时间:2017-09-24 15:55:54

标签: r

library('AER')
data(Affairs)
attach(Affairs)

tobit.ll <-function(theta,y,X){ 
y <- affairs
X <- cbind(1, age, yearsmarried, religiousness ,occupation ,rating)
n <-nrow(X)
k <-ncol(X)
sigm <-theta[k+1]
bet <- theta[1:k]  
bet[1]<-theta[1]; bet[2] <- theta[2]; bet[3]<- theta[3]; 
bet[4] <-theta[4]; bet[5] <- theta[5]; bet[6] <- theta[6]; sigm <-theta[7]
theta <-c(bet, sigm)
res <- y-X%*%bet

for(j in 1:k){
   for(i in 1:n){

       if(y[i]>0){
       d=1
       } else {
       d=0
       }
    ll <-sum(d*(-0.5*n*log(2*pi)-0.5*n*log(sigm)^2-sum(0.5* (t(res[i])%*%res[i])^2/sigm^2))+(1-d)*(log(1-pnorm((X[i,j]*bet[j])/sigm))))
   } 
 }  
 return(ll)
}

tobit.b <- optim(c(0,0,0,0,0,0,0),tobit.ll, method="Nelder-Mead")

=============================================== ========================

我认为这些参数是vetors。我不知道为什么我一直有这个错误信息。

1 个答案:

答案 0 :(得分:1)

问题是sigm,即初始条件向量中的第7位元素,不应该等于零。试试这个:

tobit.b <- optim(c(0,0,0,0,0,0,.5),tobit.ll, method="Nelder-Mead")