Package 'DistLagHet'

Title: Treatment Effect Estimation in Complex Designs
Description: Implements estimation methods for treatment effects in complex designs under parallel-trends assumptions. Based on the methodology of de Chaisemartin and D'Haultfoeuille (2025) <doi:10.48550/arXiv.2508.07808>. Provides three model specifications: base model with K lags, full dynamics model using all available periods, and interactions model with lag interaction terms. Includes C++ optimizations for faster bootstrap estimation.
Authors: Clement de Chaisemartin [aut], Xavier D'Haultfoeuille [aut], Henri Fabre [cre, ctb]
Maintainer: Henri Fabre <[email protected]>
License: MIT + file LICENSE
Version: 0.1.0
Built: 2026-10-24 14:46:29 UTC
Source: https://github.com/cran/DistLagHet

Help Index


Base RC Model Estimation (C++ backend)

Description

Estimates the base RC model with K lags using optimized C++ code. This function uses RcppEigen for fast matrix operations.

Usage

estim_RC_model_base(
  K,
  data,
  group_col,
  deltaY_col,
  deltaD_col,
  D_col,
  X_cols,
  weights = NULL,
  same_sample = FALSE
)

Arguments

K

Integer. Number of lags (required for "base" and "interactions" models, ignored for "full_dynamics"). Default is NULL.

data

Data frame containing the panel data.

group_col

Character. Name of the column identifying groups (e.g., counties, individuals).

deltaY_col

Character. Name of the column for DeltaY (first difference of outcome).

deltaD_col

Character. Name of the column for DeltaD (first difference of treatment).

D_col

Character. Name of the column for D (treatment level).

X_cols

Character vector. Names of columns for covariates.

weights

Numeric vector. Optional weights for estimation (default: uniform weights).

same_sample

Logical. If TRUE, restrict estimation to groups identified for ALL coefficients simultaneously (default: FALSE). When TRUE, also returns n_valid_groups.

Value

A list containing estimation results.


Full Dynamics RC Model Estimation (C++ backend)

Description

Estimates the RC model with full dynamics (all available periods) using optimized C++ code.

Usage

estim_RC_model_full(
  K = NULL,
  data,
  group_col,
  deltaY_col,
  deltaD_col,
  D_col,
  X_cols,
  weights = NULL,
  same_sample = FALSE
)

Arguments

K

Integer. Number of lags (required for "base" and "interactions" models, ignored for "full_dynamics"). Default is NULL.

data

Data frame containing the panel data.

group_col

Character. Name of the column identifying groups (e.g., counties, individuals).

deltaY_col

Character. Name of the column for DeltaY (first difference of outcome).

deltaD_col

Character. Name of the column for DeltaD (first difference of treatment).

D_col

Character. Name of the column for D (treatment level).

X_cols

Character vector. Names of columns for covariates.

weights

Numeric vector. Optional weights for estimation (default: uniform weights).

same_sample

Logical. If TRUE, restrict estimation to groups identified for ALL coefficients simultaneously (default: FALSE). When TRUE, also returns n_valid_groups.

Value

A list containing estimation results.


RC Model with Interaction Terms (C++ backend)

Description

Estimates the RC model with interaction terms between lags using optimized C++ code.

Usage

estim_RC_model_interactions(
  K,
  data,
  group_col,
  deltaY_col,
  deltaD_col,
  D_col,
  X_cols,
  weights = NULL,
  same_sample = FALSE
)

Arguments

K

Integer. Number of lags (required for "base" and "interactions" models, ignored for "full_dynamics"). Default is NULL.

data

Data frame containing the panel data.

group_col

Character. Name of the column identifying groups (e.g., counties, individuals).

deltaY_col

Character. Name of the column for DeltaY (first difference of outcome).

deltaD_col

Character. Name of the column for DeltaD (first difference of treatment).

D_col

Character. Name of the column for D (treatment level).

X_cols

Character vector. Names of columns for covariates.

weights

Numeric vector. Optional weights for estimation (default: uniform weights).

same_sample

Logical. If TRUE, restrict estimation to groups identified for ALL coefficients simultaneously (default: FALSE). When TRUE, also returns n_valid_groups.

Value

A list containing estimation results.


Unified RC Model Estimation with Multiple Specifications

Description

Main wrapper function that allows choosing between different model specifications for estimating treatment effects in complex designs under parallel-trends assumptions. Optionally computes bootstrap standard errors and confidence intervals.

Usage

estim_RC_model_unified(
  K = NULL,
  data,
  group_col,
  deltaY_col,
  deltaD_col,
  D_col,
  X_cols,
  weights = NULL,
  model = c("base", "full_dynamics", "interactions"),
  bootstrap = FALSE,
  B = 200,
  conf_level = 0.95,
  verbose = TRUE,
  same_sample = FALSE
)

Arguments

K

Integer. Number of lags (required for "base" and "interactions" models, ignored for "full_dynamics"). Default is NULL.

data

Data frame containing the panel data.

group_col

Character. Name of the column identifying groups (e.g., counties, individuals).

deltaY_col

Character. Name of the column for DeltaY (first difference of outcome).

deltaD_col

Character. Name of the column for DeltaD (first difference of treatment).

D_col

Character. Name of the column for D (treatment level).

X_cols

Character vector. Names of columns for covariates.

weights

Numeric vector. Optional weights for estimation (default: uniform weights).

model

Character. One of "base", "full_dynamics", or "interactions". Default is "base".

bootstrap

Logical. If TRUE, compute bootstrap standard errors (default: FALSE).

B

Integer. Number of bootstrap iterations (default: 200, ignored if bootstrap = FALSE).

conf_level

Numeric. Confidence level for bootstrap intervals (default: 0.95, ignored if bootstrap = FALSE).

verbose

Logical. Show bootstrap progress bar (default: TRUE, ignored if bootstrap = FALSE).

same_sample

Logical. If TRUE, restrict estimation to groups identified for ALL coefficients simultaneously (default: FALSE). When TRUE, also returns n_valid_groups.

Value

A list containing:

gamma

Coefficients for covariates

B_hat

Estimated treatment effect coefficients (betas)

Nobs

Number of observations used for each coefficient

model

Model type used

n_valid_groups

Number of groups identified for all coefficients (only if same_sample = TRUE)

If bootstrap = TRUE, also includes:

se

Bootstrap standard errors

t_stat

t-statistics (B_hat / se)

ci_lower

Lower bounds of confidence intervals

ci_upper

Upper bounds of confidence intervals

boot_iterations

Number of successful bootstrap iterations

Examples

# Generate example data
data <- generate_did_data(n_groups = 50, n_periods = 5)

# Estimate base model without bootstrap
result <- estim_RC_model_unified(
  K = 2,
  data = data,
  group_col = "group",
  deltaY_col = "delta_Y",
  deltaD_col = "delta_D",
  D_col = "D",
  X_cols = c("X1", "X2"),
  model = "base"
)

# Estimate with bootstrap standard errors
result_boot <- estim_RC_model_unified(
  K = 2,
  data = data,
  group_col = "group",
  deltaY_col = "delta_Y",
  deltaD_col = "delta_D",
  D_col = "D",
  X_cols = c("X1", "X2"),
  model = "base",
  bootstrap = TRUE,
  B = 200
)

print(result_boot$B_hat)
print(result_boot$se)

Generate Synthetic DID Panel Data

Description

Creates synthetic panel data for testing and examples. The data includes groups, time periods, treatment indicators, outcomes, and covariates.

Usage

generate_did_data(
  n_groups = 50,
  n_periods = 5,
  treatment_prob = 0.3,
  seed = 123
)

Arguments

n_groups

Integer. Number of groups (e.g., counties, individuals). Default is 50.

n_periods

Integer. Number of time periods. Default is 5.

treatment_prob

Numeric. Probability of treatment switching (0 to 1). Default is 0.3.

seed

Integer. Random seed for reproducibility. Default is 123.

Value

A data frame with columns:

group

Group identifier

time

Time period

D

Treatment level

delta_D

First difference of treatment

Y

Outcome variable

delta_Y

First difference of outcome

X1, X2

Covariates

Examples

# Generate data with 30 groups and 4 time periods
data <- generate_did_data(n_groups = 30, n_periods = 4)
head(data)

Print Summary of RC Model Results

Description

Provides a formatted summary of estimation results, including bootstrap standard errors and confidence intervals if available.

Usage

summary_RC_model(result)

Arguments

result

A list returned by estim_RC_model_unified or related functions.

Value

Invisibly returns the result object.

Examples

data <- generate_did_data()
result <- estim_RC_model_unified(K = 2, data = data, group_col = "group",
                                  deltaY_col = "delta_Y", deltaD_col = "delta_D",
                                  D_col = "D", X_cols = c("X1", "X2"),
                                  model = "base", bootstrap = TRUE)
summary_RC_model(result)