Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Staggered DID in Structural Gravity: Monte Carlo Analysis of Heterogeneous RTA Effects

License: MIT R Methodology Estimation

📊 Overview

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)

🎯 Key Findings

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

📁 Project Structure

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

🔬 Methodology

Data Generating Process

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:

Fixed Effects Structure

Three-way fixed effects control for multilateral resistance:

  • Origin-Time FEit): Time-varying supply factors (GDP, productivity)
  • Destination-Time FEjt): Time-varying demand factors (market size, policies)
  • Origin-Destination FEij): Time-invariant bilateral factors (distance, culture)

All fixed effects: αit, γjt, δij ~ N(0, 0.25²)

Staggered RTA Implementation

  • 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

Outcome Variable Specification

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)

Error Structure

  • 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)

Estimation Strategy

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:

  1. Baseline Effects: Average RTA impact pooling all cohorts and periods
  2. Cohort-Specific Effects: Separate coefficients for each implementation cohort
  3. Dynamic Event Study: Time-varying effects by years since implementation

🚀 Getting Started

Prerequisites

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

Installation

  1. Clone the repository:
git clone https://github.com/jamiubadmusng/sdid_gravity_monte_carlo.git
cd sdid_gravity_monte_carlo
  1. Set working directory in each R script to your local path:
setwd("your/path/to/sdid_gravity_monte_carlo")

Usage

1️⃣ Generate Simulated Data

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

2️⃣ Estimate Baseline & Cohort-Specific Effects

source("code/rta_effects_estimation.R")

Estimation sequence:

  1. Baseline PPML with average RTA effects
  2. Cohort-specific effects (5 separate coefficients)
  3. Event study analysis (dynamic effects by relative time)

Output:

  • output/rta_effects_results.tex (Regression table)
  • output/event_study_effects.png (Event study plot)

3️⃣ Analyze Cohort-Specific Dynamics

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)

📈 Key Results

Baseline RTA Effects

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-Specific Heterogeneity

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.

Dynamic Effects

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


📚 Theoretical Foundation

This project synthesizes insights from three methodological traditions:

1. Structural Gravity Models

2. High-Dimensional Fixed Effects Estimation

3. Difference-in-Differences with Staggered Adoption

4. Staggered Difference-in-Difference in Structural Gravity Settings

5. Trade & Investment Linkages


🧪 Validation & Robustness

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


🤝 Acknowledgments & Credits

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.

👤 Author

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)


📄 License

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

📖 Citation

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}
}

🔮 Future Extensions

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.)

📞 Contact & Feedback

Questions, suggestions, or collaboration opportunities?

Star this repository if you find it useful for your research!


Made with ❤️ for the international trade research community

GitHub stars GitHub forks

Releases

Packages

Contributors

Languages