********************************************************************************
*  Empirical Exercise 7.1 - Birthweight and Smoking: Hypothesis Testing
*  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
*
* Chapter 7 Focus: Hypothesis Testing and Confidence Intervals in Multiple Regression
*
* Key Variables for this exercise:
*   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
*   unmarried: 1 if mother is unmarried
*   age: Mother's age (years)
*   educ: Mother's education (years)

describe birthweight smoker alcohol nprevist unmarried age educ
summarize birthweight smoker alcohol nprevist unmarried age educ

********************************************************************************
* THREE BASE REGRESSIONS (as specified in the question)
********************************************************************************
* (1) Birthweight on Smoker
* (2) Birthweight on Smoker, Alcohol, Nprevist
* (3) Birthweight on Smoker, Alcohol, Nprevist, Unmarried

*------------------------------------------------------------------------------
* REGRESSION (1): Simple regression
*------------------------------------------------------------------------------
regress birthweight smoker

* Store results
scalar b_smoker_1 = _b[smoker]
scalar se_smoker_1 = _se[smoker]

*------------------------------------------------------------------------------
* REGRESSION (2): Adding Alcohol and Nprevist
*------------------------------------------------------------------------------
regress birthweight smoker alcohol nprevist

* Store results
scalar b_smoker_2 = _b[smoker]
scalar se_smoker_2 = _se[smoker]

*------------------------------------------------------------------------------
* REGRESSION (3): Adding Unmarried
*------------------------------------------------------------------------------
regress birthweight smoker alcohol nprevist unmarried

* Store results
scalar b_smoker_3 = _b[smoker]
scalar se_smoker_3 = _se[smoker]
scalar b_unmarried = _b[unmarried]
scalar se_unmarried = _se[unmarried]

********************************************************************************
* (a) ESTIMATED EFFECT OF SMOKING IN EACH REGRESSION
********************************************************************************
* The coefficient on Smoker represents the estimated effect of smoking
* on birthweight, holding other included variables constant

********************************************************************************
* (b) 95% CONFIDENCE INTERVALS FOR SMOKING EFFECT
********************************************************************************
* Formula: β̂ ± 1.96 × SE(β̂)  for large samples
* Or use Stata's built-in CI from regression output

* Manual calculation for each regression:
* CI for Regression (1)
scalar ci_lo_1 = b_smoker_1 - 1.96 * se_smoker_1
scalar ci_hi_1 = b_smoker_1 + 1.96 * se_smoker_1

* CI for Regression (2)
scalar ci_lo_2 = b_smoker_2 - 1.96 * se_smoker_2
scalar ci_hi_2 = b_smoker_2 + 1.96 * se_smoker_2

* CI for Regression (3)
scalar ci_lo_3 = b_smoker_3 - 1.96 * se_smoker_3
scalar ci_hi_3 = b_smoker_3 + 1.96 * se_smoker_3

********************************************************************************
* (c) & (d) OMITTED VARIABLE BIAS ANALYSIS
********************************************************************************
* OVB occurs when:
*   (1) Omitted variable is correlated with included regressor (Smoker)
*   (2) Omitted variable is a determinant of Y (Birthweight)
*
* Check correlations between Smoker and potential confounders

correlate smoker alcohol nprevist unmarried

********************************************************************************
* (e) ANALYSIS OF UNMARRIED COEFFICIENT
********************************************************************************
* From Regression (3):

* (e)(i) 95% Confidence Interval for Unmarried coefficient
scalar ci_lo_unmar = b_unmarried - 1.96 * se_unmarried
scalar ci_hi_unmar = b_unmarried + 1.96 * se_unmarried

* (e)(ii) Statistical significance: Check if CI includes 0 or |t| > 1.96
scalar t_unmarried = b_unmarried / se_unmarried

* (e)(iii) & (e)(iv) Economic interpretation
* Note: Large coefficient does NOT imply causal effect
* Unmarried may proxy for other factors (income, support, stress, etc.)

********************************************************************************
* (f) ROBUSTNESS ANALYSIS - Adding More Controls
********************************************************************************
* Examine how the smoking coefficient changes with additional controls
* This follows the Table 7.1 approach in the textbook

* Regression (4): Add Age and Education
regress birthweight smoker alcohol nprevist unmarried age educ

scalar b_smoker_4 = _b[smoker]
scalar se_smoker_4 = _se[smoker]
scalar ci_lo_4 = b_smoker_4 - 1.96 * se_smoker_4
scalar ci_hi_4 = b_smoker_4 + 1.96 * se_smoker_4

* Regression (5): Use Young indicator instead of continuous Age
gen young = (age <= 20)
label variable young "Mother age <= 20"

regress birthweight smoker alcohol nprevist unmarried young educ

scalar b_smoker_5 = _b[smoker]
scalar se_smoker_5 = _se[smoker]
scalar ci_lo_5 = b_smoker_5 - 1.96 * se_smoker_5
scalar ci_hi_5 = b_smoker_5 + 1.96 * se_smoker_5

* Regression (6): Include all available controls
regress birthweight smoker alcohol nprevist unmarried age educ tripre0 tripre2 tripre3

scalar b_smoker_6 = _b[smoker]
scalar se_smoker_6 = _se[smoker]
scalar ci_lo_6 = b_smoker_6 - 1.96 * se_smoker_6
scalar ci_hi_6 = b_smoker_6 + 1.96 * se_smoker_6

********************************************************************************
* SUMMARY TABLE: Robustness of Smoking Effect
********************************************************************************
* Display results in Table 7.1 style

di ""
di "=============================================================================="
di "ROBUSTNESS OF SMOKING EFFECT ON BIRTHWEIGHT"
di "=============================================================================="
di "Regression |  β̂_smoker  |    SE     |    95% CI          | Controls"
di "-----------|------------|-----------|--------------------|-----------------"
di "   (1)     | " %9.2f b_smoker_1 " | " %7.2f se_smoker_1 " | (" %7.1f ci_lo_1 ", " %6.1f ci_hi_1 ") | None"
di "   (2)     | " %9.2f b_smoker_2 " | " %7.2f se_smoker_2 " | (" %7.1f ci_lo_2 ", " %6.1f ci_hi_2 ") | Alc, Nprevist"
di "   (3)     | " %9.2f b_smoker_3 " | " %7.2f se_smoker_3 " | (" %7.1f ci_lo_3 ", " %6.1f ci_hi_3 ") | + Unmarried"
di "   (4)     | " %9.2f b_smoker_4 " | " %7.2f se_smoker_4 " | (" %7.1f ci_lo_4 ", " %6.1f ci_hi_4 ") | + Age, Educ"
di "   (5)     | " %9.2f b_smoker_5 " | " %7.2f se_smoker_5 " | (" %7.1f ci_lo_5 ", " %6.1f ci_hi_5 ") | + Young, Educ"
di "   (6)     | " %9.2f b_smoker_6 " | " %7.2f se_smoker_6 " | (" %7.1f ci_lo_6 ", " %6.1f ci_hi_6 ") | + Tripre dummies"
di "=============================================================================="
di ""
di "Unmarried coefficient in Reg (3): " %9.2f b_unmarried " (SE = " %6.2f se_unmarried ")"
di "  95% CI: (" %7.1f ci_lo_unmar ", " %6.1f ci_hi_unmar ")"
di "  t-stat: " %6.2f t_unmarried

********************************************************************************
* END OF E7.1
********************************************************************************