yoshi yoshi - 24 days ago 5
R Question

Optim error: function cannot be evaluated at initial parameters

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")


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

I think these parameters are vetors. I don't know why I have this error message all the time.

Answer Source

The problem is that sigm, i.e. the element of place 7 in the vector of initial conditions, should not be equal to zero. Try this:

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