|
刚刚编写了一个heckman two step的教程,参见下面代码:
/* how to construct Heckman two step estimator*/
/* step 1: construct the primary equation: y=b0+b1*x1+b2*x2+u;*/
/* in order to avoid endogeneity, assume x1, x2 and u are IID Normal*/
clear
set obs 1000
gen x1=rnormal()
gen x2=rnormal()
/*for comparision purpose, assume b0=3,b1=0.5,b2=2*/
scalar b0=3
scalar b1=0.5
scalar b2=2
/*error term is from normal distribution*/
gen u=rnormal()
/*get simulated values of y*/
gen y=b0+b1*x1+b2*x2+u
/* step2: construct the selection function: dstar=c0+c1*x1+c2*x2+c3*z+v */
gen z=rnormal()
gen v=rnormal()
scalar c0=0.4
scalar c1=0.2
scalar c2=0.3
scalar c3=0.4
gen dstar=c0+c1*x1+c2*x2+c3*z+v
/* d=1 if dstar>0 and 0 otherwise*/
gen d=dstar>=0
replace y=. if d==0
/*step 3: do probit regression to get normalpdf(dhat)/normalcdf(dhat), or n1/n2*/
probit d x1 x2 z
predict dhat,xb
gen n1=normalden(dhat)
gen n2=normal(dhat)
gen gamma=n1/n2
/*step 4: put gamma into primary equation, and then do OLS;compare with no correction term*/
reg y x1 x2 gamma
reg y x1 x2
/*step 5: use STATA Package to compare the results*/
heckman y x1 x2, twostep select(d= x1 x2 z)
reg y x1 x2 gamma
|