Deal all:
i want to do the probit with sample selection estimation, the following
is my code:
probit with sample selection can be done by stata :heckprob
The heckprobll is the likelihood function shown in W.H. Greene 5th p714
¡´ The question is the convergence is very slow compare with Stata using
likellihood only.
¡´ Second i did the similar way in matlab using
fminsearch , the estimated value is close to true value, however, R gave
wrong result, far away the true value.
Is there any way to correct the estimation in R and speed up
the algorithm.
y1 is the selection equation
y2 is the probit equation
Thanks much
Codes:
require(mvtnorm)
obs=1000
sig=matrix(c(1,0.7,0.7,1),2,2)
mu=c(0,0)
e = rmvnorm(obs,mu,sig);
x1 = rnorm(obs);x2=rnorm(obs);x3=rnorm(obs);
y1 = as.integer(1+2*x1-2*x2+e[,1]>0)
y2 = as.integer(0.2-2*x2+x3+e[,2]>0)
x1 = cbind(1,x1,x2);
x2 = cbind(1,x2,x3)
iprobit<-function(y,x){
bstart = solve(qr(x),y)
b=optim(bstart,probll,y=y,x=x,hessian=F)$par
return(b)
}
probll<-function(bstart,y,x){
prob=pnorm(x%*%bstart);
ll= -sum(y*log(prob)+(1-y)*log(1-prob));
return(ll)
}
heckprob<-function(y1,x1,y2,x2){
bstart = as.vector(c(iprobit(y1,x1),iprobit(y2,x2),0.5))
b=optim(bstart,heckpll,y1=y1,x1=x1,y2=y2,x2=x2,hessian=F)$par
return(b)
}
heckpll<-function(bstart,y1,x1,y2,x2){
y1=as.vector(y1);y2=as.vector(y2);
x1=as.matrix(x1);x2=as.matrix(x2);
p=ncol(x1);
k=ncol(x2);
obs = length(y1);
ll = numeric(obs);
b1 =bstart[1:p];
b2 =bstart[(p+1):(k+p)];
rho=(exp(bstart[(k+p+1)]) - 1)/(exp(bstart[(k+p+1)]) - 1);
xb1 = x1%*%b1;
xb2 = x2%*%b2;
prob = pnorm(xb1);
for (i in 1:obs){
ll[i] = (1-y1[i])*log(1-prob[i])+
y1[i]*(1-y2[i])*log(pmvnorm(lower-Inf,upper=c(-xb1[i],xb2[i]),corr=matrix(c(1,-rho,-rho,1),2,2)))+
y1[i]*y2[i]*log(pmvnorm(lower=-Inf,upper=c(xb1[i],xb2[i]),corr=matrix(c(1,rho,rho,1),2,2)))
}
return(-sum(ll));
}
b=heckprob(y1,x1,y2,x2)
[[alternative HTML version deleted]]