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

* Quick data inspection
describe
summarize

********************************************************************************
* DATA OVERVIEW
********************************************************************************
* Dataset: Cross-country growth data for 65 countries (1960-1995)
* Key variables:
*   - growth: Average annual GDP growth rate (%)
*   - tradeshare: (Exports + Imports) / GDP, averaged over 1960-1995
*   - country_name: Country identifier

* List countries with extreme trade shares (for outlier identification)
list country_name tradeshare growth if tradeshare > 1

********************************************************************************
* (a) SCATTERPLOT: Growth vs Trade Share
********************************************************************************

* Basic scatterplot to visualize relationship
scatter growth tradeshare, ///
	title("Economic Growth vs Trade Share (1960-1995)") ///
	xtitle("Trade Share (Exports + Imports)/GDP") ///
	ytitle("Average Annual Growth Rate (%)") ///
	mlabel(country_name) mlabsize(tiny) mlabposition(9) ///
	note("Source: Growth dataset. N = 65 countries.")
graph export "fig1_growth_tradeshare_scatter.png", replace width(1200)

* Cleaner version without labels (for presentation)
scatter growth tradeshare, ///
	title("Economic Growth vs Trade Share") ///
	xtitle("Trade Share") ///
	ytitle("Growth Rate (%)") ///
	mcolor(navy) ///
	note("Source: Growth dataset. N = 65 countries.")
graph export "fig1b_growth_tradeshare_clean.png", replace width(1200)

********************************************************************************
* (b) IDENTIFY MALTA AS OUTLIER
********************************************************************************

* Malta has trade share > 1.5 (actually around 2.0)
* This is unusual because trade share typically < 1 for most countries
list country_name tradeshare growth if tradeshare > 1.5

* Summary statistics to see Malta's position
tabstat tradeshare, stat(mean sd min max p50 p95)

********************************************************************************
* (c) REGRESSION: All Observations (Including Malta)
********************************************************************************

* OLS regression of Growth on TradeShare
* Model: Growth_i = β₀ + β₁ × TradeShare_i + u_i
regress growth tradeshare

* Store coefficients for predictions
local b0_all = _b[_cons]
local b1_all = _b[tradeshare]

* Predicted growth for specific trade share values
* Formula: Ŷ = β̂₀ + β̂₁ × X

* Prediction for TradeShare = 0.5
local pred_05_all = `b0_all' + `b1_all' * 0.5

* Prediction for TradeShare = 1.0
local pred_10_all = `b0_all' + `b1_all' * 1.0

* Display results
di "=============================================="
di "REGRESSION WITH ALL OBSERVATIONS (n=65)"
di "=============================================="
di "Estimated equation: Growth = " %5.2f `b0_all' " + " %5.2f `b1_all' " × TradeShare"
di ""
di "Predictions:"
di "  TradeShare = 0.5: Predicted Growth = " %5.2f `pred_05_all' "%"
di "  TradeShare = 1.0: Predicted Growth = " %5.2f `pred_10_all' "%"

********************************************************************************
* (d) REGRESSION: Excluding Malta
********************************************************************************

* Exclude Malta (country with tradeshare > 1.5)
regress growth tradeshare if country_name != "Malta"

* Store coefficients
local b0_excl = _b[_cons]
local b1_excl = _b[tradeshare]

* Predictions without Malta
local pred_05_excl = `b0_excl' + `b1_excl' * 0.5
local pred_10_excl = `b0_excl' + `b1_excl' * 1.0

* Display results
di "=============================================="
di "REGRESSION EXCLUDING MALTA (n=64)"
di "=============================================="
di "Estimated equation: Growth = " %5.2f `b0_excl' " + " %5.2f `b1_excl' " × TradeShare"
di ""
di "Predictions:"
di "  TradeShare = 0.5: Predicted Growth = " %5.2f `pred_05_excl' "%"
di "  TradeShare = 1.0: Predicted Growth = " %5.2f `pred_10_excl' "%"

********************************************************************************
* COMPARISON TABLE
********************************************************************************

di ""
di "=============================================="
di "COMPARISON: Effect of Excluding Malta"
di "=============================================="
di "                      With Malta    Without Malta"
di "----------------------------------------------"
di "Intercept (β̂₀)         " %6.2f `b0_all' "         " %6.2f `b0_excl'
di "Slope (β̂₁)             " %6.2f `b1_all' "         " %6.2f `b1_excl'
di "----------------------------------------------"
di "Pred. Growth (TS=0.5)  " %6.2f `pred_05_all' "         " %6.2f `pred_05_excl'
di "Pred. Growth (TS=1.0)  " %6.2f `pred_10_all' "         " %6.2f `pred_10_excl'
di "=============================================="

********************************************************************************
* (e) PLOT BOTH REGRESSION LINES
********************************************************************************

* Generate predicted values for both models
predict yhat_all, xb
regress growth tradeshare if country_name != "Malta"
predict yhat_excl if country_name != "Malta", xb

* Scatterplot with both fitted lines
twoway (scatter growth tradeshare, mcolor(navy%70)) ///
       (lfit growth tradeshare, lcolor(blue) lwidth(medium)) ///
       (lfit growth tradeshare if country_name != "Malta", ///
			lcolor(red) lwidth(medium) lpattern(dash)), ///
       title("Growth vs Trade Share: Impact of Malta") ///
       subtitle("Including vs Excluding Malta from Regression") ///
       xtitle("Trade Share") ///
       ytitle("Growth Rate (%)") ///
       legend(order(2 "With Malta (slope = 2.31)" ///
                    3 "Without Malta (slope = 1.68)") ///
              position(5) ring(0)) ///
       note("Malta (tradeshare ≈ 2.0) pulls the regression line steeper.")
graph export "fig2_regression_comparison.png", replace width(1200)

* Highlight Malta in the plot
twoway (scatter growth tradeshare if country_name != "Malta", mcolor(navy%70)) ///
       (scatter growth tradeshare if country_name == "Malta", ///
			mcolor(red) msize(large) msymbol(diamond)) ///
       (lfit growth tradeshare, lcolor(blue) lwidth(medium)) ///
       (lfit growth tradeshare if country_name != "Malta", ///
			lcolor(red) lwidth(medium) lpattern(dash)), ///
       title("Malta as an Influential Observation") ///
       xtitle("Trade Share") ///
       ytitle("Growth Rate (%)") ///
       legend(order(1 "Other Countries" 2 "Malta" ///
                    3 "Regression (all)" 4 "Regression (excl. Malta)") ///
              position(5) ring(0)) ///
       note("Malta's high trade share (≈2.0) gives it high leverage in OLS.")
graph export "fig3_malta_influence.png", replace width(1200)

********************************************************************************
* (f) DISCUSSION: Should Malta Be Included?
********************************************************************************

* Malta is a small island nation in the Mediterranean Sea (south of Sicily)
* Its high trade share (≈2.0) is due to its role as a freight transshipment hub
* Goods arrive (imports) and are immediately re-exported to other countries
* This makes Malta's "trade" fundamentally different from typical countries

* Key considerations:
* 1. Malta is an OUTLIER in X (high leverage point)
* 2. Its trade share reflects transshipment, not typical economic openness
* 3. Including Malta steepens the slope (2.31 vs 1.68)
* 4. The relationship for "typical" countries may be better captured without Malta

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