********************************************************************************
*  Empirical Exercise 4.2 - Earnings and Height
*  Stock & Watson - Introduction to Econometrics (4th Edition)
*
*  Code: Fatih Kansoy - www.fatih.ai
*  Course: Financial Econometrics, SJTU Summer School 2025
********************************************************************************

clear all
set more off

* Set the working directory to the folder that holds the data before running,
* for example:  cd "path/to/data"

* Load data
import delimited "Earnings_and_Height.csv", clear

* Quick data inspection
describe
summarize

********************************************************************************
* DATA OVERVIEW
********************************************************************************
* Dataset: Random sample of U.S. workers
* Key variables:
*   - earnings: Annual earnings ($)
*   - height: Height in inches
*   - sex: 0 = Female, 1 = Male
*
* Note: Earnings are reported in 23 brackets (not continuous values)
*       Each bracket is assigned a single average value

* Check the distribution of earnings values
tab earnings, sort
distinct earnings

********************************************************************************
* (a) MEDIAN HEIGHT
********************************************************************************

* Detailed summary statistics for height
summarize height, detail

* Store median for later use
local median_height = r(p50)
di "Median height in the sample: " `median_height' " inches"

********************************************************************************
* (b) AVERAGE EARNINGS BY HEIGHT GROUP
********************************************************************************

* Create indicator for "tall" workers (height > 67 inches)
gen tall = (height > 67)
label define tall_lbl 0 "Short (≤67 in)" 1 "Tall (>67 in)"
label values tall tall_lbl

* (i) Average earnings for shorter workers (height ≤ 67)
di "=============================================="
di "(b.i) Workers with Height ≤ 67 inches"
di "=============================================="
ci means earnings if height <= 67

* (ii) Average earnings for taller workers (height > 67)
di "=============================================="
di "(b.ii) Workers with Height > 67 inches"
di "=============================================="
ci means earnings if height > 67

* (iii) Difference with 95% CI using two-sample t-test
di "=============================================="
di "(b.iii) Difference: Tall - Short"
di "=============================================="
ttest earnings, by(tall) unequal

* Summary table
tabstat earnings, by(tall) stat(mean sd n) format(%12.0fc)

********************************************************************************
* (c) SCATTERPLOT: Earnings vs Height
********************************************************************************

* Basic scatterplot
scatter earnings height, ///
	title("Annual Earnings vs Height") ///
	xtitle("Height (inches)") ///
	ytitle("Annual Earnings ($)") ///
	mcolor(navy%30) msize(small) ///
	note("Source: Earnings and Height dataset.")
graph export "fig1_earnings_height_scatter.png", replace width(1200)

* Note: Points fall along horizontal lines because earnings are reported
* in 23 discrete brackets, not as continuous values

* Scatterplot with fitted regression line
twoway (scatter earnings height, mcolor(navy%30) msize(small)) ///
       (lfit earnings height, lcolor(red) lwidth(medium)), ///
       title("Annual Earnings vs Height") ///
       subtitle("With OLS Fitted Line") ///
       xtitle("Height (inches)") ///
       ytitle("Annual Earnings ($)") ///
       legend(order(1 "Data" 2 "Fitted Line") position(5) ring(0)) ///
       note("Horizontal bands reflect 23 discrete earnings brackets in the data.")
graph export "fig2_earnings_height_fitted.png", replace width(1200)

********************************************************************************
* (d) REGRESSION: Earnings on Height (All Workers)
********************************************************************************

* OLS regression
* Model: Earnings_i = β₀ + β₁ × Height_i + u_i
regress earnings height

* Store coefficients
local b0 = _b[_cons]
local b1 = _b[height]
local r2 = e(r2)
local rmse = e(rmse)

* (i) Estimated slope
di "=============================================="
di "REGRESSION RESULTS: All Workers"
di "=============================================="
di "Estimated equation: Earnings = " %10.1f `b0' " + " %6.1f `b1' " × Height"
di ""
di "Slope interpretation: Each additional inch of height is associated"
di "                      with $" %6.1f `b1' " higher annual earnings."
di ""
di "R² = " %5.3f `r2' " (Height explains " %3.1f `r2'*100 "% of variation in earnings)"
di "SER = $" %8.0f `rmse'

* (ii) Predictions for specific heights
local pred_65 = `b0' + `b1' * 65
local pred_67 = `b0' + `b1' * 67
local pred_70 = `b0' + `b1' * 70

di ""
di "Predicted Earnings:"
di "  Height = 65 inches: $" %8.0f `pred_65'
di "  Height = 67 inches: $" %8.0f `pred_67'
di "  Height = 70 inches: $" %8.0f `pred_70'

********************************************************************************
* (e) REGRESSION WITH HEIGHT IN CENTIMETERS
********************************************************************************

* Unit conversion: 1 inch = 2.54 cm
* Create height in centimeters
gen height_cm = height * 2.54

* Run regression with height in cm
regress earnings height_cm

* Store results
local b0_cm = _b[_cons]
local b1_cm = _b[height_cm]
local r2_cm = e(r2)
local rmse_cm = e(rmse)

di "=============================================="
di "REGRESSION WITH HEIGHT IN CENTIMETERS"
di "=============================================="
di "(i)   Slope:     " %8.2f `b1_cm' " ($/cm)"
di "(ii)  Intercept: " %8.2f `b0_cm' " ($)"
di "(iii) R²:        " %8.4f `r2_cm'
di "(iv)  SER:       " %8.0f `rmse_cm' " ($)"
di ""
di "Note: R² and SER are unchanged by unit conversion."
di "      Slope in $/inch = " %6.1f `b1' " = " %6.1f `b1_cm' " × 2.54"

********************************************************************************
* (f) REGRESSION: Females Only
********************************************************************************

* Note: sex = 0 for females
regress earnings height if sex == 0

* Store results
local b0_f = _b[_cons]
local b1_f = _b[height]
local r2_f = e(r2)

* Average height for females
summarize height if sex == 0, meanonly
local mean_height_f = r(mean)

di "=============================================="
di "REGRESSION: Female Workers Only"
di "=============================================="
di "Estimated equation: Earnings = " %10.1f `b0_f' " + " %6.1f `b1_f' " × Height"
di ""
di "(i) Slope: $" %6.1f `b1_f' " per inch"
di ""
di "(ii) A woman 1 inch taller than average:"
di "     Average female height = " %4.1f `mean_height_f' " inches"
di "     Predicted earnings increase = $" %6.1f `b1_f' " per year"
di ""
di "R² = " %5.4f `r2_f'

* Count female observations
count if sex == 0
local n_female = r(N)
di "Sample size (females): " `n_female'

********************************************************************************
* (g) REGRESSION: Males Only
********************************************************************************

* Note: sex = 1 for males
regress earnings height if sex == 1

* Store results
local b0_m = _b[_cons]
local b1_m = _b[height]
local r2_m = e(r2)

* Average height for males
summarize height if sex == 1, meanonly
local mean_height_m = r(mean)

di "=============================================="
di "REGRESSION: Male Workers Only"
di "=============================================="
di "Estimated equation: Earnings = " %10.1f `b0_m' " + " %6.1f `b1_m' " × Height"
di ""
di "(i) Slope: $" %6.1f `b1_m' " per inch"
di ""
di "     Average male height = " %4.1f `mean_height_m' " inches"
di "     Predicted earnings increase = $" %6.1f `b1_m' " per year"
di ""
di "R² = " %5.4f `r2_m'

* Count male observations
count if sex == 1
local n_male = r(N)
di "Sample size (males): " `n_male'

********************************************************************************
* COMPARISON TABLE: All vs Female vs Male
********************************************************************************

di ""
di "=============================================="
di "COMPARISON: Regression by Gender"
di "=============================================="
di "                    All        Female      Male"
di "----------------------------------------------"
di "Intercept        " %8.1f `b0' "   " %8.1f `b0_f' "  " %8.1f `b0_m'
di "Slope ($/inch)   " %8.1f `b1' "   " %8.1f `b1_f' "  " %8.1f `b1_m'
di "R²               " %8.4f `r2' "   " %8.4f `r2_f' "  " %8.4f `r2_m'
di "=============================================="
di ""
di "Note: The height-earnings relationship is MUCH stronger for males"
di "      ($" %4.0f `b1_m' "/inch) than for females ($" %4.0f `b1_f' "/inch)."

********************************************************************************
* FIGURE: Regression Lines by Gender
********************************************************************************

twoway (scatter earnings height if sex == 0, mcolor(red%20) msize(vsmall)) ///
       (scatter earnings height if sex == 1, mcolor(blue%20) msize(vsmall)) ///
       (lfit earnings height if sex == 0, lcolor(red) lwidth(medium)) ///
       (lfit earnings height if sex == 1, lcolor(blue) lwidth(medium)), ///
       title("Earnings vs Height by Gender") ///
       xtitle("Height (inches)") ///
       ytitle("Annual Earnings ($)") ///
       legend(order(3 "Female" 4 "Male") position(5) ring(0)) ///
       note("Male slope ($1,307/inch) is more than double female slope ($511/inch).")
graph export "fig3_earnings_height_gender.png", replace width(1200)

********************************************************************************
* (h) DISCUSSION: Is Height Uncorrelated with Other Factors?
********************************************************************************

* Key question: Does E(u|Height) = 0?
*
* Height is likely correlated with other factors that affect earnings:
*   1. Gender: Men are taller on average AND earn more
*   2. Nutrition/Health: Better childhood nutrition → taller + better outcomes
*   3. Confidence/Social factors: Taller people may be perceived differently
*   4. Physical strength: May matter in some occupations
*
* Therefore, β̂₁ likely suffers from OMITTED VARIABLE BIAS
* The true causal effect of height on earnings is probably smaller than
* our estimate, because height is positively correlated with other
* earnings-enhancing factors.

********************************************************************************
* END OF DO FILE
********************************************************************************