The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

Package {DistLagHet}


Type: Package
Title: Treatment Effect Estimation in Complex Designs
Version: 0.1.0
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.
License: MIT + file LICENSE
Encoding: UTF-8
Depends: R (≥ 3.5.0)
Imports: MASS, Rcpp (≥ 1.0.0)
LinkingTo: Rcpp, RcppEigen
Suggests: testthat (≥ 3.0.0)
RoxygenNote: 7.3.2
NeedsCompilation: yes
Packaged: 2026-10-15 09:34:12 UTC; henrifabre
Author: Clement de Chaisemartin [aut], Xavier D'Haultfoeuille [aut], Henri Fabre [cre, ctb]
Maintainer: Henri Fabre <henri.fabre@ensae.fr>
Repository: CRAN
Date/Publication: 2026-10-24 11:50:07 UTC

DistLagHet: Treatment Effect Estimation in Complex Designs

Description

Implements estimators for treatment effects in complex designs under parallel-trends assumptions, with optimized C++ routines for the core computations.

Author(s)

Maintainer: Henri Fabre henri.fabre@ensae.fr [contributor]

Authors:


Internal Bootstrap Function for RC Models

Description

Internal function to compute bootstrap standard errors and confidence intervals. This function is called by estim_RC_model_unified when bootstrap = TRUE. Uses the fast C++ backend for base model.

Usage

.bootstrap_RC_model_internal(
  result,
  data,
  group_col,
  deltaY_col,
  deltaD_col,
  D_col,
  X_cols,
  K = NULL,
  B = 1000,
  conf_level = 0.95,
  verbose = TRUE,
  same_sample = FALSE
)

Arguments

result

A result object from an RC model estimation function.

data

Data frame containing the panel data.

group_col

Character. Name of the column identifying groups.

deltaY_col

Character. Name of the column for DeltaY.

deltaD_col

Character. Name of the column for DeltaD.

D_col

Character. Name of the column for D.

X_cols

Character vector. Names of columns for covariates.

K

Integer. Number of lags (required for "base" and "interactions" models).

B

Integer. Number of bootstrap iterations.

conf_level

Numeric. Confidence level for intervals.

verbose

Logical. Show progress bar.

Value

A list containing bootstrap statistics:

se

Bootstrap standard errors

t_stat

t-statistics

ci_lower

Lower bounds of confidence intervals

ci_upper

Upper bounds of confidence intervals

boot_iterations

Number of successful bootstrap iterations


Build M matrix using C++ (vectorized)

Description

Fast C++ implementation of M matrix construction for RC model.

Usage

build_M_matrix_cpp(DeltaD_g, nb_u, K)

Arguments

DeltaD_g

NumericVector of DeltaD for group g

nb_u

Integer, number of usable observations

K

Integer, number of lags

Value

NumericMatrix M_g (nb_u x (K+1))


Build lower triangular M matrix for full dynamics model

Description

Build lower triangular M matrix for full dynamics model

Usage

build_M_matrix_full_cpp(DeltaD_g, T_periods)

Arguments

DeltaD_g

NumericVector of DeltaD for group g

T_periods

Integer, number of periods

Value

NumericMatrix M_g (T_periods-1 x T_periods-1)


Build M matrix with interactions for interactions model

Description

Build M matrix with interactions for interactions model

Usage

build_M_matrix_interactions_cpp(DeltaD_g, D_g, nb_u, K)

Arguments

DeltaD_g

NumericVector of DeltaD for group g

D_g

NumericVector of D for group g

nb_u

Integer, number of usable observations

K

Integer, number of lags

Value

NumericMatrix M_g with main effects and interactions


Compute projection matrix Pi_g efficiently in C++

Description

Computes Pi_g = I - M_g

Usage

compute_projection_cpp(M_g)

Arguments

M_g

NumericMatrix M for group g

Value

NumericMatrix projection matrix Pi_g


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 RC model estimation in C++ (base model)

Description

Complete C++ implementation of the base RC model estimation. This replaces the entire R loop structure with optimized C++ code.

Usage

estim_RC_model_cpp_full(
  K,
  list_g_data,
  list_g,
  DeltaY,
  DeltaD,
  D,
  X,
  weights,
  same_sample = FALSE
)

Arguments

K

Integer, number of lags

list_g_data

IntegerVector, group identifiers for each observation

list_g

IntegerVector, unique group values

DeltaY

NumericVector, outcome differences

DeltaD

NumericVector, treatment differences

D

NumericVector, treatment levels

X

NumericMatrix, covariates (can have 0 columns)

weights

NumericVector, weights for each group

Value

List with 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.


Full dynamics RC model estimation in C++

Description

Full dynamics RC model estimation in C++

Usage

estim_RC_model_full_cpp(
  T_periods,
  list_g_data,
  list_g,
  DeltaY,
  DeltaD,
  X,
  weights,
  group_sizes,
  same_sample = FALSE
)

Arguments

T_periods

Integer, number of periods

list_g_data

IntegerVector, group identifiers for each observation

list_g

IntegerVector, unique group values

DeltaY

NumericVector, outcome differences

DeltaD

NumericVector, treatment differences

X

NumericMatrix, covariates

weights

NumericVector, weights for each group

group_sizes

IntegerVector, number of observations per group

Value

List with 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.


Interactions RC model estimation in C++

Description

Interactions RC model estimation in C++

Usage

estim_RC_model_interactions_cpp(
  K,
  list_g_data,
  list_g,
  DeltaY,
  DeltaD,
  D,
  X,
  weights,
  same_sample = FALSE
)

Arguments

K

Integer, number of lags

list_g_data

IntegerVector, group identifiers for each observation

list_g

IntegerVector, unique group values

DeltaY

NumericVector, outcome differences

DeltaD

NumericVector, treatment differences

D

NumericVector, treatment levels

X

NumericMatrix, covariates

weights

NumericVector, weights for each group

Value

List with 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)



Extract group data efficiently in C++

Description

Fast extraction of group-specific data

Usage

extract_by_indices_cpp(data, indices)

Arguments

data

NumericVector or NumericMatrix

indices

IntegerVector of indices to extract

Value

Extracted data


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)


Compute weighted projection and store results (Phase 1 loop body)

Description

Optimized C++ implementation of the inner Phase 1 loop

Usage

phase1_group_cpp(DeltaY_g, X_g, DeltaD_g, nb_u, K, sqrt_weight)

Arguments

DeltaY_g

NumericVector of DeltaY for group g

X_g

NumericMatrix of X for group g (can be empty)

DeltaD_g

NumericVector of DeltaD for group g

nb_u

Integer, number of usable observations

K

Integer, number of lags

sqrt_weight

Double, square root of weight for this group

Value

List containing M_g, newY_g, newX_g


Compute beta coefficients for group (Phase 2 loop body)

Description

Optimized C++ implementation of Phase 2 beta computation

Usage

phase2_group_cpp(M_g, DeltaY_Xg_gamma, K, weight_j)

Arguments

M_g

NumericMatrix M for group g

DeltaY_Xg_gamma

NumericVector residuals after removing X*gamma

K

Integer, number of lags

weight_j

Double, weight for this group

Value

List with ind_avg and beta_hat vectors


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)


These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.