********************************************************************************
*  Empirical Exercise 6.1 - Birthweight and Smoking: Multiple Regression
*  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 your working directory
* Set the working directory to the folder that holds the data before running,
* for example:  cd "path/to/data"

import delimited "Birthweight_Smoking.csv", clear

********************************************************************************
* DATA OVERVIEW
********************************************************************************
* Source: 1989 National Natality-Mortality Detail files (Pennsylvania)
* Sample: 3,000 singleton births
*
* Key Variables:
*   birthweight: Birth weight (grams)
*   smoker: 1 if mother smoked during pregnancy
*   alcohol: 1 if mother drank alcohol during pregnancy
*   nprevist: Number of prenatal care visits
*   tripre0/1/2/3: 1 if prenatal care began in trimester 0/1/2/3
*                  (tripre0 = no prenatal care)

describe birthweight smoker alcohol nprevist tripre0 tripre1 tripre2 tripre3
summarize birthweight smoker alcohol nprevist

********************************************************************************
* (a) SIMPLE REGRESSION: Birthweight on Smoker
********************************************************************************
* Model: Birthweight_i = β₀ + β₁ Smoker_i + u_i
*
* Question: What is the estimated effect of smoking on birth weight?
* This is a bivariate regression - may suffer from omitted variable bias

regress birthweight smoker

* Store coefficient for later comparison
scalar b_smoker_simple = _b[smoker]

********************************************************************************
* (b) MULTIPLE REGRESSION: Birthweight on Smoker, Alcohol, Nprevist
********************************************************************************
* Model: Birthweight_i = β₀ + β₁ Smoker_i + β₂ Alcohol_i + β₃ Nprevist_i + u_i
*
* KEY CONCEPT 6.1 - Omitted Variable Bias:
* OVB occurs when:
*   (1) The omitted variable is correlated with included regressor
*   (2) The omitted variable is a determinant of Y
*
* If both conditions hold → β̂₁ from simple regression is biased

regress birthweight smoker alcohol nprevist

* Store coefficients
scalar b_smoker_multi = _b[smoker]
scalar b_alcohol = _b[alcohol]
scalar b_nprevist = _b[nprevist]
scalar b_cons = _b[_cons]

* (b)(i) Check OVB conditions: Are Alcohol and Nprevist correlated with Smoker?
correlate smoker alcohol nprevist

* (b)(iii) Prediction for Jane: Smoker=1, Alcohol=0, Nprevist=8
* Ŷ = β̂₀ + β̂₁(1) + β̂₂(0) + β̂₃(8)
scalar jane_pred = b_cons + b_smoker_multi*1 + b_alcohol*0 + b_nprevist*8

* Alternative: Use margins command
margins, at(smoker=1 alcohol=0 nprevist=8)

********************************************************************************
* (c) FRISCH-WAUGH THEOREM (Appendix 6.3)
********************************************************************************
* Theorem: The coefficient on X₁ in a multiple regression of Y on X₁, X₂, X₃
*          equals the coefficient from regressing resid(Y|X₂,X₃) on resid(X₁|X₂,X₃)
*
* Intuition: The FW residuals "partial out" the effect of other regressors,
*            isolating the unique variation in X₁ not explained by X₂, X₃
*
* Three-step process to get β̂_smoker:

* Step 1: Regress Smoker on the other X's → get residuals
quietly regress smoker alcohol nprevist
predict smoker_resid, residuals

* Step 2: Regress Birthweight on the other X's → get residuals
quietly regress birthweight alcohol nprevist
predict bw_resid, residuals

* Step 3: Regress Y residuals on X₁ residuals (no constant needed)
regress bw_resid smoker_resid, noconstant

* Verify: This coefficient should equal β̂_smoker from part (b)

********************************************************************************
* (d) REGRESSION WITH TRIMESTER DUMMIES
********************************************************************************
* Replace continuous Nprevist with categorical timing of first prenatal visit
* Model: Birthweight = β₀ + β₁ Smoker + β₂ Alcohol + β₃ Tripre0 + β₄ Tripre2 + β₅ Tripre3 + u
*
* DUMMY VARIABLE TRAP (Key Concept 6.3):
* We cannot include all four dummies (Tripre0, Tripre1, Tripre2, Tripre3)
* because Tripre0 + Tripre1 + Tripre2 + Tripre3 = 1 for all observations
* This would create perfect multicollinearity with the constant term
*
* Solution: Omit one category as the "reference group"
* Here: Tripre1 (first trimester) is the reference

* First check the dummy variables sum to 1
gen tripre_sum = tripre0 + tripre1 + tripre2 + tripre3
tab tripre_sum

regress birthweight smoker alcohol tripre0 tripre2 tripre3

* (d)(i)  Tripre1 excluded to avoid perfect multicollinearity
* (d)(ii) β̂_tripre0: Effect of NO prenatal care vs starting in 1st trimester
* (d)(iii) β̂_tripre2, β̂_tripre3: Effect of starting care later vs 1st trimester
* (d)(iv) Compare R² to model (b)

********************************************************************************
* END OF E6.1
********************************************************************************