********************************************************************************
*  Empirical Exercise 6.2 - Growth and Trade: 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 "Growth.csv", clear

* Exclude Malta (influential outlier identified in E4.1)
drop if country_name == "Malta"

********************************************************************************
* DATA OVERVIEW
********************************************************************************
* Source: Cross-country growth data, 1960-1995
* Sample: 64 countries (Malta excluded)
*
* Variables:
*   growth: Average annual GDP growth rate (%)
*   tradeshare: (Exports + Imports) / GDP
*   yearsschool: Average years of schooling
*   rev_coups: Number of revolutions and coups (annual average)
*   assasinations: Political assassinations per million population
*   rgdp60: Real GDP per capita in 1960 (initial income level)
*   oil: 1 if country is oil exporter (NOTE: All zeros in this sample)

describe growth tradeshare yearsschool rev_coups assasinations rgdp60 oil

********************************************************************************
* (a) SUMMARY STATISTICS
********************************************************************************
* Construct table with mean, SD, min, max for all variables

summarize growth tradeshare yearsschool rev_coups assasinations rgdp60 oil

* More detailed format
tabstat growth tradeshare yearsschool rev_coups assasinations rgdp60, ///
    statistics(mean sd min max) columns(statistics) format(%9.3f)

* Store means and SDs for predictions
foreach var in growth tradeshare yearsschool rev_coups assasinations rgdp60 {
    quietly summarize `var'
    scalar mean_`var' = r(mean)
    scalar sd_`var' = r(sd)
}

********************************************************************************
* (b) MULTIPLE REGRESSION
********************************************************************************
* Model: Growth_i = β₀ + β₁ TradeShare_i + β₂ YearsSchool_i + β₃ Rev_Coups_i
*                     + β₄ Assassinations_i + β₅ RGDP60_i + u_i
*
* This is a cross-country growth regression (Barro-style)
* Controls for human capital, political instability, and initial income

regress growth tradeshare yearsschool rev_coups assasinations rgdp60

* Store key coefficient
scalar b_revcoups = _b[rev_coups]
scalar b_trade = _b[tradeshare]

* Interpretation of Rev_Coups coefficient:
* β̂_revcoups gives the effect of a 1-unit increase in coups on growth
* holding all other variables constant
*
* Economic significance: Compare effect of 1 SD change
* Effect = β̂ × SD(rev_coups)

********************************************************************************
* (c) PREDICTION AT AVERAGE VALUES
********************************************************************************
* Predict growth for a country with average values for all regressors
*
* Key property of OLS: The regression line passes through the point of means
* Ŷ at (X̄₁, X̄₂, ..., X̄ₖ) = Ȳ
*
* This is because: Ȳ = β̂₀ + β̂₁X̄₁ + ... + β̂ₖX̄ₖ  (OLS normal equations)

margins, at((mean) tradeshare (mean) yearsschool (mean) rev_coups ///
            (mean) assasinations (mean) rgdp60)

* The predicted value should equal the sample mean of growth

********************************************************************************
* (d) PREDICTION: TRADESHARE ONE SD ABOVE MEAN
********************************************************************************
* Now predict for a country with TradeShare = mean + 1 SD
* All other variables remain at their means
*
* Change in predicted growth = β̂_tradeshare × SD(tradeshare)

margins, at(tradeshare=`=mean_tradeshare + sd_tradeshare' ///
            (mean) yearsschool (mean) rev_coups ///
            (mean) assasinations (mean) rgdp60)

* Verify manually:
* Δŷ = β̂_trade × Δtradeshare = β̂_trade × sd_tradeshare

********************************************************************************
* (e) WHY IS OIL OMITTED?
********************************************************************************
* Check the Oil variable

tab oil
summarize oil

* All observations have Oil = 0
* No variation in Oil → Cannot estimate its effect
*
* Technical reason:
* In OLS, we need Var(X) > 0 to estimate β
* If Var(X) = 0, then X'X is singular (not invertible)
* β̂ = (X'X)⁻¹X'Y is undefined
*
* What happens if we try to include it?

regress growth tradeshare yearsschool rev_coups assasinations rgdp60 oil

* Stata will automatically drop Oil due to collinearity

********************************************************************************
* END OF E6.2
********************************************************************************