program define example1 version 2.1 * The first line tells Stata we are going to define a program * named "example1". This program bootstraps the mean of a * variable named "X", from a dataset called "source.dta". * To apply example1.ado to your own data: * * . use * . rename X * . keep if X~=. * . save source, replace * set more 1 * Tells Stata to wait only 1 second before scrolling a full * screen. Default: waits for keyboard input before scrolling. drop _all capture erase bootdat1.log set maxobs 2000 * For confidence intervals or other applications using * bootstrap-distribution tail percentiles, at least B=2,000 * bootstrap iterations are needed. Simpler purposes, including * standard error estimation, require substantially fewer * iterations. * If source.dta contains > 2,000 cases, set maxobs higher. log using bootdat1.log log off * Log file bootdat1.log will record bootstrap results. set seed 1111 * Sets the random-generator seed. We can repeat the random * sequence later by using the same seed, or avoid repeating it * by choosing a different seed (any large odd number). macro define _bsample 1 * _bsample counts the number of bootstrap samples. * _bsample is the name of this macro; %_bsample refers to * the macro's current contents: while %_bsample<1001 { quietly use source.dta, clear quietly drop if X==. quietly generate XX=X[int(_N*uniform())+1] * Variable XX holds randomly resampled X values. The * expression int(_N*uniform())+1 generates random integers * from 1 through _N (sample size). quietly summarize XX log on display %_bsample display _result(3) display log off * For each bootstrap sample, the log file contains the * sample number and mean of XX. macro define _bsample=%_bsample+1 } * Curly brackets enclose "while %_bsample<1001" loop. log close drop _all infile bsample bmean using bootdat1.log label variable bsample "bootstrap sample number" label variable bmean "sample mean of X" label data "bootstrap mean" save boot1.dta, replace * Final steps read the log file "bootdat1.log", label * variables, and save dataset boot1.dta containing means from * 1,000 bootstrap resamplings of the original source.dta data. end