* File-Name: Simulations.do * Date: January 23, 2009 * Author: Alex Herzog * Purpose: Use simulation to investigate the sample properties of the OLS estimator * Data Used: - * Output File: Simulations.log * Data Output: - * Machine: Alex's MacBook * Note: This is the same do-file as used in section on January 23, 2009 clear version 10.1 set more off capture log close program drop _all * Log file log using week1.log, replace * ================== * = RANDOM NUMBERS = * ================== set obs 1000 gen unif = runiform() gen norm01 = rnormal() gen norm52 = rnormal(5,2) gen bin = rbinomial(1,.5) gen chi2 = rchi2(1) sum unif norm01 norm52 bin chi2 histogram unif, title("U(0,1)") nodraw saving(unif, replace) histogram norm01, normal title("N(0,1)") nodraw saving(norm01, replace) histogram norm52, normal title("N(5,2)") nodraw saving(norm52, replace) histogram bin , title("B(1,5)") nodraw saving(bin, replace) histogram chi2, title("Chi2(1)") nodraw saving(chi2, replace) graph combine unif.gph norm01.gph norm52.gph bin.gph chi2.gph * ============================ * = OLS simulation - fixed x = * ============================ * Define global macros for sample size and number of simulations global numobs 150 // sample size N global numsims 1000 // number of simulations clear set obs $numobs set seed 10101 gen x = runiform() program define simulation1 version 10.1 * this keeps x fixed keep x * generate the error term gen e = rnormal() * generate the dependent variable gen y = 1 + 2*x + e * and regress regress y x end * Now do the simulation. Note that the simulation has built-in commands for estimates and se. * Clearly, "reps" is the number of replications, whereas "obs" is the number of observations * created in each replication. There are lots of options: type "help simulate". set seed 10101 simulate _b _se, reps($numsims): simulation1 sum _b_cons _se_cons _b_x _se_x mean _b_cons _se_cons _b_x _se_x * Produce a histogram and add a normal and a kernel density estimator hist _b_x, normal kdensity * ================================= * = OLS simulation - stochastic x = * ================================= * Here's the key to the stochastic x: we drop all variables and error terms create new ones program define simulation2 version 10.1 * drop all variables drop _all * set the number of obs using the globaly defined sample size from above set obs $numobs * generate new x gen x = runiform() * generate the error term gen e = rnormal() * generate the dependent variable gen y = 1 + 2*x + e * and regress regress y x end * Now do the simulation set seed 10101 simulate _b _se, reps($numsims): simulation2 sum _b_cons _se_cons _b_x _se_x mean _b_cons _se_cons _b_x _se_x * Produce a histogram and add a normal and a kernel density estimator hist _b_x, normal kdensity log close exit