*. simpsel2 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 simpsel2 pro def simpsel2, 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 w ww predict `eta' if e(sample), resid qui sortedf1 `eta' /* get sorted data density of V model error*/ local f `eta' zrnd `f' /* round 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 sorted data density of `3', 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 "simpsel2 varlist" _b, size() level() rep() saving() replace"' end