program define example2 version 2.1 * data-resampling regression bootstrap * assumes variables "Y" and "X" in "source.dta" * set more 1 drop _all set maxobs 2000 * If source.dta contains > 2,000 cases, set maxobs higher. quietly use source.dta quietly drop if Y==. | X==. save, replace quietly regress Y X macro define _coefX=_b[X] * _coefX equals the original-sample regression coefficient on X capture erase bootdat2.log log using bootdat2.log log off set seed 1111 macro define _bsample 1 while %_bsample<1001 { * For confidence intervals or tests, we need 2000 or more * bootstrap samples. quietly use source.dta, clear generate randnum=int(_N*uniform())+1 quietly generate YY=Y[randnum] quietly generate XX=X[randnum] quietly regress YY XX * The last three commands randomly resample (X,Y) pairs * from the data. macro define _bSE=_b[XX]/sqrt(_result(6)) * Stata version 3.0 or later: omit previous command. log on display %_bsample display _b[_cons] display _b[XX] display %_bSE * Version 3.0 or later: replace previous command with * display _se[XX] display (_b[XX]-%_coefX)/%_bSE * Version 3.0 or later: replace previous command with * display (_b[XX]-%_coefX)/_se[XX] * Calculated either way, this command obtains a * studentized coefficient: * (bootstrap coef. - original coef.)/SE of bootstrap coef. display log off macro define _bsample=%_bsample+1 } log close drop _all infile bsample bcons bcoefX bSE stucoefX using bootdat2.log label variable bsample "bootstrap sample number" label variable bcons "sample Y-intercept, b0" label variable bcoefX "sample coefficient on X, b1" label variable bSE "sample standard error of b1" label variable stucoefX "studentized coefficient on X" label data "regression boot/data resampling" save boot2.dta, replace end