### Install and Load triplediff Source: http://marcelortiz.com/triplediff/index.html Install the development version from GitHub and load the library into the R environment. ```R # install.packages("devtools") devtools::install_github("marcelortizv/triplediff") library(triplediff) ``` -------------------------------- ### Triple-Differences Aggregation Example Source: http://marcelortiz.com/triplediff/reference/agg_ddd.html Example demonstrating data generation, model estimation, and simple aggregation of treatment effects. ```R #---------------------------------------------------------- # Triple Diff with multiple time periods #---------------------------------------------------------- data <- gen_dgp_mult_periods(size = 500, dgp_type = 1)[["data"]] out <- ddd(yname = "y", tname = "time", idname = "id", gname = "state", pname = "partition", xformla = ~cov1 + cov2 + cov3 + cov4, data = data, control_group = "nevertreated", base_period = "varying", est_method = "dr") # Simple aggregation agg_ddd(out, type = "simple", alpha = 0.10) ``` -------------------------------- ### Execute DDD estimation Source: http://marcelortiz.com/triplediff/reference/ddd.html Example call to the ddd function specifying outcome, time, id, group, and partition variables along with covariates and estimation method. ```R ddd(yname = "y", tname = "time", idname = "id", gname = "state", pname = "partition", xformla = ~cov1 + cov2 + cov3 + cov4, data = data, control_group = "nevertreated", base_period = "varying", est_method = "dr") ``` -------------------------------- ### Simulate Data for Two-Period DDD Source: http://marcelortiz.com/triplediff/index.html Generate a synthetic dataset for a two-period DDD setup using the built-in gen_dgp_2periods function. ```R set.seed(1234) # Set seed for reproducibility # Simulate data for a two-periods DDD setup df <- gen_dgp_2periods( size = 5000, # Number of units dgp_type = 1 # Type of DGP design )$data head(df) ``` -------------------------------- ### Simulate Multiple-Period DDD Data Source: http://marcelortiz.com/triplediff/index.html Generates a dataset for a multiple-period DDD setup with staggered treatment adoption using the gen_dgp_mult_periods function. ```R set.seed(1234) # Set seed for reproducibility # Simulate data for a multiple-period DDD setup with staggered treatment adoption data <- gen_dgp_mult_periods(size = 500, dgp_type = 1)$data head(data) ``` -------------------------------- ### Generate Data for Multiple Time Periods Source: http://marcelortiz.com/triplediff/reference/ddd.html Initializes a dataset for a triple difference analysis spanning multiple time periods. ```R #---------------------------------------------------------- # Triple Diff with multiple time periods #---------------------------------------------------------- data <- gen_dgp_mult_periods(size = 1000, dgp_type = 1)[["data"]] ``` -------------------------------- ### Initialize ddd estimator Source: http://marcelortiz.com/triplediff/reference/ddd.html The primary function call for computing Doubly Robust DDD estimators. Ensure all required column names for outcome, time, group, and partition variables are provided. ```R ddd( yname, tname, idname = NULL, gname, pname, xformla, data, control_group = NULL, base_period = NULL, est_method = "dr", panel = TRUE, allow_unbalanced_panel = FALSE, weightsname = NULL, boot = FALSE, nboot = NULL, cluster = NULL, cband = FALSE, alpha = 0.05, use_parallel = FALSE, cores = 1, inffunc = FALSE, skip_data_checks = FALSE ) ``` -------------------------------- ### ddd(yname, tname, idname, gname, pname, xformla, data, ...) Source: http://marcelortiz.com/triplediff/reference/ddd.html Computes the Doubly Robust DDD estimators for the ATT. This is the primary function for the package. ```APIDOC ## ddd ### Description Computes the Doubly Robust DDD estimators for the ATT with balanced panel data, supporting covariates and multiple time periods. ### Parameters - **yname** (string) - Required - The name of the outcome variable. - **tname** (string) - Required - The name of the column containing the time periods. - **idname** (string) - Optional - The name of the column containing the unit id. - **gname** (string) - Required - The name of the column containing the first period when a particular observation is treated. - **pname** (string) - Optional - The name of the column containing the partition variable. - **xformla** (formula) - Optional - The formula for the covariates to be included in the model (default: ~1). - **data** (data.frame/data.table) - Required - The data to be used. - **control_group** (string) - Optional - The control group to be used (default: "notyettreated"). - **base_period** (string) - Optional - Choose between "varying" or "universal" base period. - **est_method** (string) - Optional - The estimation method: "dr", "reg", or "ipw" (default: "dr"). - **panel** (boolean) - Optional - Whether data is treated as panel data (default: TRUE). - **allow_unbalanced_panel** (boolean) - Optional - Whether to allow unbalanced panel data (default: FALSE). - **weightsname** (string) - Optional - The name of the column containing the weights. - **boot** (boolean) - Optional - Whether to compute standard errors using multiplier bootstrap (default: FALSE). - **nboot** (integer) - Optional - Number of bootstrap samples (default: 999 if boot=TRUE). - **cluster** (string) - Optional - The name of the variable to be used for clustering. - **cband** (boolean) - Optional - Whether to compute a uniform confidence band (default: FALSE). - **alpha** (numeric) - Optional - Significance level for confidence intervals (default: 0.05). - **use_parallel** (boolean) - Optional - Whether to use parallel processing (default: FALSE). - **cores** (integer) - Optional - Number of cores for parallel processing (default: 1). - **inffunc** (boolean) - Optional - Whether to return the influence function (default: FALSE). - **skip_data_checks** (boolean) - Optional - Whether to skip data checks (default: FALSE). ``` -------------------------------- ### Prepare Aggregated Data for Visualization Source: http://marcelortiz.com/triplediff/index.html Extract event-study estimates and confidence intervals into a tibble for plotting with ggplot2. ```R es_df <- with(es_e$aggte_ddd, { tibble::tibble(period = egt, estimate = att.egt, ci_lower = estimate - crit.val.egt * se.egt, ci_upper = estimate + crit.val.egt * se.egt) }) all_per <- sort(unique(es_df$period)) ``` -------------------------------- ### gen_dgp_mult_periods() Source: http://marcelortiz.com/triplediff/reference/index.html Generate panel data with staggered treatment adoption (three periods). ```APIDOC ## gen_dgp_mult_periods() ### Description Generate panel data with staggered treatment adoption (three periods). ``` -------------------------------- ### ddd() Source: http://marcelortiz.com/triplediff/reference/index.html Doubly Robust DDD estimators for the group-time average treatment effects. ```APIDOC ## ddd() ### Description Doubly Robust DDD estimators for the group-time average treatment effects. ``` -------------------------------- ### Usage of gen_dgp_2periods Source: http://marcelortiz.com/triplediff/reference/gen_dgp_2periods.html Call this function with the desired sample size and a DGP type integer to generate a list containing the panel data and theoretical parameters. ```R gen_dgp_2periods(size, dgp_type) ``` -------------------------------- ### Usage of gen_dgp_mult_periods Source: http://marcelortiz.com/triplediff/reference/gen_dgp_mult_periods.html Call this function to generate a simulated dataset with specified unit size and model configuration. ```R gen_dgp_mult_periods(size, dgp_type = 1, include_covariates = TRUE) ``` -------------------------------- ### Function Usage for agg_ddd Source: http://marcelortiz.com/triplediff/reference/agg_ddd.html Standard signature for the agg_ddd function. ```R agg_ddd( ddd_obj, type = "eventstudy", cluster = NULL, balance_e = NULL, min_e = -Inf, max_e = Inf, na.rm = FALSE, boot = NULL, nboot = NULL, cband = NULL, alpha = 0.05 ) ``` -------------------------------- ### BibTeX Citation for R Package Source: http://marcelortiz.com/triplediff/authors.html Use this BibTeX entry to cite the triplediff R package version 0.2.4. ```BibTeX @Misc{, title = {triplediff: Triple Differences Estimators}, author = {Marcelo Ortiz-Villavicencio and Pedro H.C. Sant'Anna}, year = {2025}, note = {R package version 0.2.4}, url = {http://marcelortiz.com/triplediff/}, } ``` -------------------------------- ### Aggregate DDD results by event study Source: http://marcelortiz.com/triplediff/reference/agg_ddd.html Performs dynamic aggregation of ATT estimates based on event time. ```R agg_ddd(out, type = "eventstudy", alpha = 0.10) ``` -------------------------------- ### Aggregate DDD results by calendar time Source: http://marcelortiz.com/triplediff/reference/agg_ddd.html Performs aggregation of ATT estimates based on calendar time. ```R agg_ddd(out, type = "calendar", alpha = 0.10) ``` -------------------------------- ### Visualize DDD estimates with ggplot2 Source: http://marcelortiz.com/triplediff/index.html Creates an error bar plot for DDD estimates with custom theme and axis formatting. ```R p <- ggplot(es_df, aes(period, estimate)) + geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = .15, position = position_dodge(width = .3)) + geom_point(size = 2.2, position = position_dodge(width = .3)) + geom_hline(yintercept = 0, linewidth = .3) + geom_vline(xintercept = -1, linetype = "dashed", linewidth = .3) + scale_x_continuous( breaks = all_per ) + labs(title = "", x = "Event Time", y = "Treatment Effect") + theme_bw(base_size = 12) + theme(plot.title = element_text(hjust = .25), legend.position = "right") p ``` -------------------------------- ### BibTeX Citation for Research Paper Source: http://marcelortiz.com/triplediff/authors.html Use this BibTeX entry to cite the research paper titled 'Better Understanding Triple Differences Estimators'. ```BibTeX @Article{, title = {Better Understanding Triple Differences Estimators}, author = {Marcelo Ortiz-Villavicencio and Pedro H.C. Sant'Anna}, year = {2025}, journal = {ArXiv}, url = {https://arxiv.org/abs/2505.09942}, } ``` -------------------------------- ### agg_ddd() Source: http://marcelortiz.com/triplediff/reference/index.html Aggregate Group-Time Average Treatment Effects in Staggered Triple-Differences Designs. ```APIDOC ## agg_ddd() ### Description Aggregate Group-Time Average Treatment Effects in Staggered Triple-Differences Designs. ``` -------------------------------- ### Estimate Triple Difference with Covariates Source: http://marcelortiz.com/triplediff/reference/ddd.html Demonstrates a two-period DDD estimation using covariates and the DR (doubly robust) method. ```R #---------------------------------------------------------- # Triple Diff with covariates and 2 time periods #---------------------------------------------------------- set.seed(1234) # Set seed for reproducibility # Simulate data for a two-periods DDD setup df <- gen_dgp_2periods(size = 5000, dgp_type = 1)$data head(df) #> Key: #> id state partition time y cov1 cov2 cov3 #> #> 1: 1 0 0 1 209.9152 -0.97080934 -1.1726958 2.3893945 #> 2: 1 0 0 2 417.5260 -0.97080934 -1.1726958 2.3893945 #> 3: 2 0 0 1 211.4919 0.02591115 0.2763066 0.1063123 #> 4: 2 0 0 2 420.3656 0.02591115 0.2763066 0.1063123 #> 5: 3 0 0 1 221.9431 0.97147321 -0.4292088 0.5012794 #> 6: 3 0 0 2 440.9623 0.97147321 -0.4292088 0.5012794 #> cov4 cluster #> #> 1: 0.2174955 39 #> 2: 0.2174955 39 #> 3: -0.1922253 29 #> 4: -0.1922253 29 #> 5: 1.1027248 44 #> 6: 1.1027248 44 att_22 <- ddd(yname = "y", tname = "time", idname = "id", gname = "state", pname = "partition", xformla = ~cov1 + cov2 + cov3 + cov4, data = df, control_group = "nevertreated", est_method = "dr") summary(att_22) #> Call: #> ddd(yname = "y", tname = "time", idname = "id", gname = "state", #> pname = "partition", xformla = ~cov1 + cov2 + cov3 + cov4, #> data = df, control_group = "nevertreated", est_method = "dr") #> =========================== DDD Summary ============================== #> DR-DDD estimation for the ATT: #> ATT Std. Error Pr(>|t|) [95% Ptwise. Conf. Band] #> -0.0780 0.0828 0.3463 -0.2404 0.0843 #> #> Note: * indicates that the confidence interval does not contain zero. #> --------------------------- Data Info ----------------------------- #> Panel Data #> Outcome variable: y #> Qualification variable: partition #> No. of units at each subgroup: #> treated-and-eligible: 1232 #> treated-but-ineligible: 1285 #> eligible-but-untreated: 1256 #> untreated-and-ineligible: 1227 #> --------------------------- Algorithms ------------------------------ #> Outcome Regression estimated using: OLS #> Propensity score estimated using: Maximum Likelihood #> --------------------------- Std. Errors ---------------------------- #> Level of significance: 0.05 #> Analytical standard errors. #> Type of confidence band: Pointwise Confidence Interval #> ===================================================================== #> See Ortiz-Villavicencio and Sant'Anna (2025) for details. ``` -------------------------------- ### Estimate ATT(2,2) using ddd Source: http://marcelortiz.com/triplediff/index.html Estimates the average treatment effect on the treated using the ddd function with specified formula and control group. ```R att_22 <- ddd(yname = "y", tname = "time", idname = "id", gname = "state", pname = "partition", xformla = ~cov1 + cov2 + cov3 + cov4, data = df, control_group = "nevertreated", est_method = "dr") summary(att_22) ``` -------------------------------- ### Estimate GMM-based DDD with not-yet-treated units Source: http://marcelortiz.com/triplediff/index.html Uses the ddd function with control_group set to 'notyettreated' to leverage additional information for estimation. ```R # do the same but using GMM-based notyettreated att_gt_nyt <- ddd(yname = "y", tname = "time", idname = "id", gname = "state", pname = "partition", xformla = ~cov1 + cov2 + cov3 + cov4, data = data, control_group = "notyettreated", base_period = "universal", est_method = "dr") summary(att_gt_nyt) ``` -------------------------------- ### ddd() Source: http://marcelortiz.com/triplediff/reference/ddd.html Performs Triple Difference estimation on panel data. ```APIDOC ## ddd(yname, tname, idname, gname, pname, xformla, data, control_group, base_period, est_method) ### Description Estimates the Average Treatment Effect on the Treated (ATT) for groups and time periods using a Triple Difference (DDD) approach. ### Parameters - **yname** (string) - Name of the outcome variable. - **tname** (string) - Name of the time variable. - **idname** (string) - Name of the unit identifier variable. - **gname** (string) - Name of the group variable. - **pname** (string) - Name of the partition variable. - **xformla** (formula) - Formula for covariates. - **data** (data.frame) - The dataset to be used. - **control_group** (string) - The control group definition (e.g., "nevertreated"). - **base_period** (string) - The base period strategy (e.g., "varying"). - **est_method** (string) - The estimation method (e.g., "dr"). ``` -------------------------------- ### gen_dgp_2periods(size, dgp_type) Source: http://marcelortiz.com/triplediff/reference/gen_dgp_2periods.html Generates panel data with a single treatment date and two periods based on the specified size and DGP type. ```APIDOC ## gen_dgp_2periods(size, dgp_type) ### Description Generates panel data with a single treatment date and two periods. This function is used to create datasets for testing estimators under different nuisance function specifications. ### Arguments - **size** (Integer) - Number of units. - **dgp_type** (Integer) - Value in {1, 2, 3, 4}. 1 = both nuisance functions correct; 2 = only the outcome model correct; 3 = only the propensity score correct; 4 = both nuisance functions incorrect. ### Value A list containing: - **data** (data.table) - Long format data with columns: id, state, time, partition, x1, x2, x3, x4, y, cluster. - **att** (Numeric) - True average treatment effect on the treated (ATT), set to 0. - **att.unf** (Numeric) - Oracle ATT computed under the unfeasible specification. - **eff** (Numeric) - Theoretical efficiency bound for the estimator. ``` -------------------------------- ### gen_dgp_2periods() Source: http://marcelortiz.com/triplediff/reference/index.html Function that generates panel data with single treatment date assignment and two time periods. ```APIDOC ## gen_dgp_2periods() ### Description Function that generates panel data with single treatment date assignment and two time periods. ``` -------------------------------- ### Aggregate DDD Results Source: http://marcelortiz.com/triplediff/index.html Aggregate the group-time average treatment effects into event-study estimates using the agg_ddd function. ```R es_e <- agg_ddd(att_gt, type = "eventstudy") summary(es_e) ``` -------------------------------- ### Aggregate DDD results by group Source: http://marcelortiz.com/triplediff/reference/agg_ddd.html Performs aggregation of ATT estimates based on cohort or group. ```R agg_ddd(out, type = "group", alpha = 0.10) ``` -------------------------------- ### Estimate Group-Time Average Treatment Effect Source: http://marcelortiz.com/triplediff/index.html Use the ddd function to estimate the ATT(g,t) for panel data. Requires specifying outcome, time, id, group, and partition variables. ```R att_gt <- ddd(yname = "y", tname = "time", idname = "id", gname = "state", pname = "partition", xformla = ~cov1 + cov2 + cov3 + cov4, data = data, control_group = "nevertreated", base_period = "universal", est_method = "dr") summary(att_gt) ``` -------------------------------- ### agg_ddd(ddd_obj, type, alpha) Source: http://marcelortiz.com/triplediff/reference/agg_ddd.html Aggregates DDD results based on the specified type and significance level. ```APIDOC ## agg_ddd(ddd_obj, type, alpha) ### Description Aggregates DDD results from a previously computed object. Supports event study, group, and calendar time aggregation. ### Parameters - **ddd_obj** (object) - Required - The DDD object to be aggregated. - **type** (string) - Required - The type of aggregation to perform: "eventstudy", "group", or "calendar". - **alpha** (numeric) - Optional - The level of significance for confidence intervals (default is 0.10). ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.