Inference in two-period triple differences
We use a two-period triple-differences (DDD) design to study how treatment-effect heterogeneity affects inference for the population average treatment effect on the treated (ATT). Fully interacted OLS and regression adjustment (RA) give the same point estimate in every draw. Do both sets of 95% confidence intervals cover the true ATT about 95% of the time?
Data-generating process
We draw a balanced panel of n units observed in two periods. Let S indicate adoption and Q indicate eligibility. Each of the four (S, Q) cells has probability 1/4. Only eligible units in the adopting group receive treatment, starting in period 2. Within each cell, we draw X from a normal distribution with variance 1 and the mean given below.
| (S, Q) | (0, 0) | (0, 1) | (1, 0) | (1, 1) |
|---|---|---|---|---|
| Mean X | −0.5 | 0 | 0.2 | 0.4 |
We set the pre-treatment outcome Y1 to zero and generate the second-period outcome Y2 = ΔY as follows.
ΔY = 0.3 + 0.5S − 0.4Q + (−2 + S − 0.8Q)X + SQ(2 + bX) + ε.
The error ε is independent of (S, Q, X) and normal with mean zero and variance 2. Conditional DDD parallel trends holds because, at each X, the mean untreated change is additive in S and Q. The effect for a treated unit is 2 + bX. Since the population mean of X among treated units is 0.4, the ATT is 2 + 0.4b.
Regression and standard errors
In each simulated sample, we center X at its mean among treated units, X̄11. With = X − X̄11, we estimate the following fully interacted regression in first differences.
ΔY = α + γS + δQ + τSQ + (β + βSS + βQQ + θSQ) + u.
The coefficient τ on SQ gives the RA point estimate. To see this, note that the regression fits a separate line for the outcome change in each cell. The RA estimator takes the DDD of the fitted changes at each treated unit’s X and averages over those units. Because the fits are linear, this is equivalent to taking the DDD at X̄11, where = 0.
We use two standard errors for this estimate. The first is the regression-based standard error (HC2) on S:Q from estimatr::lm_robust(dy ~ S * Q * xt, data = dat), where dy is ΔY and xt is . HC2 scales the squared residual for unit i by 1/(1 − hii), where hii is its leverage. It treats X̄11 as fixed.
The second standard error is based on the RA DDD influence function from triplediff::ddd with est_method = "reg", panel = TRUE, and boot = FALSE. It accounts for the sampling variation in X̄11, which matters when the effect varies with X. We use the same normal critical value for both 95% confidence intervals.
To see how the two standard errors affect coverage, we vary treatment-effect heterogeneity and sample size. The slider varies b, the change in the treatment effect for a one-unit increase in X. At b = 0, the effect is constant. The sample-size menu changes the number of units per draw.
1,000 Monte Carlo draws per setting.
Coverage as heterogeneity increases
The dots mark the selected slope.
Selected setting
- Population ATT
- 3.60
- Sampling SD
- 0.230
- Variance HC2 leaves out
| Method | Mean SE | Coverage |
|---|---|---|
| Regression-based standard error (HC2) | 0.142 | 78.1% |
| Standard error based on RA DDD influence function | 0.228 | 94.1% |
The variance share is the percentage of the estimate’s large-sample variance that comes from estimating the mean of X among treated units. The regression-based standard error treats that mean as fixed.
Sampling distribution
The histogram shows the common estimation error. The normal curves use each method’s mean standard error.
95% confidence intervals
All 1,000 draws in simulation order. The same row in each panel uses the same draw.
Replication code (R)
Run Rscript ddd-inference.R 2000 4 for the default setting. The two arguments set the sample size and slope. The script saves the draws as a CSV and the three figures as a PDF. A single-setting run uses the saved coverage curve for that sample size. Run Rscript ddd-inference.R all to recompute all 404 settings and plot the default setting.
# Rscript ddd-inference.R 2000 4 # sample size, treatment-effect slope
# Rscript ddd-inference.R all # all 404 settings; plots for n = 2,000, b = 4
# install.packages(c("estimatr", "triplediff", "foreach", "doRNG"))
# The original runs used R 4.4.2, estimatr 1.0.4, and triplediff 0.3.0.
library(doRNG)
foreach::registerDoSEQ()
RNGversion("4.4.2")
simulate_draw <- function(n, b) {
cell <- sample.int(4, n, replace = TRUE, prob = rep(0.25, 4))
S <- as.numeric(cell %in% c(3, 4))
Q <- as.numeric(cell %in% c(2, 4))
x <- rnorm(n, c(-0.5, 0, 0.2, 0.4)[cell], 1)
dy <- 0.3 + 0.5 * S - 0.4 * Q + (-2 + S - 0.8 * Q) * x +
S * Q * (2 + b * x) + rnorm(n, 0, sqrt(2))
xt <- x - mean(x[S == 1 & Q == 1])
fit <- estimatr::lm_robust(dy ~ S * Q * xt, se_type = "HC2")
panel <- data.frame(
id = rep(seq_len(n), 2), period = rep(1:2, each = n),
y = c(rep(0, n), dy), g = rep(ifelse(S == 1, 2, 0), 2),
Q = rep(Q, 2), x = rep(x, 2)
)
ra <- triplediff::ddd(
yname = "y", tname = "period", idname = "id", gname = "g",
pname = "Q", xformla = ~x, data = panel, est_method = "reg",
panel = TRUE, boot = FALSE
)
stopifnot(abs(coef(fit)[["S:Q"]] - ra$ATT) < 1e-10)
c(estimate = ra$ATT, se_hc2 = fit$std.error[["S:Q"]], se_ra = ra$se)
}
simulate_setting <- function(n, b) {
set.seed(20261033)
draws <- foreach(draw = seq_len(1000), .combine = rbind) %dorng% simulate_draw(n, b)
# For each method, the interval is estimate +/- qnorm(0.975) * SE.
data.frame(n = n, b = b, draw = seq_len(nrow(draws)), true_att = 2 + 0.4 * b, draws)
}
covered <- function(draws) {
abs(draws$estimate - draws$true_att) <= qnorm(0.975) * as.matrix(draws[c("se_hc2", "se_ra")])
}
write_figures <- function(draws, coverage_curve, file) {
method <- c("Regression-based standard error (HC2)",
"Standard error based on RA DDD influence function")
colors <- c("#62666D", "#0057D9")
styles <- c(1, 2)
slope <- draws$b[1]
error <- draws$estimate - draws$true_att
se <- as.matrix(draws[c("se_hc2", "se_ra")])
halfwidth <- qnorm(0.975) * se
reach <- abs(error) + apply(halfwidth, 1, max)
covers <- covered(draws)
round_limit <- function(x) ceiling(4 * 1.04 * x) / 4
error_ticks <- function(limit) {
ticks <- seq(-limit, limit, length.out = 5)
axis(1, at = ticks, labels = as.character(sign(ticks) * floor(abs(ticks) * 100 + 0.5) / 100))
}
centered_legend <- function(...) {
bounds <- par("usr")
y <- grconvertY(grconvertY(bounds[3], "user", "inches") - 0.85, "inches", "user")
legend(mean(bounds[1:2]), y, ...,
horiz = TRUE, bty = "n", text.width = NA, x.intersp = 1.3, xjust = 0.5, yjust = 0.5, xpd = NA, cex = 0.78)
}
pdf(file, width = 9, height = 4.5, bg = "white")
on.exit(dev.off())
par(mar = c(5, 3.8, 2.7, 1.2), xaxs = "i", yaxs = "i", bty = "n", las = 1)
ticks <- seq(0.4, 1, by = 0.1)
plot(NA, xlim = c(0, 10), ylim = c(0.4, 1), axes = FALSE,
xlab = "Heterogeneity slope, b", ylab = "")
title("Coverage as heterogeneity increases", adj = 0)
abline(h = ticks, col = "#E8E8E8")
abline(h = 0.95, lty = 2)
text(10, 0.97, "95% nominal", adj = 1, cex = 0.8)
abline(v = slope, col = "#999999", lty = 3)
matlines((0:100) / 10, coverage_curve, col = colors, lty = styles, lwd = 2)
points(rep(slope, 2), colMeans(covers), col = colors, pch = 16, cex = c(1, 0.8))
axis(1, at = seq(0, 10, by = 2))
axis(2, at = ticks, labels = paste0(round(100 * ticks), "%"), lwd = 0)
centered_legend(method, col = colors, lty = styles, lwd = 2)
bounds <- range(draws$estimate)
bounds <- signif(bounds + c(-1, 1) * 0.001 * diff(bounds), 5)
bins <- hist(draws$estimate, breaks = seq(bounds[1], bounds[2], length.out = 41), plot = FALSE)
edges <- bins$breaks - draws$true_att[1]
mean_se <- signif(colMeans(se), 5)
# Keep the HTML's histogram axis scale. All draws enter the histogram.
limit <- round_limit(max(4 * colMeans(se), head(reach, 30)))
peak <- 1.08 * max(bins$density, dnorm(0, sd = mean_se))
ticks <- peak * seq(0.25, 1, by = 0.25)
plot(NA, xlim = c(-limit, limit), ylim = c(0, peak), axes = FALSE,
xlab = "Estimate minus population ATT", ylab = "")
title("Sampling distribution", adj = 0)
mtext("Density", side = 3, adj = 0, line = 0.15, cex = 0.8)
abline(h = ticks, col = "#E8E8E8")
rect(head(edges, -1), 0, tail(edges, -1), bins$density, col = "#CFD2D6", border = "white")
abline(v = 0, lty = 2)
x <- seq(-limit, limit, length.out = 161)
matlines(x, sapply(mean_se, function(s) dnorm(x, sd = s)), col = colors, lty = styles, lwd = 2)
error_ticks(limit)
axis(2, at = ticks, labels = sprintf("%.1f", ticks), lwd = 0)
centered_legend(c("Simulated errors", method), col = c("#CFD2D6", colors),
pch = c(15, NA, NA), lty = c(0, styles), lwd = 2)
limit <- round_limit(max(reach))
par(mfrow = c(2, 1), mar = c(0.5, 3.8, 1.4, 1.2), oma = c(4.5, 0, 1.8, 0),
cex = 1, xaxs = "i", yaxs = "i")
for (j in 1:2) {
plot(NA, xlim = c(-limit, limit), ylim = rev(range(draws$draw) + c(-0.5, 0.5)),
axes = FALSE, xlab = "", ylab = "")
abline(v = c(-limit, -limit / 2, limit / 2, limit), col = "#E8E8E8")
# Use the same gray and blue blends on white as the HTML intervals.
ink <- ifelse(covers[, j], c("#C8C9CC", "#73A3EA")[j], "#000000")
i <- order(ink == "#000000")
segments(error[i] - halfwidth[i, j], draws$draw[i],
error[i] + halfwidth[i, j], draws$draw[i], col = ink[i], lwd = 0.45)
abline(v = 0, lty = 2)
mtext(method[j], side = 3, adj = 0, line = 0.25, font = 2, cex = 0.8)
mtext(sprintf("Coverage: %.1f%%", 100 * mean(covers[, j])), side = 3, adj = 1, line = 0.25, cex = 0.8)
}
mtext("95% confidence intervals", outer = TRUE, side = 3, adj = 0.07, font = 2)
error_ticks(limit)
mtext("Estimate minus population ATT", side = 1, line = 2.5)
centered_legend(c("Population ATT", "Misses the population ATT"), lty = c(2, 1))
}
args <- commandArgs(trailingOnly = TRUE)
sizes <- c(500, 1000, 2000, 5000)
slopes <- (0:100) / 10
full <- identical(args, "all")
stopifnot(full || length(args) %in% c(0, 2))
setting <- if (full || !length(args)) c(2000, 4) else as.numeric(args)
n <- setting[1]
b <- setting[2]
stopifnot(n %in% sizes, b %in% slopes)
if (full) {
grid <- expand.grid(n = sizes, b = slopes)
draws <- do.call(rbind, Map(simulate_setting, grid$n, grid$b))
at_n <- draws[draws$n == n, ]
coverage_curve <- rowsum(1L * covered(at_n), at_n$b) / 1000
selected <- at_n[at_n$b == b, ]
} else {
draws <- selected <- simulate_setting(n, b)
# Archived counts for the full coverage curve when running a single setting.
counts <- matrix(scan(text = "
# n = 500
940 939 939 938 939 940 937 939 940 941 938 941 937 939 936 936 935 936 930 937 926 935 923 934
922 933 919 935 914 936 906 937 905 937 904 937 899 937 895 936 890 935 884 937 879 940 872 940
870 940 866 941 860 943 853 945 846 945 837 945 830 948 828 946 822 948 814 948 809 947 804 946
798 948 790 949 785 946 776 945 772 945 768 946 756 948 745 946 735 946 727 946 719 947 711 948
703 949 696 949 690 948 685 950 680 949 673 949 662 949 652 951 641 952 635 950 626 950 615 950
606 950 601 948 589 948 582 947 579 947 574 946 566 947 563 948 555 948 554 948 551 948 549 948
547 948 544 947 539 948 537 948 532 948 528 947 524 947 520 948 515 948 505 948 502 947 496 949
487 949 483 949 480 951 477 951 469 951 458 951 454 950 449 950 447 950 442 950 437 950 433 951
433 951 429 952 425 952 421 952 418 952
# n = 1000
952 951 953 955 954 954 955 953 953 953 950 953 948 953 948 955 946 954 943 952 940 952 939 952
937 952 934 952 932 951 929 951 925 951 922 950 914 952 913 952 909 949 907 949 899 947 890 946
883 947 880 948 873 947 866 949 857 948 846 951 839 951 834 951 830 955 827 955 818 952 817 953
807 955 799 953 792 953 787 953 782 953 780 953 770 953 767 952 754 952 751 951 742 952 731 950
724 950 720 950 719 950 712 949 706 948 702 947 694 948 688 947 684 947 681 948 680 949 675 949
667 947 659 946 654 946 647 946 641 946 635 947 629 948 625 946 620 946 614 946 605 946 596 947
590 948 580 949 576 950 573 950 567 950 563 949 558 949 555 949 550 948 548 947 544 947 536 946
532 945 531 945 526 946 521 945 518 945 518 945 513 945 511 945 508 944 504 941 500 941 496 941
495 941 493 942 489 942 485 942 482 942
# n = 2000
944 943 944 942 940 941 940 941 939 940 936 939 933 939 930 936 929 942 929 942 928 941 925 942
923 944 920 945 918 948 911 948 910 947 907 945 901 946 898 947 894 946 892 945 890 946 888 947
886 948 876 948 867 946 864 945 856 944 846 944 844 945 838 946 829 948 823 946 817 944 813 944
808 943 805 943 795 940 791 941 781 941 772 943 766 942 761 943 755 943 743 943 734 943 727 942
723 943 717 945 712 945 705 945 699 944 692 944 685 946 679 946 677 949 666 949 664 949 653 948
647 949 640 947 630 947 624 947 617 947 611 947 603 947 598 947 594 947 590 946 584 946 580 946
578 946 571 945 567 944 560 944 558 944 556 944 551 944 545 944 541 943 533 944 526 944 520 946
516 946 511 946 507 947 502 947 496 947 492 947 487 947 481 947 480 947 474 946 472 947 470 947
456 947 453 947 452 947 452 947 450 947
# n = 5000
961 961 958 958 954 954 953 953 953 954 951 953 950 953 948 956 947 955 946 954 944 953 937 953
935 954 931 955 929 956 925 955 925 957 922 956 919 956 916 959 913 957 909 959 903 959 897 958
891 956 884 956 878 957 871 958 866 958 861 956 857 956 848 957 839 958 829 959 825 959 815 958
809 958 805 958 792 957 784 957 771 958 764 958 759 958 751 956 739 956 731 956 723 957 721 958
715 959 707 959 697 959 694 959 687 959 681 959 679 959 674 959 670 959 665 959 659 958 656 959
649 959 644 959 637 959 623 959 618 959 611 959 601 959 593 959 583 958 580 958 575 958 568 958
565 957 560 956 554 956 551 956 547 956 545 956 540 956 534 956 530 956 526 956 519 956 514 956
508 954 504 952 500 952 497 952 493 952 492 953 488 953 483 953 480 953 477 953 471 953 467 953
465 953 462 953 460 954 458 954 457 954
", quiet = TRUE, comment.char = "#"), ncol = 2, byrow = TRUE)
rows <- (match(n, sizes) - 1) * length(slopes) + seq_along(slopes)
coverage_curve <- counts[rows, ] / 1000
}
tag <- sprintf("n%s-b%s", n, b)
write.csv(draws, paste0("ddd-draws-", if (full) "all" else tag, ".csv"), row.names = FALSE)
write_figures(selected, coverage_curve, paste0("ddd-plots-", tag, ".pdf"))