Skip to content

human-nature-lab/TSCSMethods.jl

Repository files navigation

TSCSMethods.jl

CI Documentation Docs

Non-parametric generalized difference-in-differences with covariate matching for time-series cross-sectional data

TSCSMethods.jl v2.0.1 implements the matching methodology developed in Feltham et al. (2023), which extends the framework of Imai et al. (2021) with novel innovations for causal inference in staggered treatment designs. The package provides non-parametric generalized difference-in-differences estimation with covariate matching for panel data, where units receive treatment at different times.

Key Features

  • Staggered treatments: Handle units treated at different times
  • Flexible time windows: Specify pre-treatment matching periods and post-treatment estimation
  • Event studies: Focus on treatment effects relative to event timing
  • Covariate matching: Match treated units to similar controls using time-varying covariates
  • Multiple balancing: Manual and automatic covariate balancing
  • Advanced features: Calipers, stratification, refinement, auto-balancing
  • Bootstrap inference: Weighted block-bootstrap for uncertainty quantification
  • Statistical validation and comprehensive testing: See documentation and test files.

Quick Start

using TSCSMethods

# Load example data
dat = example_data()

# Create model for causal inference
model = makemodel(
    dat,              # Your data
    :day,             # Time variable
    :fips,            # Unit identifier  
    :gub,             # Treatment variable
    :death_rte,       # Outcome variable
    [:pop_dens],      # Covariates for matching
    Dict(:pop_dens => false),  # Which covariates are time-varying
    5:10,             # F: Post-treatment periods to estimate
    -15:-10           # L: Pre-treatment periods for matching
)

### Key Parameters Explained

- **F periods** (`5:10`): How many periods after treatment to estimate effects
- **L periods** (`-15:-10`): Which pre-treatment periods to use for matching
  - **Must be negative** for pre-treatment
  - Used to find similar control units
- **Time-varying covariates**: Set `true` if covariate changes over time

# Complete workflow
match!(model, dat)      # Find matched control units
balance!(model, dat)    # Balance covariates  
estimate!(model, dat; dobayesfactor=false)  # Estimate treatment effects

# Results
println("ATT: ", model.overall.ATT)
println("95% CI: [", model.overall.p05, ", ", model.overall.p95, "]")

User Workflow

flowchart TD
    A["Load Data<br/>example_data()"] --> B["Create Model<br/>makemodel()"]
    B --> C["match!(model, dat)"]
    C --> D["balance!(model, dat)"]
    D --> E["estimate!(model, dat)"]
    E --> F["Results<br/>ATT & Confidence Intervals"]
    
    style A fill:#e3f2fd,stroke:#1565c0,stroke-width:2px
    style F fill:#c8e6c9,stroke:#2e7d32,stroke-width:3px
Loading

Installation

TSCSMethods.jl v2.0.1 requires Julia 1.10 or later (tested on 1.10, 1.11). Install from the Julia REPL:

using Pkg
Pkg.add(url="https://github.com/human-nature-lab/TSCSMethods.jl")

Documentation

Method Overview

The package implements the extended matching approach developed in Feltham et al. (2023), building on Imai et al. (2021), for time-series cross-sectional data:

flowchart TD
    A[Time-Series Cross-Sectional Data] --> B[Treatment Event Detection]
    B --> C[Define Time Windows]
    C --> D["F: Post-Treatment Periods<br/>(e.g., 1:10)"]
    C --> E["L: Pre-Treatment Periods<br/>(e.g., -20:-1)"]
    
    D --> F[Matching Process]
    E --> F
    
    F --> G[Mahalanobis Distance Calculation]
    G --> H[Covariate Balancing]
    H --> I[Treatment Effect Estimation]
    I --> J[Bootstrap Inference]
    J --> K["ATT with Confidence Intervals<br/>Final Results"]
    
    style A fill:#e1f5fe,stroke:#01579b,stroke-width:2px
    style K fill:#c8e6c9,stroke:#2e7d32,stroke-width:3px
    style F fill:#fff3e0,stroke:#ef6c00,stroke-width:2px
    style H fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px
Loading

This approach addresses key challenges in panel data analysis: selection bias, time-varying confounding, and temporal correlation.

System Requirements

  • Julia: 1.10 or later
  • Memory: 8GB+ recommended (larger for larger datasets)
  • OS: Windows, macOS, or Linux

The package automatically installs all required dependencies.

Citation

If you use TSCSMethods.jl in your research, please cite:

@article{feltham_mass_2023,
  title={Mass gatherings for political expression had no discernible association with the local course of the COVID-19 pandemic in the USA in 2020 and 2021},
  author={Feltham, Eric and Forastiere, Laura and Alexander, Marcus and Christakis, Nicholas A},
  journal={Nature Human Behaviour},
  year={2023},
  publisher={Nature Publishing Group}
}

@misc{feltham_tscsmethods_2023,
  title={TSCSMethods.jl: Matching methods for causal inference with time-series cross-sectional data},
  author={Feltham, Eric Martin},
  year={2023},
  url={https://github.com/human-nature-lab/TSCSMethods.jl}
}

Please also cite the foundational methodology:

@article{imai_matching_2021,
  title={Matching Methods for Causal Inference with Time-Series Cross-Sectional Data},
  author={Imai, Kosuke and Kim, In Song and Wang, Erik H},
  journal={American Journal of Political Science},
  year={2021},
  publisher={Wiley Online Library}
}

Contributing

Contributions are welcome!

References

  • Imai, K., Kim, I. S., & Wang, E. H. (2021). Matching Methods for Causal Inference with Time-Series Cross-Sectional Data. American Journal of Political Science.
  • Feltham, E., Forastiere, L., Alexander, M., & Christakis, N. A. (2023). Mass gatherings for political expression had no discernible association with the local course of the COVID-19 pandemic in the USA in 2020 and 2021. Nature Human Behaviour.
  • Kim, I. S., Ruah, A., Wang, E., & Imai, K. (2020). Insongkim/PanelMatch [R, C]. https://github.com/insongkim/PanelMatch (Original work published 2018)

About

Non-parametric generalized differences-in-differences estimation, with covariate matching.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages