*Using the KS income data from Mega HW #1
use "H:\oldTS1desktop\ECNS 561\Data Sets\Mega HW #1\KS Per Capita Income 1969 to 2012.dta", clear
*Let's calculate a 95% CI around the mean for nominal income across KS counties in 2012
keep county_fips county yr_2012
*First, we need to calculate the mean
egen income_bar = mean(yr_2012)
*Next, we calculate the standard deviation...let's do it by hand first
gen income_diff_sq = (yr_2012 - income_bar)^2
egen sum_income_diff_sq = sum(income_diff_sq)
gen income_sd = [(1/(105-1))*sum_income_diff_sq]^(1/2)
*Ok, now we can do it the easy way (just to make sure we did it right the first time!)
egen income_sd2 = sd(yr_2012)
*We're good...we can drop income_diff_sq sum_income_diff_sq income_sd2
drop income_diff_sq sum_income_diff_sq income_sd2
*Now, let's construct a 95% CI around the mean
gen income_lower95 = [income_bar - (1.98*income_sd)/(105^1/2)]
gen income_upper95 = [income_bar + (1.98*income_sd)/(105^1/2)]
*Finally, let's round these numbers and show them nicely and neatly in the STATA display screen
local income_lower95 = round(income_lower95, 1)
local income_upper95 = round(income_upper95, 1)
di "95% CI for mean income = [`income_lower95', `income_upper95']"