2017-09-24 100 views
0
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

問題是sigm,即初始條件向量中的地點7的元素不應等於零。試試這個:

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