*. simpsel1 pdv, sel() exog() very() /*Observed data are n observations of p,d,y,z,v,x; The model is p = (yB + e)d, E(ez)=0, d = I(0 < v + M* < A*), v = xA + eta p is the observed outcome, is zero when d=0. d is the discrete selection indicator v is the very exogenous regressor. y is varlist in sel() z is varlist in exog(), instruments for p model x is varlist in very(), regressors in d and v models. e = outcome error, M* and A* are latent variables (A* can be infinity) M*, e, and X can be related in unknown ways, but are all independent of eta. eta = v model error*/ cap pro drop simpsel1 pro def simpsel1, eclass local varlist "required existing max(3) min(3)" local if "optional " local options "sel(string) exog(string) very( string)" parse "`*'" preserve tokenize "`varlist'" di in g "Regression of `3' on `very'" reg `3' `very' tempvar eta sigma f w ww predict `eta' if e(sample), resid gen `sigma'=sqrt(e(rss)/(_N)) gen `f'=normden(`eta', 0, `sigma') /* assume v model error is normal, estimate the density*/ zrnd `f' /* round the near zero density away from zero*/ gen `w'=`2'/`f' /* generate weight*/ qui sum `w' gen `ww'=`w'/r(mean) /* normalize weight*/ di " " di in g "Based on conditional normal density of `v', sample selection is corrected." di " " di in g "Structure model parameters are estimated by 2SLS." ivreg `1' (`sel'=`exog') [iw=`ww'] di in g "Note the Std. Err. and Conf. Interval are wrong! " di " " di in g "Bootstrap command can be used to get correct Std Err. and Conf. Interval." di " " di in y `"Try it, :)) - bs "simpsel1 varlist" _b, size() level() rep() saving() replace"' end