
/*******************************************************************************
 TITLE:
 Revisiting Twin Deficit Hypothesis in South Asia
 Crowding-Out Effect and Capital Flow Dynamics
*******************************************************************************/

clear all
set more off
set scheme s2color
set varabbrev off

/*******************************************************************************
 SECTION 1: DATA IMPORT & SAMPLE SELECTION
*******************************************************************************/

import excel "04. Raw data.xlsx", sheet("Data") firstrow clear

rename CountryName country
rename Time year

keep if inlist(country,"Bangladesh","Bhutan","India","Nepal","Sri Lanka")
keep if year >= 1990 & year <= 2025

encode country, gen(country_id)
xtset country_id year

/*******************************************************************************
 SECTION 2: DATA CLEANING
*******************************************************************************/

ds, has(type string)
foreach var of varlist `r(varlist)' {
    replace `var' = subinstr(`var',"..","",.)
    replace `var' = subinstr(`var',"NA","",.)
    replace `var' = subinstr(`var',"N/A","",.)
    destring `var', replace force
}

/*******************************************************************************
 SECTION 3: VARIABLE RENAMING (DATASET-CORRECT)
*******************************************************************************/

rename Currentaccount*     cab_gdp
rename Domesticcredit*     private_credit_gdp
rename Foreigndirect*      fdi_gdp
rename GDPpercapitac*      gdp_pc
rename Netlending*         fiscal_balance_gdp
rename Personalremit*      remit_gdp
rename Realinterest*       interest_rate
rename Totaldebtservice*   debt_gni
rename Trade*              trade_gdp

gen ln_gdp = ln(gdp_pc) if gdp_pc > 0
gen fiscal_deficit = -fiscal_balance_gdp

/*******************************************************************************
 SECTION 4: CORE INTERACTIONS (ONLY 2 – OPTIMAL Q1 DESIGN)
*******************************************************************************/

gen fiscal_fdi   = fiscal_balance_gdp * fdi_gdp
gen fiscal_remit = fiscal_balance_gdp * remit_gdp

gen covid = inrange(year,2020,2021)

/*******************************************************************************
 SECTION 5: DESCRIPTIVE STATISTICS (TABLE 1)
*******************************************************************************/

tabstat cab_gdp fiscal_balance_gdp private_credit_gdp interest_rate ///
        fdi_gdp remit_gdp trade_gdp ln_gdp debt_gni, ///
        stat(n mean sd min median max)

/*******************************************************************************
 SECTION 6: CORE DIAGNOSTICS (REVIEWER REQUIREMENT)
*******************************************************************************/

pwcorr cab_gdp fiscal_balance_gdp private_credit_gdp interest_rate ///
       fdi_gdp remit_gdp trade_gdp ln_gdp debt_gni, sig star(0.05)

reg cab_gdp fiscal_balance_gdp private_credit_gdp interest_rate ///
    fdi_gdp remit_gdp trade_gdp ln_gdp debt_gni

vif

xtserial cab_gdp fiscal_balance_gdp private_credit_gdp interest_rate


xtreg cab_gdp fiscal_balance_gdp private_credit_gdp interest_rate ///
      fdi_gdp remit_gdp trade_gdp ln_gdp debt_gni, fe

xttest3



xtcsd, pesaran abs

/*******************************************************************************
 SECTION 7: UNIT ROOT TEST (IPS)
*******************************************************************************/

xtunitroot ips cab_gdp
xtunitroot ips fiscal_balance_gdp
xtunitroot ips private_credit_gdp
xtunitroot ips interest_rate
xtunitroot ips fdi_gdp
xtunitroot ips remit_gdp
xtunitroot ips trade_gdp
xtunitroot ips ln_gdp

/*******************************************************************************
 SECTION 8: COINTEGRATION TEST
*******************************************************************************/

xtwest cab_gdp fiscal_balance_gdp private_credit_gdp interest_rate ///
       fdi_gdp remit_gdp

/*******************************************************************************
 SECTION 9: BASELINE TDH MODEL
*******************************************************************************/

xtreg cab_gdp fiscal_balance_gdp trade_gdp ln_gdp debt_gni covid, ///
      fe vce(cluster country_id)

est store M1

/*******************************************************************************
 SECTION 10: CROWDING-OUT MODEL
*******************************************************************************/

xtreg cab_gdp fiscal_balance_gdp private_credit_gdp interest_rate ///
      trade_gdp ln_gdp debt_gni covid, ///
      fe vce(cluster country_id)

est store M2

/*******************************************************************************
 SECTION 11: CAPITAL FLOW MODEL
*******************************************************************************/

xtreg cab_gdp fiscal_balance_gdp fdi_gdp remit_gdp ///
      trade_gdp ln_gdp debt_gni covid, ///
      fe vce(cluster country_id)

est store M3

/*******************************************************************************
 SECTION 12: MAIN Q1 MODEL (INTERACTIONS)
*******************************************************************************/

xtreg cab_gdp fiscal_balance_gdp private_credit_gdp interest_rate ///
      fdi_gdp remit_gdp fiscal_fdi fiscal_remit ///
      trade_gdp ln_gdp debt_gni covid, ///
      fe vce(cluster country_id)

est store M4

/*******************************************************************************
 SECTION 13: DRISCOLL-KRAAY ROBUST MODEL (FINAL MAIN SPECIFICATION)
*******************************************************************************/

xtscc cab_gdp fiscal_balance_gdp private_credit_gdp interest_rate ///
      fdi_gdp remit_gdp fiscal_fdi fiscal_remit ///
      trade_gdp ln_gdp debt_gni covid, fe

est store M5

/*******************************************************************************
 SECTION 14: MECHANISM TEST (CROWDING-OUT VALIDATION – FULL CONTROLS)
*******************************************************************************/

xtreg private_credit_gdp fiscal_deficit fdi_gdp remit_gdp ///
      trade_gdp ln_gdp debt_gni covid, ///
      fe vce(cluster country_id)

est store MECH1

xtreg interest_rate fiscal_deficit fdi_gdp remit_gdp ///
      trade_gdp ln_gdp debt_gni covid, ///
      fe vce(cluster country_id)

est store MECH2

/*******************************************************************************
 SECTION 15: ROBUSTNESS CHECK (SAMPLE STABILITY)
*******************************************************************************/

preserve
keep if year <= 2019
xtreg cab_gdp fiscal_balance_gdp private_credit_gdp interest_rate ///
      fdi_gdp remit_gdp fiscal_fdi fiscal_remit ///
      trade_gdp ln_gdp debt_gni, fe vce(cluster country_id)
est store R1
restore

preserve
keep if year >= 2010
xtreg cab_gdp fiscal_balance_gdp private_credit_gdp interest_rate ///
      fdi_gdp remit_gdp fiscal_fdi fiscal_remit ///
      trade_gdp ln_gdp debt_gni, fe vce(cluster country_id)
est store R2
restore

/*******************************************************************************
 SECTION 16: FINAL PUBLICATION TABLE EXPORTS
*******************************************************************************/

esttab M1 M2 M3 M4 M5 using "TDH_Q1_MAIN_RESULTS.rtf", ///
replace se star(* 0.10 ** 0.05 *** 0.01) ///
title("Twin Deficit Hypothesis in South Asia: Final Q1 Results")

esttab MECH1 MECH2 using "TDH_Q1_MECHANISM.rtf", ///
replace se star(* 0.10 ** 0.05 *** 0.01) ///
title("Crowding-Out Mechanism Validation")

esttab R1 R2 using "TDH_Q1_ROBUSTNESS.rtf", ///
replace se star(* 0.10 ** 0.05 *** 0.01) ///
title("Robustness Checks")

display "FINAL Q1 SUBMISSION-LOCKED DO FILE EXECUTED SUCCESSFULLY"
