clear
set more off
*set our desired number of observations
set obs 20
*Declare a local scalar variable that will represent the number of draws from our desired distribution
local n = 20
*set seed so that we can recreate our results
set seed 12412312
*Draw n random variables from a normal distribution with mean=3 and variance=1
forvalues i=1/`n' {
gen X`i' = rnormal(3, 1)
}
*Create a placeholder for the first observation in each Xi for i = 1, 2, ..., n
gen x1 = .
forvalues i=1/`n' {
replace x1 = X`i'[1] in `i'
}
*Generate the means of each Xi for i = 1,2, ..., n and create a placeholder for these values, called x_bar
gen x_bar = .
forvalues i=1/`n' {
egen X`i'_bar = mean(X`i')
replace x_bar = X`i'_bar[1] in `i'
drop X`i'_bar
}
*Let's look at the means and range of both x1 and x_bar
sum x1 x_bar
/*
The averages are both close to the mean of the distribution...illustrating the unbiasedness
of both estimators (and we could get these averages closer to 3 by doing more than 20 reps).
But, we see that the values for x1 are much more spread out than those for x_bar...x_bar is
clearly better than x1 as an estimator of mu.
*/