This project implements a Monte Carlo simulation framework to examine heterogeneous effects of Regional Trade Agreements (RTAs) on both trade and foreign direct investment (FDI) flows using staggered difference-in-differences estimation in a structural gravity setting. The analysis extends the pioneering work of Weidner and Zylkin (2021) to incorporate:
- ✅ Staggered RTA implementation across multiple cohorts (5 implementation periods)
- ✅ Joint analysis of trade and FDI outcomes in a partial equilibrium framework
- ✅ Cohort-specific treatment effects identification
- ✅ Dynamic treatment effect evolution over time
- ✅ PPML estimation with three-way fixed effects (exporter-time, importer-time, exporter-importer)
The simulated analysis reveals:
- Heterogeneous RTA Effects: Early implementation cohorts exhibit larger trade effects (78-84% increases) compared to later cohorts (67-73% increases)
- Persistent First-Mover Advantages: Benefits established at implementation persist with no convergence over time
- FDI Stability: Investment effects remain more stable across cohorts (45-58% increases) than trade effects
- Immediate Impact: RTA effects materialize immediately upon implementation with remarkable persistence over 10+ years
- Methodological Importance: Demonstrates the critical need to account for treatment effect heterogeneity in gravity models
sdid_gravity_monte_carlo/
│
├── 📂 code/ # R scripts for simulation and estimation
│ ├── staggered_rta_data_simulation.R # Data generating process for trade & FDI
│ ├── rta_effects_estimation.R # Baseline & cohort-specific estimations
│ └── cohort_dynamic_analysis.R # Cohort-specific dynamic effects over time
│
├── 📂 data/ # Simulated datasets
│ ├── trade_partial_equilibrium.csv # Trade flow data (N×N×T observations)
│ ├── fdi_partial_equilibrium.csv # FDI flow data (N×N×T observations)
│ └── simulation_parameters_partial.rds # True parameter values for validation
│
├── 📂 output/ # Estimation results and visualizations
│ ├── rta_effects_results.tex # Regression tables (LaTeX format)
│ ├── event_study_effects.png # Pooled event study visualization
│ ├── trade_cohort_dynamics.png # Trade effects by cohort over time
│ ├── fdi_cohort_dynamics.png # FDI effects by cohort over time
│ ├── cohort_comparison_dynamics.png # Comparative cohort analysis
│ ├── cohort_dynamic_effects_detailed.csv # Detailed coefficient estimates
│ └── cohort_dynamic_effects_summary.csv # Summary statistics by cohort
│
├── 📄 report.tex # Full technical report (LaTeX source)
├── 📄 report.pdf # Compiled research report
├── 📄 WZ21.pdf # Weidner & Zylkin (2021) reference paper
├── 📄 references.bib # BibTeX bibliography
└── 📄 README.md # This file
The simulation creates a partial equilibrium gravity model for N = 150 countries over T = 55 time periods, generating 1,237,500 observations per outcome variable:
Three-way fixed effects control for multilateral resistance:
- Origin-Time FE (αit): Time-varying supply factors (GDP, productivity)
- Destination-Time FE (γjt): Time-varying demand factors (market size, policies)
- Origin-Destination FE (δij): Time-invariant bilateral factors (distance, culture)
All fixed effects: αit, γjt, δij ~ N(0, 0.25²)
- 30% of country pairs receive RTA treatment
- 5 implementation cohorts: Years 5, 15, 25, 35, 45
- Bidirectional treatment (affects both i→j and j→i flows)
- Treatment persists through end of sample period
Trade Flows:
𝔼[Trade_ijt] = exp(β₁ᵀ × x_ijt + β₂ᵀ × RTA_ijt + α_it + γ_jt + δ_ij)
- β₁ᵀ = -0.8 (distance-type elasticity)
- β₂ᵀ = 0.6 (RTA effect ≈ 82% increase)
FDI Flows:
𝔼[FDI_ijt] = exp(β₁ᶠ × x_ijt + β₂ᶠ × RTA_ijt + α_it + γ_jt + δ_ij)
- β₁ᶠ = -0.6 (distance-type elasticity)
- β₂ᶠ = 0.4 (RTA effect ≈ 49% increase)
- Log-normal multiplicative errors: εijt = exp(σ·ηijt - σ²/2) with σ = 0.5
- AR(1) autocorrelation: ρ = 0.3 (realistic temporal dependence)
- Structural zeros: 5% for trade, 15% for FDI (based on conditional means)
All models estimated using Poisson Pseudo-Maximum Likelihood (PPML) with:
- ✅ Three-way fixed effects (exporter×time, importer×time, exporter×importer)
- ✅ Multi-way clustering (clustered by origin AND destination)
- ✅ Proper handling of zero flows and heteroskedasticity
Three complementary specifications:
- Baseline Effects: Average RTA impact pooling all cohorts and periods
- Cohort-Specific Effects: Separate coefficients for each implementation cohort
- Dynamic Event Study: Time-varying effects by years since implementation
Required R packages:
install.packages(c(
"dplyr", # Data manipulation
"tidyr", # Data reshaping
"purrr", # Functional programming
"fixest", # PPML estimation with HDFE
"ggplot2", # Visualization
"gridExtra" # Multiple plots
))Recommended R version: 4.0 or higher
- Clone the repository:
git clone https://github.com/jamiubadmusng/sdid_gravity_monte_carlo.git
cd sdid_gravity_monte_carlo- Set working directory in each R script to your local path:
setwd("your/path/to/sdid_gravity_monte_carlo")Run the data simulation script first:
source("code/staggered_rta_data_simulation.R")Output:
data/trade_partial_equilibrium.csv(Trade flows)data/fdi_partial_equilibrium.csv(FDI flows)data/simulation_parameters_partial.rds(True parameter values)
Console output:
=== PARTIAL EQUILIBRIUM DATA GENERATION ===
Countries: 150, Time periods: 55
Trade observations: 1237500
Zero trade flows: 5.00%
Zero FDI flows: 15.00%
RTA coverage: 30.00% of international pairs
source("code/rta_effects_estimation.R")Estimation sequence:
- Baseline PPML with average RTA effects
- Cohort-specific effects (5 separate coefficients)
- Event study analysis (dynamic effects by relative time)
Output:
output/rta_effects_results.tex(Regression table)output/event_study_effects.png(Event study plot)
source("code/cohort_dynamic_analysis.R")Analysis:
- Separate event studies for each cohort
- Cohort × time interaction effects
- Visual comparison across cohorts
Output:
output/trade_cohort_dynamics.png(Trade dynamics by cohort)output/fdi_cohort_dynamics.png(FDI dynamics by cohort)output/cohort_comparison_dynamics.png(Comparative analysis)output/cohort_dynamic_effects_detailed.csv(Full coefficients)output/cohort_dynamic_effects_summary.csv(Summary statistics)
| Outcome | Coefficient | Std. Error | % Effect | Interpretation |
|---|---|---|---|---|
| Trade | 0.6131*** | (0.0024) | +84.6% | RTAs nearly double trade flows |
| FDI | 0.4413*** | (0.0023) | +55.4% | RTAs increase FDI by over half |
Note: *** p<0.01. Effects calculated as (exp(β)-1)×100%
| Cohort | Implementation Year | Trade Effect | FDI Effect |
|---|---|---|---|
| 1 (Early) | Year 5 | +78.5%*** | +54.4%*** |
| 2 | Year 15 | +77.6%*** | +50.9%*** |
| 3 (Middle) | Year 25 | +75.9%*** | +51.2%*** |
| 4 | Year 35 | +73.9%*** | +49.1%*** |
| 5 (Late) | Year 45 | +72.9%*** | +49.0%*** |
Key Pattern: Early implementers enjoy 5-6 percentage point advantages that persist throughout the observation period.
✅ Pre-trends: No significant effects in periods t-6 to t-2 (validates parallel trends)
✅ Immediate impact: Full effects materialize at implementation (t=0)
✅ Persistence: Effects remain stable 10+ years post-implementation
✅ No fade-out: Contrary to adjustment theories, benefits do not decay over time
This project synthesizes insights from three methodological traditions:
- Anderson & van Wincoop (2003) - Gravity with multilateral resistance
- Head & Mayer (2014) - Handbook chapter on gravity equations
- Yotov et al. (2016) - Advanced guide to trade policy analysis
- Weidner & Zylkin (2021) - Bias and consistency in three-way gravity models
- Correia, Guimarães & Zylkin (2020) - Fast Poisson estimation with HDFE
- Silva & Tenreyro (2006) - The log of gravity (PPML foundations)
- Callaway & Sant'Anna (2021) - DID with multiple time periods
- Sun & Abraham (2021) - Estimating dynamic treatment effects
- Nagengast & Yotov (2025) - ETWFE estimator within the structural gravity model
- Nagengast et al. (2025) - ETWFE estimator within the structural gravity model for the case of the EU
- Anderson et al. (2019) - Trade and investment in a multi-country dynamic fraemwork
- Larch & Yotov (2025) - Deep trade agreements and FDI in a structural estimation framework
The simulation framework incorporates several validation checks:
✅ Parameter recovery: True parameters βtrade=0.6, βFDI=0.4 closely recovered
✅ Pre-trend tests: Event study shows no anticipatory effects (parallel trends hold)
✅ Specification tests: Pseudo-R² and BIC confirm model fit
✅ Cluster robustness: Multi-way clustering accounts for within-country correlation
✅ Zero-flow handling: PPML naturally accommodates 5-15% structural zeros
This project was developed as part of a research internship at the United Nations University Institute on Comparative Regional Integration Studies (UNU-CRIS) in the Economic and Political Interactions Cluster.
Special Thanks:
- Stijn Salden (Ghent University) for providing the R translation of the original Weidner & Zylkin (2021) Stata code, which served as the foundational code structure for this analysis.
Jamiu Olamilekan Badmus
Erasmus Mundus Master in Economics of Globalization and European Integration
Research Intern, Economic and Political Interactions Cluster
United Nations University Institute on Comparative Regional Integration Studies (UNU-CRIS)
- 🌐 Website: jamiu-olamilekan-badmus
- 📧 Email: jamiubadmus001@gmail.com
- 💻 GitHub: @jamiubadmusng
This project is licensed under the MIT License - see the LICENSE file for details.
You are free to:
- ✅ Use this code for academic research
- ✅ Modify and extend the methodology
- ✅ Distribute and share with attribution
If you use this code or methodology in your research, please cite:
@misc{badmus2025staggered,
author = {Badmus, Jamiu Olamilekan},
title = {Staggered DID in Structural Gravity: Monte Carlo Analysis of Heterogeneous RTA Effects},
year = {2025},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/jamiubadmusng/sdid_gravity_monte_carlo}},
note = {Research Internship Project, UNU-CRIS}
}And cite the foundational methodology:
@article{weidner2021bias,
title={Bias and consistency in three-way gravity models},
author={Weidner, Martin and Zylkin, Thomas},
journal={Journal of International Economics},
volume={132},
pages={103513},
year={2021},
publisher={Elsevier}
}Potential avenues for extension:
- General equilibrium simulation with structural linkages between trade and FDI
- Monte Carlo replications to assess estimator performance under varying DGPs
- Alternative estimators (e.g., Callaway-Sant'Anna, Sun-Abraham)
- Sensitivity analysis to parameter misspecification
- Empirical application to real RTA data (e.g., WTO RTA database)
- Heterogeneous effects by country characteristics (GDP, distance, etc.)
Questions, suggestions, or collaboration opportunities?
- Open an Issue on GitHub
- Email me at jamiubadmus001@gmail.com
⭐ Star this repository if you find it useful for your research!