diff --git a/DESCRIPTION b/DESCRIPTION index e287a940..90412768 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: fect Type: Package Title: Fixed Effects Counterfactual Estimators Version: 2.2.0 -Date: 2026-03-21 +Date: 2026-03-20 Authors@R: c(person("Licheng", "Liu", , "lichengl@stanford.edu", role = c("aut")), person("Ziyi", "Liu", , "zyliu2023@berkeley.edu", role = c("aut")), @@ -11,9 +11,12 @@ Authors@R: person("Tianzhu", "Qin", , "tianzhu@stanford.edu", role = c("aut")), person("Shiyun", "Hu", , "hushiyun@pku.edu.cn", role = c("aut")), person("Rivka", "Lipkovitz", , "rivkal@mit.edu", role = c("aut"))) +Author: Licheng Liu [aut], Ziyi Liu [aut], Ye Wang [aut], Yiqing Xu [aut, cre], + Tianzhu Qin [aut], Shiyun Hu [aut], Rivka Lipkovitz [aut] Maintainer: Yiqing Xu Description: Provides tools for estimating causal effects in panel data using counterfactual methods, as well as other modern DID estimators. It is designed for causal panel analysis with binary treatments under the parallel trends assumption. The package supports scenarios where treatments can switch on and off and allows for limited carryover effects. It includes several imputation estimators, such as Gsynth (Xu 2017), linear factor models, and the matrix completion method. Detailed methodology is described in Liu, Wang, and Xu (2024) and Chiu et al. (2025) . Optionally integrates with the "HonestDiDFEct" package for sensitivity analyses compatible with imputation estimators. "HonestDiDFEct" is not on CRAN but can be obtained from . -URL: https://yiqingxu.org/packages/fect/ +URL: https://yiqingxu.org/packages/fect/, https://github.com/xuyiqing/fect +BugReports: https://github.com/xuyiqing/fect/issues NeedsCompilation: yes License: MIT + file LICENSE Imports: @@ -25,14 +28,14 @@ Imports: foreach (>= 1.4.3), abind (>= 1.4-0), codetools, - MASS, + MASS, gridExtra, grid, fixest, doRNG, - future, - mvtnorm, + future, parallelly, + mvtnorm, dplyr, future.apply, reshape2, @@ -49,6 +52,7 @@ Suggests: Depends: R (>= 4.1.0) LinkingTo: Rcpp, RcppArmadillo RoxygenNote: 7.3.2 -Packaged: 2026-03-20 00:00:00 UTC; yiqingxu +Packaged: 2024-01-26 03:25:56 UTC; ziyil Encoding: UTF-8 Config/testthat/edition: 3 +LazyData: true diff --git a/R/boot.R b/R/boot.R index 1647cd07..5a1931b9 100644 --- a/R/boot.R +++ b/R/boot.R @@ -66,6 +66,8 @@ fect_boot <- function( cl = NULL, I, II, + cm=FALSE, + II.cm=NULL, T.on, T.off = NULL, T.on.carry = NULL, @@ -197,6 +199,8 @@ fect_boot <- function( W = W, I = I, II = II, + cm = cm, + II.cm = II.cm, T.on = T.on, T.off = T.off, T.on.carry = T.on.carry, @@ -311,6 +315,8 @@ fect_boot <- function( kappaQ.id = kappaQ.id, I = I, II = II, + cm = cm, + II.cm = II.cm, T.on = T.on, T.off = T.off, T.on.carry = T.on.carry, @@ -643,6 +649,8 @@ fect_boot <- function( W = W, I = I, II = II, + cm = cm, + II.cm = II.cm, T.on = T.on, T.off = T.off, T.on.carry = T.on.carry, @@ -704,7 +712,11 @@ fect_boot <- function( att.carryover.W = NA, balance.avg.att = NA, balance.time = NA, - group.output = list() + group.output = list(), + eff = if (keep.sims) matrix(NA_real_, TT, N) else NULL, + D = if (keep.sims) matrix(NA_real_, TT, N) else NULL, + I = if (keep.sims) matrix(NA_real_, TT, N) else NULL, + boot.id = NULL ) return(boot0) } else { @@ -1024,6 +1036,7 @@ fect_boot <- function( D.boot <- out$D[, id.boot, drop = FALSE] I.boot <- out$I[, id.boot, drop = FALSE] II.boot <- out$II[, id.boot, drop = FALSE] + II.cm.boot <- out$II.cm[, id.boot, drop = FALSE] W.boot <- NULL if (!is.null(W)) { W.boot <- NULL @@ -1103,7 +1116,11 @@ fect_boot <- function( count.off.W = NA, time.off.W = NA, att.carryover.W = NA, - group.output = list() + group.output = list(), + eff = if (keep.sims) matrix(NA_real_, TT, N) else NULL, + D = if (keep.sims) matrix(NA_real_, TT, N) else NULL, + I = if (keep.sims) matrix(NA_real_, TT, N) else NULL, + boot.id = NULL ) return(boot0) } else { @@ -1161,6 +1178,8 @@ fect_boot <- function( W = W, I = I, II = II, + cm = cm, + II.cm = II.cm, T.on = T.on, T.off = T.off, T.on.carry = T.on.carry, @@ -1264,7 +1283,11 @@ fect_boot <- function( count.off.W = NA, time.off.W = NA, att.carryover.W = NA, - group.output = list() + group.output = list(), + eff = if (keep.sims) matrix(NA_real_, TT, N) else NULL, + D = if (keep.sims) matrix(NA_real_, TT, N) else NULL, + I = if (keep.sims) matrix(NA_real_, TT, N) else NULL, + boot.id = NULL ) return(boot0) } else { @@ -1403,7 +1426,11 @@ fect_boot <- function( count.off.W = NA, time.off.W = NA, att.carryover.W = NA, - group.out = list() + group.out = list(), + eff = matrix(NA_real_, TT, length(boot.id)), + D = matrix(NA_real_, TT, length(boot.id)), + I = matrix(NA_real_, TT, length(boot.id)), + boot.id = boot.id ) return(boot0) } else { @@ -1470,6 +1497,8 @@ fect_boot <- function( W = W.boot, I = I.boot, II = II[, boot.id], + cm = cm, + II.cm = II.cm[, boot.id], T.on = T.on[, boot.id], T.off = T.off.boot, T.on.carry = T.on.carry[, boot.id], @@ -1625,7 +1654,11 @@ fect_boot <- function( att.off.W = NA, count.off.W = NA, time.off.W = NA, - att.carryover.W = NA + att.carryover.W = NA, + eff = matrix(NA_real_, TT, length(boot.id)), + D = matrix(NA_real_, TT, length(boot.id)), + I = matrix(NA_real_, TT, length(boot.id)), + boot.id = boot.id ) return(boot0) } else { @@ -2145,6 +2178,12 @@ fect_boot <- function( } } } + if (keep.sims) { + eff.boot <- eff.boot[, , -boot.rm, drop = FALSE] + D.boot <- D.boot[, , -boot.rm, drop = FALSE] + I.boot <- I.boot[, , -boot.rm, drop = FALSE] + colnames.boot <- colnames.boot[-boot.rm] + } } if (dis) { message(dim(att.boot)[2], " runs\n", sep = "") diff --git a/R/cfe.R b/R/cfe.R index 992b0730..5a7deb38 100644 --- a/R/cfe.R +++ b/R/cfe.R @@ -17,6 +17,8 @@ fect_cfe <- function( kappaQ.id = NULL, I, II, + cm=FALSE, + II.cm=NULL, T.on, T.off = NULL, T.on.carry = NULL, diff --git a/R/default.R b/R/default.R index eccbe5bf..844a5607 100644 --- a/R/default.R +++ b/R/default.R @@ -90,7 +90,8 @@ fect <- function( permute = FALSE, ## permutation test m = 2, ## block length normalize = FALSE, # accelerate option - keep.sims = FALSE # keep individual bootstrap/jackknife simulations + keep.sims = FALSE, # keep individual bootstrap/jackknife simulations + cm = FALSE # causal moderation ) { UseMethod("fect") } @@ -163,7 +164,8 @@ fect.formula <- function( permute = FALSE, ## permutation test m = 2, ## block length normalize = FALSE, - keep.sims = FALSE + keep.sims = FALSE, + cm = FALSE ) { ## parsing varnames <- all.vars(formula) @@ -266,7 +268,8 @@ fect.formula <- function( permute = permute, m = m, normalize = normalize, - keep.sims = keep.sims + keep.sims = keep.sims, + cm = cm ) out$call <- match.call() @@ -343,7 +346,8 @@ fect.default <- function( permute = FALSE, ## permutation test m = 2, ## block length normalize = FALSE, - keep.sims = FALSE + keep.sims = FALSE, + cm=FALSE ) { ## -------------------------------## ## Checking Parameters @@ -381,6 +385,11 @@ fect.default <- function( id <- index[1] time <- index[2] + + if (cm == TRUE & ! method %in% c("fe", "ife")) { + stop("\"cm\" option is only available for the \"fe\" and \"ife\" methods.") + } + if (se == 1) { if (!vartype %in% c("bootstrap", "jackknife", "parametric")) { stop("\"vartype\" option misspecified.") @@ -1483,6 +1492,12 @@ fect.default <- function( II <- I II[which(D == 1)] <- 0 ## regard treated values as missing + II.cm <- matrix(0, TT, N) + II.cm[which(D == 1)] <- 1 + II.cm[is.nan(Y.ind)] <- 0 + + # Unbalance Check + ## 1. remove units that have too control status T0 <- apply(II, 2, sum) T0.min <- min(T0) @@ -1571,6 +1586,7 @@ fect.default <- function( I <- as.matrix(I[, -rm.id]) ## after removing I.D <- as.matrix(I.D[, -rm.id]) II <- as.matrix(II[, -rm.id]) + II.cm <- as.matrix(II.cm[, -rm.id]) if (!is.null(group)) { G <- as.matrix(G[, -rm.id]) } @@ -1610,6 +1626,7 @@ fect.default <- function( I <- I[-which(I.use == 0), ] ## remove that period I.D <- I.D[-which(I.use == 0), ] II <- II[-which(I.use == 0), ] ## remove that period + II.cm <- II.cm[-which(I.use == 0), ] ## remove that period D <- D[-which(I.use == 0), ] ## remove that period Y <- Y[-which(I.use == 0), ] ## remove that period @@ -2048,6 +2065,8 @@ fect.default <- function( W = W, I = I, II = II, + cm=cm, + II.cm = II.cm, T.on = T.on, T.off = T.off, r = r, @@ -2251,6 +2270,8 @@ fect.default <- function( W = W, I = I, II = II, + cm = cm, + II.cm = II.cm, T.on = T.on, T.off = T.off, T.on.carry = T.on.carry, diff --git a/R/effect.R b/R/effect.R index 83df4682..88954aad 100644 --- a/R/effect.R +++ b/R/effect.R @@ -82,15 +82,33 @@ effect <- function(x, ## a fect object if (is.null(x$est.avg)) { cat("No uncertainty estimates.") } else { - # Perform bootstrap analysis - nboots <- length(x$att.avg.boot) + # Determine nboots from the 3D arrays + if (!is.null(x$eff.boot) && length(dim(x$eff.boot)) == 3) { + nboots <- dim(x$eff.boot)[3] + } else { + stop("eff.boot is not a valid 3D array. Ensure keep.sims = TRUE in fect().") + } + if (nboots == 0) { + stop("All bootstrap iterations failed. Cannot compute cumulative effects.") + } + + # Validate D.boot and I.boot exist with matching dimensions + has.D.boot <- !is.null(x$D.boot) && length(dim(x$D.boot)) == 3 && dim(x$D.boot)[3] >= nboots + has.I.boot <- !is.null(x$I.boot) && length(dim(x$I.boot)) == 3 && dim(x$I.boot)[3] >= nboots + catt.boot <- matrix(NA, period[2] - period[1] + 1, nboots) # Calculate treatment effect for each bootstrap sample for (i in 1:nboots) { # Extract bootstrap matrices - D.boot <- x$D.boot[, , i] - I.boot <- x$I.boot[, , i] + if (has.D.boot) { + D.boot <- x$D.boot[, , i] + I.boot <- x$I.boot[, , i] + } else { + # Fallback: use original D.dat and I.dat (less accurate but prevents crash) + D.boot <- x$D.dat + I.boot <- x$I.dat + } eff.boot <- x$eff.boot[, , i] # Select treated units in bootstrap sample diff --git a/R/fe.R b/R/fe.R index 7cd24e42..1687e3c3 100644 --- a/R/fe.R +++ b/R/fe.R @@ -7,6 +7,8 @@ fect_fe <- function(Y, # Outcome variable, (T*N) matrix W, I, II, + cm = FALSE, + II.cm = NULL, T.on, T.off = NULL, T.on.carry = NULL, @@ -53,9 +55,6 @@ fect_fe <- function(Y, # Outcome variable, (T*N) matrix X <- array(0, dim = c(1, 1, 0)) } - ## replicate data - YY <- Y - YY[which(II == 0)] <- 0 ## reset to 0 D.c <- apply(D, 2, function(vec) { cumsum(vec) @@ -76,63 +75,87 @@ fect_fe <- function(Y, # Outcome variable, (T*N) matrix data.ini[, (3 + i)] <- c(X[, , i]) } } - ## observed Y0 indicator: - initialOut <- Y0 <- beta0 <- FE0 <- xi0 <- factor0 <- NULL - oci <- which(c(II) == 1) - if (binary == FALSE) { - if (!is.null(W)) { - initialOut <- initialFit(data = data.ini, force = force, w = c(W), oci = oci) - } else { - initialOut <- initialFit(data = data.ini, force = force, w = NULL, oci = oci) - } - Y0 <- initialOut$Y0 - beta0 <- initialOut$beta0 - if (p > 0 && sum(is.na(beta0)) > 0) { - beta0[which(is.na(beta0))] <- 0 - } - - ## ini.res <- initialOut$res - } else { - initialOut <- BiInitialFit(data = data.ini, QR = QR, r = r.cv, force = force, oci = oci) - Y0 <- initialOut$Y0 - beta0 <- initialOut$beta0 - FE0 <- initialOut$FE0 - if (QR == 1) { - xi0 <- initialOut$xi0 - factor0 <- initialOut$factor0 - } - } - - ## -------------------------------## - ## ----------- Main Algorithm ----------- ## - ## -------------------------------## validX <- 1 ## no multi-colinearity - est.fect <- NULL - if (is.null(W)) { - W.use <- as.matrix(0) - } else { - W.use <- W - W.use[which(II == 0)] <- 0 - } + calculate_estimation <- function(data.ini, Y, II, W, binary, QR, force, r.cv, tol, max.iteration, oci_override = NULL) { + ## observed Y0 indicator: + initialOut <- Y0 <- beta0 <- FE0 <- xi0 <- factor0 <- NULL - if (binary == FALSE) { - est.best <- inter_fe_ub(YY, Y0, X, II, W.use, beta0, r.cv, force = force, tol, max.iteration) - if (boot == FALSE) { - if (r.cv == 0) { - est.fect <- est.best + oci <- if (is.null(oci_override)) which(c(II) == 1) else oci_override + if (binary == FALSE) { + if (!is.null(W)) { + initialOut <- initialFit(data = data.ini, force = force, w = c(W), oci = oci) } else { - est.fect <- inter_fe_ub(YY, Y0, X, II, W.use, beta0, 0, force = force, tol, max.iteration) + initialOut <- initialFit(data = data.ini, force = force, w = NULL, oci = oci) + } + Y0 <- initialOut$Y0 + beta0 <- initialOut$beta0 + if (p > 0 && sum(is.na(beta0)) > 0) { + beta0[which(is.na(beta0))] <- 0 + } + + ## ini.res <- initialOut$res + } else { + initialOut <- BiInitialFit(data = data.ini, QR = QR, r = r.cv, force = force, oci = oci) + Y0 <- initialOut$Y0 + beta0 <- initialOut$beta0 + FE0 <- initialOut$FE0 + if (QR == 1) { + xi0 <- initialOut$xi0 + factor0 <- initialOut$factor0 } } - } else { - if (QR == FALSE) { - est.best <- inter_fe_d_ub(YY, Y0, FE0, X, II, r.cv, force, tol = tol) + + ## -------------------------------## + ## ----------- Main Algorithm ----------- ## + ## -------------------------------## + + est.fect <- NULL + + if (is.null(W)) { + W.use <- as.matrix(0) } else { - est.best <- inter_fe_d_qr_ub(YY, Y0, FE0, factor0, xi0, X, II, r.cv, force, tol = tol) + W.use <- W + W.use[which(II == 0)] <- 0 } + + YY <- Y + YY[which(II == 0)] <- 0 ## reset to 0 + + if (binary == FALSE) { + est.best <- inter_fe_ub(YY, Y0, X, II, W.use, beta0, r.cv, force = force, tol, max.iteration) + if (boot == FALSE) { + if (r.cv == 0) { + est.fect <- est.best + } else { + est.fect <- inter_fe_ub(YY, Y0, X, II, W.use, beta0, 0, force = force, tol, max.iteration) + } + } + } else { + if (QR == FALSE) { + est.best <- inter_fe_d_ub(YY, Y0, FE0, X, II, r.cv, force, tol = tol) + } else { + est.best <- inter_fe_d_qr_ub(YY, Y0, FE0, factor0, xi0, X, II, r.cv, force, tol = tol) + } + } + + return(list(est.best = est.best, est.fect = est.fect)) + } + + estimation.D0 <- calculate_estimation(data.ini, Y, II, W, binary, QR, force, r.cv, tol, max.iteration) + est.best <- estimation.D0$est.best + est.fect <- estimation.D0$est.fect + + if (cm == TRUE) { + estimation.D1 <- calculate_estimation( + data.ini, Y, II.cm, W, binary, QR, force, r.cv, tol, max.iteration, + # initialize using all observed outcomes to avoid NA predictions for unseen FE levels + oci_override = which(c(I) == 1) + ) + est.best.cm <- estimation.D1$est.best } + validX <- est.best$validX validF <- ifelse(r.cv > 0, 1, 0) @@ -922,5 +945,10 @@ fect_fe <- function(Y, # Outcome variable, (T*N) matrix group.output = group.output )) } + + if (cm == TRUE) { + out <- c(out, list(est.cm = est.best.cm)) + } + return(out) } ## fe functions ends. diff --git a/R/fect_nevertreated.R b/R/fect_nevertreated.R index 74b48f76..a294d1c8 100644 --- a/R/fect_nevertreated.R +++ b/R/fect_nevertreated.R @@ -4,6 +4,8 @@ fect_nevertreated <- function(Y, # Outcome variable, (T*N) matrix W, I, II, + cm = FALSE, + II.cm = NULL, T.on, T.off = NULL, T.on.carry = NULL, diff --git a/R/fect_sens.R b/R/fect_sens.R index 6b3420ee..174fad39 100644 --- a/R/fect_sens.R +++ b/R/fect_sens.R @@ -49,7 +49,24 @@ fect_sens <- function( # Extract DTE estimates (beta.hat) and var-cov (vcov.hat) beta.hat <- fect.out$est.att[idx, 1] - vcov.hat <- fect.out$att.vcov[idx, idx] + + if (is.matrix(fect.out$att.vcov)) { + vcov.hat <- fect.out$att.vcov[idx, idx] + } else if (is.matrix(fect.out$att.boot)) { + # Fallback: compute vcov from bootstrap samples if att.vcov is unavailable + vcov.hat <- cov(t(fect.out$att.boot[idx, , drop = FALSE]), + use = "pairwise.complete.obs") + if (!is.matrix(vcov.hat)) { + stop("fect_sens requires a valid variance-covariance matrix. ", + "Could not compute one from bootstrap samples. ", + "Please re-run fect() with se = TRUE and sufficient nboots.", + call. = FALSE) + } + } else { + stop("fect_sens requires a valid variance-covariance matrix (att.vcov) from fect(). ", + "Please re-run fect() with se = TRUE and ensure sufficient bootstrap iterations (nboots).", + call. = FALSE) + } # Counts of pre and post periods numPrePeriods <- length(pre.periods) @@ -85,7 +102,7 @@ fect_sens <- function( # ------------------------------------------------------------------- if (!is.null(Mbarvec) && length(Mbarvec) > 0) { # 3a) Weighted-average, across the entire post-treatment window - rm_sens_results <- .honest("createSensitivityResults_relativeMagnitudes")( + rm_sens_results <- suppressWarnings(.honest("createSensitivityResults_relativeMagnitudes")( betahat = beta.hat, sigma = vcov.hat, numPrePeriods = numPrePeriods, @@ -93,16 +110,16 @@ fect_sens <- function( l_vec = w.att, Mbarvec = Mbarvec, parallel = parallel - ) + )) - rm_original_cs <- .honest("constructOriginalCS")( + rm_original_cs <- suppressWarnings(.honest("constructOriginalCS")( betahat = beta.hat, sigma = vcov.hat, numPrePeriods = numPrePeriods, numPostPeriods = numPostPeriods, l_vec = w.att - ) + )) } if (!is.null(periodMbarvec) && length(periodMbarvec) > 0) { # 3b) Period-by-period robust confidence sets @@ -118,7 +135,7 @@ fect_sens <- function( # For each t_i, we run createSensitivityResults_relativeMagnitudes # across all Mbar in Mbarvec - honest.dte <- .honest("createSensitivityResults_relativeMagnitudes")( + honest.dte <- suppressWarnings(.honest("createSensitivityResults_relativeMagnitudes")( betahat = beta.hat, sigma = vcov.hat, numPrePeriods = numPrePeriods, @@ -126,7 +143,7 @@ fect_sens <- function( l_vec = dte_l, Mbarvec = periodMbarvec, parallel = parallel - ) + )) # Convert to data.frame # The returned object typically has columns lb, ub, Mbar, etc. @@ -153,7 +170,7 @@ fect_sens <- function( if (!is.null(Mvec) && length(Mvec) > 0) { # 4a) Weighted-average analysis - smooth_sens_results <- .honest("createSensitivityResults")( + smooth_sens_results <- suppressWarnings(.honest("createSensitivityResults")( betahat = beta.hat, sigma = vcov.hat, numPrePeriods = numPrePeriods, @@ -162,15 +179,15 @@ fect_sens <- function( l_vec = w.att, Mvec = Mvec, parallel = parallel - ) + )) - sm_original_cs <- .honest("constructOriginalCS")( + sm_original_cs <- suppressWarnings(.honest("constructOriginalCS")( betahat = beta.hat, sigma = vcov.hat, numPrePeriods = numPrePeriods, numPostPeriods = numPostPeriods, l_vec = w.att - ) + )) } if (!is.null(periodMvec) && length(periodMvec) > 0) { # 4b) Period-by-period robust confidence sets @@ -181,7 +198,7 @@ fect_sens <- function( dte_l <- rep(0, numPostPeriods) dte_l[t_i] <- 1 - honest.dte <- .honest("createSensitivityResults")( + honest.dte <- suppressWarnings(.honest("createSensitivityResults")( betahat = beta.hat, sigma = vcov.hat, numPrePeriods = numPrePeriods, @@ -190,7 +207,7 @@ fect_sens <- function( l_vec = dte_l, Mvec = periodMvec, parallel = parallel - ) + )) honest.dte <- as.data.frame(honest.dte) honest.dte$postPeriod <- post.periods[t_i] diff --git a/R/plot.R b/R/plot.R index a5a7684b..fd1c2e3f 100644 --- a/R/plot.R +++ b/R/plot.R @@ -1,3 +1,5 @@ +utils::globalVariables(c("fit", "group", "y_hat", "y_hat_lower", "y_hat_upper")) + ## new plot # x: a fect object # type of the plot; axes limits; axes labels; @@ -12,6 +14,7 @@ plot.fect <- function( highlight = NULL, ## for carryover test and placebo test plot.ci = NULL, ## "0.9", "0.95", "none" show.points = TRUE, + loess.fit = TRUE, show.group = NULL, bound = NULL, # "none", "min", "equiv", "both" show.count = TRUE, @@ -108,6 +111,12 @@ plot.fect <- function( status.background.color = NULL, covariate = NULL, covariate.labels = NULL, + covariate.value = NULL, + covariate.value.range = FALSE, + relative.time = FALSE, + pretreatment = FALSE, + num.pretreatment = 3, + cm = FALSE, ...) { rbind_fill_fect_plot <- function(...) { @@ -168,6 +177,7 @@ plot.fect <- function( warning("'count' is deprecated. Use 'show.count'.", call. = FALSE) if (is.logical(count) && missing(show.count)) show.count <- count } + use_loess <- isTRUE(loess.fit) if (is.null(preset)) { if (is.null(connected)) connected <- FALSE if (is.null(ltype)) ltype <- c("solid", "solid") @@ -885,6 +895,13 @@ plot.fect <- function( } if (type == "calendar") { + if (!use_loess) { + x$eff.calendar.fit <- NULL + x$est.eff.calendar.fit <- NULL + x$eff.calendar.fit.group <- NULL + x$est.eff.calendar.fit.group <- NULL + } + calendar_att_avg <- x$att.avg stats <- "none" } @@ -958,10 +975,11 @@ plot.fect <- function( maintext <- "Carryover Effects" } } else if (type == "calendar") { - maintext <- "CATT by Calendar Time" + maintext <- if (isTRUE(relative.time)) "CATT by Relative Time" else "CATT by Calendar Time" ytitle <- paste("Effect on", x$Y) } else if (type == "heterogeneous") { - maintext <- paste("CATT by", covariate) + covariate_label <- if (is.character(covariate) && length(covariate) == 1 && covariate != "") covariate else "M" + maintext <- paste("CATT by", covariate_label) ytitle <- paste("Effect on", x$Y) } else if (type == "box") { maintext <- "Individual Treatment Effects" @@ -2377,6 +2395,10 @@ plot.fect <- function( ylab <- NULL } + + + + @@ -2597,6 +2619,8 @@ plot.fect <- function( # height of the histogram + att.avg.use <- x$att.avg + if (CI == FALSE) { message("Uncertainty estimates not available.\n") if (length(ylim) != 0) { @@ -3107,6 +3131,354 @@ plot.fect <- function( } if (type == "calendar") { + # If relative.time = TRUE, aggregate effects by event time (x$T.on) rather than calendar time. + # This overrides the default calendar-time aggregation. + if (isTRUE(relative.time)) { + mod_keep <- NULL + if (!is.null(covariate.value)) { + if (!is.character(covariate) || length(covariate) != 1 || covariate == "") { + stop("Please provide a single covariate name via `covariate` for calendar plots.\n") + } + if (!covariate %in% x$X) { + stop("`covariate` must be one of `x$X` for calendar plots.\n") + } + X.arr <- NULL + X.cands <- x[names(x) == "X"] + if (length(X.cands) >= 2) { + X.arr <- X.cands[2]$X + } else if (is.array(x$X)) { + X.arr <- x$X + } + if (is.null(X.arr) || length(dim(X.arr)) != 3) { + stop("Cannot locate covariate array in `x` for calendar plots.\n") + } + + mod.idx <- which(x$X == covariate)[1] + M <- X.arr[, , mod.idx] + + if (isTRUE(covariate.value.range)) { + if (!is.numeric(covariate.value) || length(covariate.value) != 2) { + stop("`covariate.value` must be numeric length 2 when `covariate.value.range = TRUE`.\n") + } + mod_keep <- M >= min(covariate.value) & M <= max(covariate.value) + } else { + mod_keep <- M %in% covariate.value + } + } + + if (is.null(x$T.on) || !is.matrix(x$T.on)) { + stop("`relative.time = TRUE` requires `x$T.on` (event-time matrix) in the fect object.\n") + } + if (is.null(mod_keep)) { + mod_keep <- matrix(TRUE, nrow = nrow(x$T.on), ncol = ncol(x$T.on)) + } + if (!all(dim(mod_keep) == dim(x$T.on))) { + stop("Dimension mismatch: cannot apply `covariate.value` filter to `x$T.on`.\n") + } + if (is.null(x$D.dat) || !all(dim(x$D.dat) == dim(x$T.on))) { + stop("`relative.time = TRUE` requires `x$D.dat` aligned with `x$T.on` for calendar plots.\n") + } + + keep_mask <- mod_keep & !is.na(x$T.on) & !is.na(x$eff) + rel_vec <- as.numeric(x$T.on[keep_mask]) + eff_vec <- as.numeric(x$eff[keep_mask]) + d_vec <- as.numeric(x$D.dat[keep_mask]) + + ok <- is.finite(rel_vec) & is.finite(eff_vec) & !is.na(d_vec) + rel_vec <- rel_vec[ok] + eff_vec <- eff_vec[ok] + d_vec <- d_vec[ok] + + if (length(eff_vec) == 0) { + stop("No observations are available for `relative.time = TRUE` under the requested filter.\n") + } + + rel_vals <- sort(unique(rel_vec)) + eff.calendar <- vapply(rel_vals, function(k) mean(eff_vec[rel_vec == k], na.rm = TRUE), numeric(1)) + N.calendar <- vapply(rel_vals, function(k) sum(!is.na(eff_vec[rel_vec == k])), numeric(1)) + se.calendar <- vapply(rel_vals, function(k) { + v <- eff_vec[rel_vec == k] + v <- v[!is.na(v)] + n <- length(v) + if (n <= 1) return(NA_real_) + stats::sd(v) / sqrt(n) + }, numeric(1)) + + if (sum(N.calendar, na.rm = TRUE) == 0 || all(is.na(eff.calendar))) { + stop("No observations match the requested filter under `relative.time = TRUE`.\n") + } + + treated_mask <- d_vec > 0 + control_mask <- d_vec == 0 + + rel_vals_tr <- sort(unique(rel_vec[treated_mask])) + rel_vals_ct <- sort(unique(rel_vec[control_mask])) + + eff_tr <- vapply(rel_vals_tr, function(k) mean(eff_vec[treated_mask & rel_vec == k], na.rm = TRUE), numeric(1)) + eff_ct <- vapply(rel_vals_ct, function(k) mean(eff_vec[control_mask & rel_vec == k], na.rm = TRUE), numeric(1)) + + N_tr <- vapply(rel_vals_tr, function(k) sum(!is.na(eff_vec[treated_mask & rel_vec == k])), numeric(1)) + N_ct <- vapply(rel_vals_ct, function(k) sum(!is.na(eff_vec[control_mask & rel_vec == k])), numeric(1)) + + x$eff.calendar.fit.group <- NULL + x$est.eff.calendar.fit.group <- NULL + eff.calendar.fit <- NULL + se.fit <- NULL + N.fit <- NULL + fit_group_df <- NULL + + if (use_loess) { + fit_group <- function(rel_vals_g, eff_g, N_g) { + if (length(eff_g) <= 1) { + return(list(fit = eff_g, se = rep(NA_real_, length(eff_g)))) + } + loess_model <- suppressWarnings(try(loess(eff_g ~ rel_vals_g, weights = N_g), silent = TRUE)) + if ("try-error" %in% class(loess_model)) { + return(list(fit = eff_g, se = rep(NA_real_, length(eff_g)))) + } + pred.fit <- stats::predict(loess_model, newdata = rel_vals_g, se = TRUE) + list(fit = as.numeric(pred.fit$fit), se = as.numeric(pred.fit$se.fit)) + } + + eff.calendar.fit <- rep(NA_real_, length(rel_vals)) + se.fit <- rep(NA_real_, length(rel_vals)) + N.fit <- rep(NA_real_, length(rel_vals)) + + if (length(rel_vals_tr) > 0) { + fit_tr <- fit_group(rel_vals_tr, eff_tr, N_tr) + idx_tr <- match(rel_vals_tr, rel_vals) + eff.calendar.fit[idx_tr] <- fit_tr$fit + se.fit[idx_tr] <- fit_tr$se + N.fit[idx_tr] <- N_tr + } + + if (length(rel_vals_ct) > 0) { + fit_ct <- fit_group(rel_vals_ct, eff_ct, N_ct) + idx_ct <- match(rel_vals_ct, rel_vals) + eff.calendar.fit[idx_ct] <- fit_ct$fit + se.fit[idx_ct] <- fit_ct$se + N.fit[idx_ct] <- N_ct + } + } + + calendar_time <- rel_vals + + df.calendar <- pmax(N.calendar - 1, 1) + ci.level <- if (!is.null(plot.ci) && plot.ci %in% c("0.9", "90")) 0.9 else 0.95 + crit.calendar <- stats::qt(1 - (1 - ci.level) / 2, df.calendar) + ci.lower <- eff.calendar - crit.calendar * se.calendar + ci.upper <- eff.calendar + crit.calendar * se.calendar + if (use_loess) { + df.fit <- pmax(N.fit - 1, 1) + crit.fit <- stats::qt(1 - (1 - ci.level) / 2, df.fit) + ci.fit.lower <- eff.calendar.fit - crit.fit * se.fit + ci.fit.upper <- eff.calendar.fit + crit.fit * se.fit + + make_fit_group_df <- function(rel_vals_g, fit_g, se_g, N_g, group_label) { + if (length(rel_vals_g) == 0) { + return(NULL) + } + df_g <- pmax(N_g - 1, 1) + crit_g <- stats::qt(1 - (1 - ci.level) / 2, df_g) + data.frame( + time = rel_vals_g, + fit = fit_g, + se = se_g, + ci.lower = fit_g - crit_g * se_g, + ci.upper = fit_g + crit_g * se_g, + count = N_g, + group = group_label + ) + } + + fit_group_df <- rbind( + make_fit_group_df( + rel_vals_tr, + if (length(rel_vals_tr) > 0) fit_tr$fit else numeric(0), + if (length(rel_vals_tr) > 0) fit_tr$se else numeric(0), + N_tr, + "treated" + ), + make_fit_group_df( + rel_vals_ct, + if (length(rel_vals_ct) > 0) fit_ct$fit else numeric(0), + if (length(rel_vals_ct) > 0) fit_ct$se else numeric(0), + N_ct, + "control" + ) + ) + if (!is.null(fit_group_df) && nrow(fit_group_df) > 0) { + x$eff.calendar.fit.group <- fit_group_df[, c("time", "fit", "count", "group")] + x$est.eff.calendar.fit.group <- fit_group_df + } + } else { + x$eff.calendar.fit.group <- NULL + x$est.eff.calendar.fit.group <- NULL + } + + x$eff.calendar <- cbind(eff.calendar, count = N.calendar) + if (use_loess) { + x$eff.calendar.fit <- cbind(eff.calendar.fit, count = N.calendar) + } else { + x$eff.calendar.fit <- NULL + } + rownames(x$eff.calendar) <- as.character(calendar_time) + if (use_loess) { + rownames(x$eff.calendar.fit) <- as.character(calendar_time) + } + x$N.calendar <- N.calendar + + x$est.eff.calendar <- cbind( + "ATT-calendar" = eff.calendar, + "S.E." = se.calendar, + "CI.lower" = ci.lower, + "CI.upper" = ci.upper, + "count" = N.calendar + ) + if (use_loess) { + x$est.eff.calendar.fit <- cbind( + "ATT-calendar Fitted" = eff.calendar.fit, + "S.E." = se.fit, + "CI.lower" = ci.fit.lower, + "CI.upper" = ci.fit.upper, + "count" = N.calendar + ) + } else { + x$est.eff.calendar.fit <- NULL + } + rownames(x$est.eff.calendar) <- as.character(calendar_time) + if (use_loess) { + rownames(x$est.eff.calendar.fit) <- as.character(calendar_time) + } + + if (sum(N.calendar, na.rm = TRUE) > 0) { + calendar_att_avg <- sum(eff.calendar * N.calendar, na.rm = TRUE) / sum(N.calendar[!is.na(eff.calendar)], na.rm = TRUE) + } + } + + if (!isTRUE(relative.time) && !is.null(covariate.value)) { + if (!is.character(covariate) || length(covariate) != 1 || covariate == "") { + stop("Please provide a single covariate name via `covariate` for calendar plots.\n") + } + if (!covariate %in% x$X) { + stop("`covariate` must be one of `x$X` for calendar plots.\n") + } + X.arr <- NULL + X.cands <- x[names(x) == "X"] + if (length(X.cands) >= 2) { + X.arr <- X.cands[2]$X + } else if (is.array(x$X)) { + X.arr <- x$X + } + if (is.null(X.arr) || length(dim(X.arr)) != 3) { + stop("Cannot locate covariate array in `x` for calendar plots.\n") + } + + mod.idx <- which(x$X == covariate)[1] + M <- X.arr[, , mod.idx] + + if (isTRUE(covariate.value.range)) { + if (!is.numeric(covariate.value) || length(covariate.value) != 2) { + stop("`covariate.value` must be numeric length 2 when `covariate.value.range = TRUE`.\n") + } + mod.keep <- M >= min(covariate.value) & M <= max(covariate.value) + } else { + mod.keep <- M %in% covariate.value + } + + D.sub <- x$D.dat + D.sub[which(D.sub == 0)] <- NA + D.sub[!mod.keep] <- NA + + eff.calendar <- apply(x$eff * D.sub, 1, mean, na.rm = TRUE) + N.calendar <- apply(!is.na(x$eff * D.sub), 1, sum) + if (sum(N.calendar, na.rm = TRUE) == 0 || all(is.na(eff.calendar))) { + stop("No treated observations match the requested `covariate.value` filter.\n") + } + T.calendar <- seq_len(dim(D.sub)[1]) + + eff.calendar.fit <- NULL + se.fit <- NULL + if (use_loess) { + if (sum(!is.na(eff.calendar)) > 1) { + loess_model <- suppressWarnings(try(loess(eff.calendar ~ T.calendar, weights = N.calendar), silent = TRUE)) + if ("try-error" %in% class(loess_model)) { + eff.calendar.fit <- eff.calendar + se.fit <- rep(NA_real_, length(eff.calendar)) + } else { + pred.fit <- stats::predict(loess_model, newdata = T.calendar, se = TRUE) + eff.calendar.fit <- as.numeric(pred.fit$fit) + se.fit <- as.numeric(pred.fit$se.fit) + } + } else { + eff.calendar.fit <- eff.calendar + se.fit <- rep(NA_real_, length(eff.calendar)) + } + } + + calendar_time <- NULL + if (!is.null(x$rawtime)) { + calendar_time <- x$rawtime + } else { + calendar_time <- T.calendar + } + + x$eff.calendar <- cbind(eff.calendar, count = N.calendar) + if (use_loess) { + x$eff.calendar.fit <- cbind(eff.calendar.fit, count = N.calendar) + } else { + x$eff.calendar.fit <- NULL + } + rownames(x$eff.calendar) <- as.character(calendar_time) + if (use_loess) { + rownames(x$eff.calendar.fit) <- as.character(calendar_time) + } + x$N.calendar <- N.calendar + + se.calendar <- apply(x$eff * D.sub, 1, function(v) { + v <- v[!is.na(v)] + n <- length(v) + if (n <= 1) return(NA_real_) + stats::sd(v) / sqrt(n) + }) + df.calendar <- pmax(N.calendar - 1, 1) + ci.level <- if (!is.null(plot.ci) && plot.ci %in% c("0.9", "90")) 0.9 else 0.95 + crit.calendar <- stats::qt(1 - (1 - ci.level) / 2, df.calendar) + ci.lower <- eff.calendar - crit.calendar * se.calendar + ci.upper <- eff.calendar + crit.calendar * se.calendar + if (use_loess) { + ci.fit.lower <- eff.calendar.fit - crit.calendar * se.fit + ci.fit.upper <- eff.calendar.fit + crit.calendar * se.fit + } + + x$est.eff.calendar <- cbind( + "ATT-calendar" = eff.calendar, + "S.E." = se.calendar, + "CI.lower" = ci.lower, + "CI.upper" = ci.upper, + "count" = N.calendar + ) + if (use_loess) { + x$est.eff.calendar.fit <- cbind( + "ATT-calendar Fitted" = eff.calendar.fit, + "S.E." = se.fit, + "CI.lower" = ci.fit.lower, + "CI.upper" = ci.fit.upper, + "count" = N.calendar + ) + } else { + x$est.eff.calendar.fit <- NULL + } + rownames(x$est.eff.calendar) <- as.character(calendar_time) + if (use_loess) { + rownames(x$est.eff.calendar.fit) <- as.character(calendar_time) + } + + if (sum(N.calendar, na.rm = TRUE) > 0) { + calendar_att_avg <- sum(eff.calendar * N.calendar, na.rm = TRUE) / sum(N.calendar[!is.na(eff.calendar)], na.rm = TRUE) + } + } + CI <- NULL if (is.null(x$est.eff.calendar) == TRUE) { CI <- FALSE @@ -3114,15 +3486,24 @@ plot.fect <- function( CI <- TRUE } if (!is.null(provided_xlim)) { - x$est.eff.calendar <- x$est.eff.calendar[which(rownames(x$est.eff.calendar) >= min(provided_xlim) & rownames(x$est.eff.calendar) <= max(provided_xlim)), ] - x$est.eff.calendar.fit <- x$est.eff.calendar.fit[which(rownames(x$est.eff.calendar.fit) >= min(provided_xlim) & rownames(x$est.eff.calendar.fit) <= max(provided_xlim)), ] + # IMPORTANT: rownames are character; comparing them to numeric xlim will do + # lexicographic comparisons and can drop valid points (e.g., "-2" > "-10"). + # Convert to numeric event/calenadar time before filtering. + tt_cal <- suppressWarnings(as.numeric(rownames(x$est.eff.calendar))) + keep_cal <- which(is.finite(tt_cal) & tt_cal >= min(provided_xlim) & tt_cal <= max(provided_xlim)) + x$est.eff.calendar <- x$est.eff.calendar[keep_cal, , drop = FALSE] + if (use_loess && !is.null(x$est.eff.calendar.fit)) { + tt_fit <- suppressWarnings(as.numeric(rownames(x$est.eff.calendar.fit))) + keep_fit <- which(is.finite(tt_fit) & tt_fit >= min(provided_xlim) & tt_fit <= max(provided_xlim)) + x$est.eff.calendar.fit <- x$est.eff.calendar.fit[keep_fit, , drop = FALSE] + } } if (plot.ci == "none") { CI <- FALSE } ## axes labels if (is.null(xlab) == TRUE) { - xlab <- "Calendar Time" + xlab <- if (isTRUE(relative.time)) "Time Since the Treatment's Onset" else "Calendar Time" } else if (xlab == "") { xlab <- NULL } @@ -3133,39 +3514,51 @@ plot.fect <- function( ylab <- NULL } + att.avg.use <- calendar_att_avg if (CI == FALSE) { message("Uncertainty estimates not available.\n") data.1 <- x$eff.calendar - data.2 <- x$eff.calendar.fit + has_fit <- use_loess && !is.null(x$eff.calendar.fit) + data.2 <- if (has_fit) x$eff.calendar.fit else NULL if (length(ylim) != 0) { rect.length <- (ylim[2] - ylim[1]) / 5 rect.min <- ylim[1] } else { - rect.length <- (max(c(data.1, data.2), na.rm = TRUE) - min(c(data.1, data.2), na.rm = TRUE)) / 2 - rect.min <- min(c(data.1, data.2), na.rm = TRUE) - rect.length + if (has_fit) { + rect.length <- (max(c(data.1, data.2), na.rm = TRUE) - min(c(data.1, data.2), na.rm = TRUE)) / 2 + rect.min <- min(c(data.1, data.2), na.rm = TRUE) - rect.length + } else { + rect.length <- (max(data.1, na.rm = TRUE) - min(data.1, na.rm = TRUE)) / 2 + rect.min <- min(data.1, na.rm = TRUE) - rect.length + } } d1 <- data.1 <- as.matrix(x$eff.calendar[which(!is.na(x$eff.calendar[, 1])), ]) - d2 <- data.2 <- as.matrix(x$eff.calendar.fit[which(!is.na(x$eff.calendar.fit[, 1])), ]) + if (has_fit) { + d2 <- data.2 <- as.matrix(x$eff.calendar.fit[which(!is.na(x$eff.calendar.fit[, 1])), ]) + } if (dim(d1)[2] == 1) { d1 <- data.1 <- t(d1) - rownames(d1) <- rownames(data.1) <- rownames(x$eff.calendar)[which(!is.na(x$est.eff.calendar[, 1]))] + rownames(d1) <- rownames(data.1) <- rownames(x$eff.calendar)[which(!is.na(x$eff.calendar[, 1]))] } - if (dim(d2)[2] == 1) { + if (has_fit && dim(d2)[2] == 1) { d2 <- data.2 <- t(d2) - rownames(d2) <- rownames(data.2) <- rownames(x$eff.calendar.fit)[which(!is.na(x$est.eff.calendar.fit[, 1]))] + rownames(d2) <- rownames(data.2) <- rownames(x$eff.calendar.fit)[which(!is.na(x$eff.calendar.fit[, 1]))] } } else { if (is.null(x$est.eff.calendar)) { stop("Uncertainty estimates not available.\n") } d1 <- data.1 <- as.matrix(x$est.eff.calendar[which(!is.na(x$est.eff.calendar[, 1])), ]) - d2 <- data.2 <- as.matrix(x$est.eff.calendar.fit[which(!is.na(x$est.eff.calendar.fit[, 1])), ]) + has_fit <- use_loess && !is.null(x$est.eff.calendar.fit) + if (has_fit) { + d2 <- data.2 <- as.matrix(x$est.eff.calendar.fit[which(!is.na(x$est.eff.calendar.fit[, 1])), ]) + } if (dim(d1)[2] == 1) { d1 <- data.1 <- t(d1) rownames(d1) <- rownames(data.1) <- rownames(x$est.eff.calendar)[which(!is.na(x$est.eff.calendar[, 1]))] } - if (dim(d2)[2] == 1) { + if (has_fit && dim(d2)[2] == 1) { d2 <- data.2 <- t(d2) rownames(d2) <- rownames(data.2) <- rownames(x$est.eff.calendar.fit)[which(!is.na(x$est.eff.calendar.fit[, 1]))] } @@ -3174,8 +3567,13 @@ plot.fect <- function( rect.length <- (ylim[2] - ylim[1]) / 5 rect.min <- ylim[1] } else { - rect.length <- (max(c(data.1[, 4], data.2[, 4]), na.rm = TRUE) - min(c(data.1[, 3], data.2[, 3]), na.rm = TRUE)) / 2 - rect.min <- min(c(data.1[, 3], data.2[, 3]), na.rm = TRUE) - rect.length + if (has_fit) { + rect.length <- (max(c(data.1[, 4], data.2[, 4]), na.rm = TRUE) - min(c(data.1[, 3], data.2[, 3]), na.rm = TRUE)) / 2 + rect.min <- min(c(data.1[, 3], data.2[, 3]), na.rm = TRUE) - rect.length + } else { + rect.length <- (max(data.1[, 4], na.rm = TRUE) - min(data.1[, 3], na.rm = TRUE)) / 2 + rect.min <- min(data.1[, 3], na.rm = TRUE) - rect.length + } } } p <- ggplot() @@ -3196,44 +3594,118 @@ plot.fect <- function( p <- p + geom_hline(yintercept = 0, colour = lcolor[1], linewidth = lwidth[1], linetype = ltype[1]) TTT <- as.numeric(rownames(data.1)) - TTT.2 <- as.numeric(rownames(data.2)) + if (has_fit) { + TTT.2 <- as.numeric(rownames(data.2)) + } if (CI == FALSE) { - p <- p + geom_hline(yintercept = x$att.avg, color = calendar.lcolor, linewidth = 0.8, linetype = "dashed") - p <- p + geom_line(aes(x = TTT.2, y = d2[, 1]), color = calendar.color, linewidth = 1.1) + p <- p + geom_hline(yintercept = att.avg.use, color = calendar.lcolor, linewidth = 0.8, linetype = "dashed") + if (has_fit) { + if (isTRUE(relative.time) && !is.null(x$eff.calendar.fit.group)) { + fit_data <- x$eff.calendar.fit.group + p <- p + geom_line( + data = fit_data, + aes(x = time, y = fit, group = group), + color = calendar.color, + linewidth = 1.1 + ) + } else { + p <- p + geom_line(aes(x = TTT.2, y = d2[, 1]), color = calendar.color, linewidth = 1.1) + } + } p <- p + geom_point(aes(x = TTT, y = d1[, 1]), color = "gray50", fill = "gray50", alpha = 1, size = 1.2) } else { - p <- p + geom_ribbon(aes(x = TTT.2, ymin = d2[, 3], ymax = d2[, 4]), color = calendar.cicolor, fill = calendar.cicolor, alpha = 0.5, size = 0) - p <- p + geom_hline(yintercept = x$att.avg, color = calendar.lcolor, linewidth = 0.8, linetype = "dashed") - p <- p + geom_line(aes(x = TTT.2, y = d2[, 1]), color = calendar.color, linewidth = 1.1) + if (has_fit) { + if (isTRUE(relative.time) && !is.null(x$est.eff.calendar.fit.group)) { + fit_data <- x$est.eff.calendar.fit.group + p <- p + geom_ribbon( + data = fit_data, + aes(x = time, ymin = ci.lower, ymax = ci.upper, group = group), + color = calendar.cicolor, + fill = calendar.cicolor, + alpha = 0.5, + linewidth = 0 + ) + p <- p + geom_hline(yintercept = att.avg.use, color = calendar.lcolor, linewidth = 0.8, linetype = "dashed") + p <- p + geom_line( + data = fit_data, + aes(x = time, y = fit, group = group), + color = calendar.color, + linewidth = 1.1 + ) + } else { + p <- p + geom_ribbon(aes(x = TTT.2, ymin = d2[, 3], ymax = d2[, 4]), color = calendar.cicolor, fill = calendar.cicolor, alpha = 0.5, linewidth = 0) + p <- p + geom_hline(yintercept = att.avg.use, color = calendar.lcolor, linewidth = 0.8, linetype = "dashed") + p <- p + geom_line(aes(x = TTT.2, y = d2[, 1]), color = calendar.color, linewidth = 1.1) + } + } else { + p <- p + geom_hline(yintercept = att.avg.use, color = calendar.lcolor, linewidth = 0.8, linetype = "dashed") + } p <- p + geom_pointrange(aes(x = TTT, y = d1[, 1], ymin = d1[, 3], ymax = d1[, 4]), color = "gray50", fill = "gray50", alpha = 1, size = 0.6) } - if (show.count == TRUE & !(type == "gap" | type == "equiv")) { + if (isTRUE(show.count) && !type %in% c("gap", "equiv")) { + # Keep the count bars as a small "indicator band" at the very bottom of the plot, + # instead of letting them consume the main y-range. + current_plot_yrange <- NULL + if (!is.null(ylim) && length(ylim) == 2) { + current_plot_yrange <- ylim + } else { + gb <- ggplot_build(p) + current_plot_yrange <- gb$layout$panel_scales_y[[1]]$range$range + } + + count_bar_space_prop <- 0.15 + count_bar_space_height <- (current_plot_yrange[2] - current_plot_yrange[1]) * count_bar_space_prop + actual_rect_length <- count_bar_space_height * 0.8 + rect_min_val <- if (!is.null(ylim) && length(ylim) == 2) ylim[1] else current_plot_yrange[1] - count_bar_space_height + T.start <- c() T.end <- c() ymin <- c() ymax <- c() T.gap <- (max(TTT) - min(TTT)) / length(TTT) - for (i in c(1:dim(d1)[1])) { + for (i in seq_len(nrow(d1))) { T.start <- c(T.start, TTT[i] - 0.25 * T.gap) T.end <- c(T.end, TTT[i] + 0.25 * T.gap) - ymin <- c(ymin, rect.min) - ymax <- c(ymax, rect.min + rect.length * d1[i, "count"] / max(d1[, "count"])) + ymin <- c(ymin, rect_min_val) + ymax <- c(ymax, rect_min_val + actual_rect_length * d1[i, "count"] / max(d1[, "count"], na.rm = TRUE)) } data.toplot <- cbind.data.frame( xmin = T.start, xmax = T.end, ymin = ymin, - ymax = ymax + ymax = ymax, + count = d1[, "count"] + ) + max_idx <- which.max(data.toplot$count) + max_count_pos <- (data.toplot$xmin[max_idx] + data.toplot$xmax[max_idx]) / 2 + + p <- p + geom_rect( + aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), + data = data.toplot, + inherit.aes = FALSE, + fill = count.color, + alpha = count.alpha, + color = count.outline.color, + linewidth = 0.3 ) - max.count.pos <- mean(TTT[which.max(d1[, "count"])]) - p <- p + geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), data = data.toplot, fill = count.color, size = 0.3, alpha = count.alpha, color = count.outline.color, linewidth = 0.2) p <- p + annotate("text", - x = max.count.pos - 0.02 * T.gap, - y = max(data.toplot$ymax) + 0.2 * rect.length, - label = max(x$N.calendar), size = cex.text * 0.8, hjust = 0.5 + x = max_count_pos, + y = data.toplot$ymax[max_idx] + 0.12 * count_bar_space_height, + label = max(data.toplot$count, na.rm = TRUE), + size = cex.text * 0.8, + hjust = 0.5, + vjust = 0 ) + + # If ylim was not user-specified, extend the plot downward to make room for the indicator band. + # IMPORTANT: don't add coord_cartesian() here; we'll apply xlim/ylim once below. + if (is.null(ylim) || length(ylim) == 0) { + final_yrange_min <- min(current_plot_yrange[1], rect_min_val) + final_yrange_max <- current_plot_yrange[2] + ylim <- c(final_yrange_min, final_yrange_max) + } } ## title @@ -3243,9 +3715,12 @@ plot.fect <- function( p <- p + ggtitle(main) } - ## ylim - if (is.null(ylim) == FALSE) { - p <- p + coord_cartesian(ylim = ylim) + ## axis limits + # NOTE: In ggplot2, adding coord_cartesian() multiple times will replace the + # previous coordinate system. So we must set xlim/ylim together to avoid + # xlim overriding ylim (or vice versa). + if (is.null(ylim) == FALSE || is.null(xlim) == FALSE) { + p <- p + coord_cartesian(xlim = xlim, ylim = ylim) } if (length(TTT) <= 10) { @@ -3266,10 +3741,7 @@ plot.fect <- function( # } # } # } - ## xlim - if (is.null(xlim) == FALSE) { - p <- p + coord_cartesian(xlim = xlim) - } + # xlim handled together with ylim above p <- p + theme( @@ -3282,7 +3754,8 @@ plot.fect <- function( axis.text = element_text(color = "black", size = cex.axis), axis.text.x = element_text(size = cex.axis, angle = angle, hjust = x.h, vjust = x.v), axis.text.y = element_text(size = cex.axis), - plot.title = element_text(size = cex.main, hjust = 0.5, face = "bold", margin = margin(10, 0, 10, 0)) + plot.title = element_text(size = cex.main, hjust = 0.5, face = "bold", margin = margin(10, 0, 10, 0)), + plot.margin = margin(15, 5.5, 5.5, 5.5, "pt") ) if (isTRUE(return.data)) { @@ -3307,16 +3780,26 @@ plot.fect <- function( if (type == "heterogeneous") { Xs <- x[names(x) == "X"][[2]] - if ((is.null(covariate) == TRUE) && ((dim(Xs) > 2) && (dim(Xs)[3] > 1))) { + covariate_is_custom <- is.matrix(covariate) || is.data.frame(covariate) || + (is.numeric(covariate) && length(covariate) == TT * N) + covariate_name <- if (is.character(covariate) && length(covariate) == 1 && covariate != "") covariate else NULL + + if (!covariate_is_custom && is.null(covariate_name) && ((dim(Xs) > 2) && (dim(Xs)[3] > 1))) { stop("Please provide a covariate to plot heterogeneous effects.\n") } - if ((is.null(covariate) == FALSE) && (!covariate %in% x$X)) { + if (!covariate_is_custom && !is.null(covariate_name) && (!covariate_name %in% x$X)) { stop("Please provide a valid covariate to plot heterogeneous effects.\n") } + if (!covariate_is_custom && is.null(covariate_name)) { + covariate_name <- x$X[1] + } + if (!covariate_is_custom && !is.null(covariate_name)) { + maintext <- paste("CATT by", covariate_name) + } if (is.null(xlab) == TRUE) { - xlab <- covariate + xlab <- if (!is.null(covariate_name)) covariate_name else "M" } else if (xlab == "") { xlab <- NULL } @@ -3330,16 +3813,124 @@ plot.fect <- function( D.missing <- x$D.dat D.missing[which(D == 0)] <- NA D.missing.vec <- as.vector(D.missing) + t.on.vec <- as.vector(x$T.on) - eff.vec <- as.vector(x$eff) - X.vec <- as.vector(x[names(x) == "X"][2]$X[, , which(x$X == covariate)]) - - eff.vec <- eff.vec[which(!is.na(D.missing.vec))] - X.vec <- X.vec[which(!is.na(D.missing.vec))] + if (isTRUE(pretreatment)) { + if (!is.numeric(num.pretreatment) || length(num.pretreatment) != 1 || is.na(num.pretreatment)) { + stop("`num.pretreatment` must be a single positive integer when `pretreatment = TRUE`.\n") + } + num.pretreatment.int <- as.integer(num.pretreatment) + if (num.pretreatment.int < 1) { + stop("`num.pretreatment` must be a single positive integer when `pretreatment = TRUE`.\n") + } + # Use the last `num.pretreatment` event-time periods up to 0: {-(K-1), ..., -1, 0} + lower_bound <- -(num.pretreatment.int - 1) + keep.pos <- which(!is.na(t.on.vec) & t.on.vec >= lower_bound & t.on.vec <= 0) + if (length(keep.pos) == 0) { + stop("No pretreatment units are available for heterogeneous effects.\n") + } + } else { + keep.pos <- which(!is.na(D.missing.vec)) + } + if (covariate_is_custom) { + if (is.data.frame(covariate)) { + covariate <- as.matrix(covariate) + } + if (is.matrix(covariate)) { + if (!all(dim(covariate) == c(TT, N))) { + stop("Custom covariate must have dimension T x N to match the fect object.\n") + } + X.vec <- as.vector(covariate) + } else { + if (length(covariate) != TT * N) { + stop("Custom covariate must have length T*N to match the fect object.\n") + } + X.vec <- as.vector(covariate) + } + } else { + X.vec <- as.vector(x[names(x) == "X"][2]$X[, , which(x$X == covariate_name)]) + } + X.vec <- X.vec[keep.pos] j <- order(X.vec) - eff.vec <- eff.vec[j] X.vec <- X.vec[j] + + if (cm == FALSE) { + eff.vec <- as.vector(x$eff) + eff.vec <- eff.vec[keep.pos] + eff.vec <- eff.vec[j] + } else { + if (is.null(x$est.cm)) { + stop("Cannot compute heterogeneous effects with cm=TRUE: `est.cm` not found in the fect object.\n", + "Please run `fect(..., cm=TRUE)`.\n") + } + if (covariate_is_custom) { + stop("Custom covariate is not supported with `cm = TRUE`.\n") + } + + # Treated cell indices in vectorized (column-major) order + tr.pos <- keep.pos + + # Full covariate array (T x N x p) stored as the 2nd `X` entry + X.full <- x[names(x) == "X"][2]$X + cov.idx <- which(x$X == covariate_name)[1] + if (length(cov.idx) == 0) { + stop("Cannot find the requested covariate in x$X.\n") + } + + calc_g <- function(est.obj, X.arr) { + TT <- dim(X.arr)[1] + N <- dim(X.arr)[2] + p <- dim(X.arr)[3] + + mu <- if (!is.null(est.obj$mu)) as.numeric(est.obj$mu) else 0 + g <- matrix(mu, TT, N) + + if (!is.null(est.obj$alpha)) { + alpha <- as.numeric(est.obj$alpha) + g <- g + matrix(rep(alpha, each = TT), TT, N) + } + if (!is.null(est.obj$xi)) { + xi <- as.numeric(est.obj$xi) + g <- g + matrix(rep(xi, times = N), TT, N) + } + + if (p > 0 && !is.null(est.obj$beta)) { + beta <- as.numeric(est.obj$beta) + for (kk in seq_len(p)) { + if (!is.na(beta[kk])) { + g <- g + X.arr[, , kk] * beta[kk] + } + } + } + return(g) + } + + # Compute \hat{theta}(m) for each unique m (= xv) by plugging m into the + # selected covariate slice, then averaging g1-g0 over treated cells. + X.unique <- unique(X.vec) + theta.unique <- sapply(X.unique, function(xv) { + X.mod <- X.full + X.mod[, , cov.idx] <- xv + + g1 <- calc_g(x$est.cm, X.mod) + g0 <- calc_g(x$est, X.mod) + diff.vec <- as.vector(g1 - g0)[tr.pos] + + mean(diff.vec, na.rm = TRUE) + }) + + # Map back to observation-level vector aligned with sorted X.vec + eff.vec <- as.numeric(theta.unique[match(X.vec, X.unique)]) + } + + if (isTRUE(pretreatment)) { + att.avg.use <- mean(eff.vec, na.rm = TRUE) + } else { + att.avg.use <- x$att.avg + } + + # print(eff.vec) # debug if (length(unique(X.vec)) <= 4) { p <- ggplot() @@ -3385,7 +3976,7 @@ plot.fect <- function( ## core geoms (even spacing because x is factor) p <- p + geom_hline(yintercept = 0, colour = lcolor[1], linewidth = lwidth[1], linetype = ltype[1]) - p <- p + geom_hline(yintercept = x$att.avg, color = heterogeneous.lcolor, linewidth = 0.8, linetype = "dashed") + p <- p + geom_hline(yintercept = att.avg.use, color = heterogeneous.lcolor, linewidth = 0.8, linetype = "dashed") # nicer CI + mean: thick error bars + solid point p <- p + geom_linerange( aes(x = x, ymin = .data$lower, ymax = .data$upper), @@ -3425,10 +4016,13 @@ plot.fect <- function( count = counts ) p <- p + geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), - data = data_counts, fill = count.color, color = count.outline.color, alpha = count.alpha, linewidth = 0.2 + data = data_counts, inherit.aes = FALSE, + fill = count.color, color = count.outline.color, alpha = count.alpha, linewidth = 0.2 ) p <- p + geom_text(aes(x = .data$xcenter, y = .data$ymax + 0.12 * count_bar_space_height, label = .data$count), - data = data_counts, size = cex.text * 0.85, hjust = 0.5, vjust = 0.5, color = "#444444") + data = data_counts, inherit.aes = FALSE, + size = cex.text * 0.85, hjust = 0.5, vjust = 0.5, color = "#444444" + ) if (!is.null(covariate.labels)) { p <- p + scale_x_discrete(labels = covariate.labels) } else { @@ -3436,22 +4030,36 @@ plot.fect <- function( } } - if (is.null(ylim) == FALSE) { - p <- p + coord_cartesian(ylim = ylim) + # NOTE: In ggplot2, adding coord_cartesian() multiple times will replace the + # previous coordinate system. So we must set xlim/ylim together to avoid + # xlim overriding ylim (or vice versa). + if (is.null(ylim) == FALSE || is.null(xlim) == FALSE) { + p <- p + coord_cartesian(xlim = xlim, ylim = ylim) } - if (is.null(xlim) == FALSE) { - p <- p + coord_cartesian(xlim = xlim) + } else { + # Continuous covariate: smooth with loess and show 95% CI ribbon. + # NOTE: predict(loess, se=TRUE) returns `se.fit` (not `se`). + # Also guard against NA/Inf values in the covariate/effects. + df_hte <- cbind.data.frame(X.vec = X.vec, eff.vec = eff.vec) + df_hte <- df_hte[is.finite(df_hte$X.vec) & is.finite(df_hte$eff.vec), , drop = FALSE] + if (nrow(df_hte) < 2) { + stop("Not enough non-missing observations to plot heterogeneous effects for the requested covariate.\n") } - } else { - plx <- predict(loess(eff.vec ~ X.vec), se = T) - se <- stats::qt(0.975, plx$df) * plx$se - y_hat <- plx$fit - y_hat_lower <- y_hat - se - y_hat_upper <- y_hat + se + if (use_loess) { + lo_fit <- stats::loess(eff.vec ~ X.vec, data = df_hte) + plx <- stats::predict(lo_fit, newdata = df_hte$X.vec, se = TRUE) + se <- stats::qt(0.975, plx$df) * plx$se.fit + df_hte$y_hat <- as.numeric(plx$fit) + df_hte$y_hat_lower <- df_hte$y_hat - se + df_hte$y_hat_upper <- df_hte$y_hat + se + } - p <- ggplot() + # Keep X.vec aligned for the histogram/count overlay below + X.vec <- df_hte$X.vec + + p <- ggplot(df_hte, aes(x = .data$X.vec)) ## xlab and ylab p <- p + xlab(xlab) + ylab(ylab) @@ -3466,9 +4074,13 @@ plot.fect <- function( } p <- p + geom_hline(yintercept = 0, colour = lcolor[1], linewidth = lwidth[1], linetype = ltype[1]) - p <- p + geom_ribbon(aes(x = X.vec, ymin = y_hat_lower, ymax = y_hat_upper), color = heterogeneous.cicolor, fill = heterogeneous.cicolor, alpha = 0.5, size = 0) - p <- p + geom_hline(yintercept = x$att.avg, color = heterogeneous.lcolor, linewidth = 0.8, linetype = "dashed") - p <- p + geom_line(aes(x = X.vec, y = y_hat), color = heterogeneous.color, linewidth = 1.1) + p <- p + geom_hline(yintercept = att.avg.use, color = heterogeneous.lcolor, linewidth = 0.8, linetype = "dashed") + if (use_loess) { + p <- p + geom_ribbon(aes(x = X.vec, ymin = y_hat_lower, ymax = y_hat_upper), color = heterogeneous.cicolor, fill = heterogeneous.cicolor, alpha = 0.5, linewidth = 0) + p <- p + geom_line(aes(x = X.vec, y = y_hat), color = heterogeneous.color, linewidth = 1.1) + } else if (isTRUE(show.points)) { + p <- p + geom_point(aes(y = .data$eff.vec), color = heterogeneous.color, alpha = 0.7, size = 1.2) + } ## title if (is.null(main) == TRUE) { @@ -3477,13 +4089,9 @@ plot.fect <- function( p <- p + ggtitle(main) } - ## ylim - if (is.null(ylim) == FALSE) { - p <- p + coord_cartesian(ylim = ylim) - } - - if (is.null(xlim) == FALSE) { - p <- p + coord_cartesian(xlim = xlim) + ## axis limits (set together; see note above) + if (is.null(ylim) == FALSE || is.null(xlim) == FALSE) { + p <- p + coord_cartesian(xlim = xlim, ylim = ylim) } if (show.count == TRUE) { @@ -3519,7 +4127,8 @@ plot.fect <- function( max_idx <- which.max(counts) max_count_pos <- (bin_xmin[max_idx] + bin_xmax[max_idx]) / 2 p <- p + geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), - data = data.toplot, fill = count.color, size = 0.3, alpha = count.alpha, color = count.outline.color, linewidth = 0.2 + data = data.toplot, inherit.aes = FALSE, + fill = count.color, linewidth = 0.3, alpha = count.alpha, color = count.outline.color ) p <- p + annotate("text", x = max_count_pos, @@ -3529,7 +4138,8 @@ plot.fect <- function( if (is.null(ylim)) { final_yrange_min <- min(current_plot_yrange[1], rect_min_val) final_yrange_max <- current_plot_yrange[2] - p <- p + coord_cartesian(ylim = c(final_yrange_min, final_yrange_max)) + # Preserve xlim if it was supplied; otherwise the new coord would drop it. + p <- p + coord_cartesian(xlim = xlim, ylim = c(final_yrange_min, final_yrange_max)) } } } @@ -3545,7 +4155,8 @@ plot.fect <- function( axis.text = element_text(color = "black", size = cex.axis), axis.text.x = element_text(size = cex.axis, angle = angle, hjust = x.h, vjust = x.v), axis.text.y = element_text(size = cex.axis), - plot.title = element_text(size = cex.main, hjust = 0.5, face = "bold", margin = margin(10, 0, 10, 0)) + plot.title = element_text(size = cex.main, hjust = 0.5, face = "bold", margin = margin(10, 0, 10, 0)), + plot.margin = margin(15, 5.5, 5.5, 5.5, "pt") ) if (isTRUE(return.data)) { @@ -3742,7 +4353,10 @@ plot.fect <- function( ymax = ymax ) max.count.pos <- data.count[which.max(data.count[, 2]), 1][1] - min(data.count[, 1]) + 1 - p <- p + geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), data = data.toplot, fill = count.color, size = 0.3, alpha = count.alpha, color = count.outline.color, linewidth = 0.2) + p <- p + geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), + data = data.toplot, inherit.aes = FALSE, + fill = count.color, size = 0.3, alpha = count.alpha, color = count.outline.color, linewidth = 0.2 + ) p <- p + annotate("text", x = max.count.pos - 0.02 * T.gap, y = max(data.toplot$ymax) + 0.1 * rect.length, @@ -3765,7 +4379,8 @@ plot.fect <- function( axis.text = element_text(color = "black", size = cex.axis), axis.text.x = element_text(size = cex.axis, angle = angle, hjust = x.h, vjust = x.v), axis.text.y = element_text(size = cex.axis), - plot.title = element_text(size = cex.main, hjust = 0.5, face = "bold", margin = margin(10, 0, 10, 0)) + plot.title = element_text(size = cex.main, hjust = 0.5, face = "bold", margin = margin(10, 0, 10, 0)), + plot.margin = margin(15, 5.5, 5.5, 5.5, "pt") ) if (is.null(xticklabels) == FALSE) { diff --git a/data/fect.RData b/data/fect.RData deleted file mode 100644 index 3360fd22..00000000 Binary files a/data/fect.RData and /dev/null differ diff --git a/man/fect.Rd b/man/fect.Rd index 6f4ac1eb..95655911 100644 --- a/man/fect.Rd +++ b/man/fect.Rd @@ -29,7 +29,9 @@ assumptions.} balance.period = NULL, fill.missing = FALSE, placeboTest = FALSE, placebo.period = NULL, carryoverTest = FALSE, carryover.period = NULL, carryover.rm = NULL, - loo = FALSE, permute = FALSE, m = 2, normalize = FALSE, keep.sims = FALSE)} + loo = FALSE, permute = FALSE, m = 2, + normalize = FALSE, keep.sims = FALSE, + cm = FALSE)} \arguments{ \item{formula}{an object of class "formula": a symbolic description of the model to be fitted, e.g, Y~D+X1+X2} \item{data}{a data frame, can be a balanced or unbalanced panel data.} @@ -103,6 +105,7 @@ assumptions.} \item{m}{an integer specifying the block length for permutation tests. Default \code{2}.} \item{normalize}{a logical flag indicating whether to scale outcome and covariates.} \item{keep.sims}{a logical flag indicating whether to save unit-time level bootstrap effects. Default \code{keep.sims = FALSE}. If \code{se = FALSE}, this argument is ignored.} +\item{cm}{a logical flag indicating whether to enable causal moderation analysis. When \code{TRUE}, the estimator decomposes the treatment effect into effect modification and causal moderation components. Currently available for \code{method = "fe"} and \code{method = "ife"}. Default is \code{FALSE}.} } \details{ \code{fect} implements counterfactual estimators for TSCS data. It first imputes counterfactuals by fitting an outcome model using untreated observations, then estimates the individual treatment effect as the difference between observed and predicted outcomes. Finally, it computes average treatment effects on the treated (ATT) and period-specific ATTs. Placebo and equivalence tests help evaluate identification assumptions. diff --git a/man/plot.fect.Rd b/man/plot.fect.Rd index dbef95d0..0878703e 100644 --- a/man/plot.fect.Rd +++ b/man/plot.fect.Rd @@ -14,6 +14,7 @@ highlight = NULL, plot.ci = NULL, show.points = TRUE, + loess.fit = TRUE, show.group = NULL, bound = NULL, show.count = TRUE, @@ -110,6 +111,12 @@ status.background.color = NULL, covariate = NULL, covariate.labels = NULL, + covariate.value = NULL, + covariate.value.range = FALSE, + relative.time = FALSE, + pretreatment = FALSE, + num.pretreatment = 3, + cm = FALSE, ... ) } @@ -222,6 +229,13 @@ \item{status.background.color}{Character string or \code{NULL} (default). If \code{NULL}, uses a default background color for status plots (e.g., \code{"gray90"} for the base style). The \code{preset} argument may define a different color. Specifying this argument directly overrides any \code{preset} or base style setting.} \item{covariate}{Character string or \code{NULL} (default). If \code{NULL}, uses a default covariate for heterogeneous plots (e.g., \code{"X"} for the base style). The \code{preset} argument may define a different covariate. Specifying this argument directly overrides any \code{preset} or base style setting.} \item{covariate.labels}{Character vector or \code{NULL} (default). If \code{NULL}, uses a default covariate labels for heterogeneous plots (e.g., \code{"X1"} for the base style). The \code{preset} argument may define a different covariate labels. Specifying this argument directly overrides any \code{preset} or base style setting.} + \item{covariate.value}{Numeric or \code{NULL} (default); a specific covariate value at which to evaluate the HTE plot. If \code{NULL}, the full range is used.} + \item{covariate.value.range}{Logical; if \code{TRUE}, plots HTE over the range of covariate values. Default is \code{FALSE}.} + \item{relative.time}{Logical; if \code{TRUE}, uses relative time (time since treatment) on the x-axis for HTE plots. Default is \code{FALSE}.} + \item{pretreatment}{Logical; if \code{TRUE}, restricts HTE analysis to pre-treatment periods only. Default is \code{FALSE}.} + \item{num.pretreatment}{Integer; number of pre-treatment periods to include when \code{pretreatment = TRUE}. Default is \code{3}.} + \item{cm}{Logical; if \code{TRUE}, plots causal moderation estimates instead of effect modification estimates for HTE plots. Requires the \code{fect} object to have been estimated with \code{cm = TRUE}. Default is \code{FALSE}.} + \item{loess.fit}{Logical; if \code{TRUE} (default), overlays a LOESS smoothing curve on HTE scatter plots.} \item{...}{Additional graphical parameters passed to internal plotting routines, primarily those accepted by \code{esplot} for event study style plots (gap, equiv, exit, sens_es, cumul).} } \details{ diff --git a/src/cfe_sub.cpp b/src/cfe_sub.cpp index 09150c43..7d8f4dad 100644 --- a/src/cfe_sub.cpp +++ b/src/cfe_sub.cpp @@ -285,6 +285,7 @@ List cfe_iter(const arma::cube& XX, const arma::mat& xxinv, double dif4 = 1.0; double dif5 = 1.0; int niter = 0; + int overall_converged_count = 0; int validF = 1; int use_weight; int r_burnin; @@ -499,6 +500,7 @@ List cfe_iter(const arma::cube& XX, const arma::mat& xxinv, dif2_tol = dif2_tol || (dif2 > tolerate); } + dif3_tol = 0.0; for (int k = 0; k < p_kappa; ++k) { dif3 = arma::norm(fit3.slice(k) - fit3_old.slice(k), "fro") / arma::norm(fit3_old.slice(k), "fro"); @@ -508,6 +510,16 @@ List cfe_iter(const arma::cube& XX, const arma::mat& xxinv, dif4 = arma::norm(fit4 - fit4_old, "fro") / arma::norm(fit4_old, "fro"); dif5 = arma::norm(fit5 - fit5_old, "fro") / arma::norm(fit5_old, "fro"); + if (dif < tolerate && dif1 < tolerate) { + overall_converged_count++; + } else { + overall_converged_count = 0; + } + + if (overall_converged_count >= 3) { + break; + } + fit_old = fit; fit1_old = fit1; fit2_old = fit2; diff --git a/tests/testthat/test-book-claims.R b/tests/testthat/test-book-claims.R index 23c500c5..3dd8998b 100644 --- a/tests/testthat/test-book-claims.R +++ b/tests/testthat/test-book-claims.R @@ -446,7 +446,7 @@ test_that("F3: mc with CV selects lambda", { test_that("G1: CFE with unit-specific linear time trend (Q.type)", { skip_on_cran() - d <- make_panel(N = 40, TT = 20, T0 = 12, Ntr = 12, seed = 8080) + d <- make_panel(N = 200, TT = 20, T0 = 12, Ntr = 60, seed = 8080) out <- fect(Y ~ D + X1, data = d, index = c("id", "time"), method = "cfe", r = 0, se = FALSE, CV = FALSE, Q.type = "linear") diff --git a/tests/testthat/test-cumu-effect-esplot.R b/tests/testthat/test-cumu-effect-esplot.R index 1249b321..59fa2d36 100644 --- a/tests/testthat/test-cumu-effect-esplot.R +++ b/tests/testthat/test-cumu-effect-esplot.R @@ -1,4 +1,5 @@ test_that("cumu/att.cumu/esplot run without error on fect output", { + skip_on_cran() suppressWarnings(try(data("simdata", package = "fect"), silent = TRUE)) expect_true(exists("simdata")) out <- fect::fect( diff --git a/tests/testthat/test-did-wrapper.R b/tests/testthat/test-did-wrapper.R index 5f4e9cb4..3b8bb4ca 100644 --- a/tests/testthat/test-did-wrapper.R +++ b/tests/testthat/test-did-wrapper.R @@ -1,4 +1,5 @@ test_that("did_wrapper twfe runs and returns structure", { + skip_on_cran() skip_if_not_installed("fixest") suppressWarnings(try(data("simdata", package = "fect"), silent = TRUE)) expect_true(exists("simdata")) diff --git a/tests/testthat/test-fect-sens.R b/tests/testthat/test-fect-sens.R index 08a09384..89c8edc3 100644 --- a/tests/testthat/test-fect-sens.R +++ b/tests/testthat/test-fect-sens.R @@ -1,4 +1,5 @@ test_that("fect_sens attaches sensitivity results when inputs present", { + skip_on_cran() skip_if_not_installed("HonestDiDFEct") suppressWarnings(try(data("simdata", package = "fect"), silent = TRUE)) expect_true(exists("simdata")) diff --git a/tests/testthat/test-getcohort.R b/tests/testthat/test-getcohort.R index 813f649a..5e2268d2 100644 --- a/tests/testthat/test-getcohort.R +++ b/tests/testthat/test-getcohort.R @@ -1,4 +1,5 @@ test_that("get.cohort adds cohort columns and handles options", { + skip_on_cran() suppressWarnings(try(data("simdata", package = "fect"), silent = TRUE)) expect_true(exists("simdata")) df <- simdata diff --git a/tests/testthat/test-gsynth-parametric-group-att-align.R b/tests/testthat/test-gsynth-parametric-group-att-align.R deleted file mode 100644 index 55981a87..00000000 --- a/tests/testthat/test-gsynth-parametric-group-att-align.R +++ /dev/null @@ -1,49 +0,0 @@ -test_that("gsynth parametric bootstrap aligns group (unit-level) results with boot resampling", { - suppressWarnings(try(data("simgsynth", package = "fect"), silent = TRUE)) - skip_if_not(exists("simgsynth"), "Dataset 'simgsynth' not available") - - set.seed(123) - out <- fect::fect( - Y ~ D, - data = simgsynth, - index = c("id", "time"), - method = "gsynth", - force = "two-way", - CV = FALSE, - r = 2, - se = TRUE, - vartype = "parametric", - nboots = 50, - keep.sims = TRUE, - min.T0 = 2, - group = "id", - parallel = FALSE - ) - - expect_true(!is.null(out$est.group.att)) - expect_true(is.matrix(out$est.group.att)) - - # Determine ever-treated units from the stored D matrix - Dmat <- out$D.dat - ever_treated <- colSums(Dmat, na.rm = TRUE) > 0 - ids <- colnames(Dmat) - - # Row names should correspond to unit identifiers when group="id" - est <- out$est.group.att - rn <- rownames(est) - # Some workflows may coerce ids; at minimum, all D ids should be represented - expect_true(all(ids %in% rn)) - - never_ids <- ids[!ever_treated] - if (length(never_ids) > 0) { - never_rows <- est[never_ids, , drop = FALSE] - # Never-treated units should have NA for ATT and all uncertainty metrics - expect_true(all(is.na(never_rows[, "ATT"]))) - expect_true(all(is.na(never_rows[, "S.E."]))) - expect_true(all(is.na(never_rows[, "CI.lower"]))) - expect_true(all(is.na(never_rows[, "CI.upper"]))) - expect_true(all(is.na(never_rows[, "p.value"]))) - } -}) - - diff --git a/tests/testthat/test-plot-refactor.R b/tests/testthat/test-plot-refactor.R index a8a16dce..24868788 100644 --- a/tests/testthat/test-plot-refactor.R +++ b/tests/testthat/test-plot-refactor.R @@ -8,7 +8,7 @@ out_fect <- NULL setup_once <- function() { if (is.null(out_fect)) { set.seed(9001) - data(fect, package = "fect") + data(simdata, package = "fect") out_fect <<- fect::fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), method = "fe", se = TRUE, nboots = 30, parallel = FALSE) @@ -182,7 +182,7 @@ test_that("D3: plot.fect count.color uses 'gray' not 'grey'", { test_that("E1: placebo test plot with pre/post colors", { skip_on_cran() - data(fect, package = "fect") + data(simdata, package = "fect") out_p <- suppressWarnings(fect::fect( Y ~ D, data = simdata, index = c("id", "time"), method = "fe", se = TRUE, nboots = 30, parallel = FALSE, @@ -230,7 +230,7 @@ test_that("F2: all-post data (only.post = TRUE)", { test_that("F3: esplot errors on fect object without se", { skip_on_cran() - data(fect, package = "fect") + data(simdata, package = "fect") out_nose <- fect::fect(Y ~ D, data = simdata, index = c("id", "time"), method = "fe", se = FALSE, CV = FALSE) expect_error(esplot(out_nose)) diff --git a/vignettes/.Rprofile b/vignettes/.Rprofile new file mode 100644 index 00000000..51278652 --- /dev/null +++ b/vignettes/.Rprofile @@ -0,0 +1 @@ +options(repos = c(CRAN = "https://cloud.r-project.org")) diff --git a/vignettes/01-start.Rmd b/vignettes/01-start.Rmd index 742d35cd..d82e13e9 100644 --- a/vignettes/01-start.Rmd +++ b/vignettes/01-start.Rmd @@ -2,7 +2,11 @@ This chapter provides installation instructions and introduces the datasets used in the tutorial. -```{r echo = FALSE} +```{r .common, include = FALSE} +source("_common.R") +``` + +```{r clear-environment, echo = FALSE} rm(list = ls()) ``` @@ -10,37 +14,37 @@ rm(list = ls()) To install **fect** from CRAN, run the code chunk below: -```{r eval = FALSE, message = FALSE, warning = FALSE} +```{r install-cran, eval = FALSE, message = FALSE, warning = FALSE} install.packages("fect") ``` We recommend users to install the most up-to-date, stable version of **fect** from Github using: -```{r eval = FALSE, message = FALSE, warning = FALSE, cache = FALSE,} +```{r install-github-main, eval = FALSE, message = FALSE, warning = FALSE, cache = FALSE,} devtools::install_github("xuyiqing/fect") ``` We fix bugs in the `dev` branch before merging into the main branch; therefore, it is often more up to date. -```{r eval = FALSE, message = FALSE, warning = FALSE, cache = FALSE,} +```{r install-github-dev, eval = FALSE, message = FALSE, warning = FALSE, cache = FALSE,} devtools::install_github("xuyiqing/fect@dev") ``` After installation, check **fect** version to make sure the package is up-to-date. -```{r} +```{r check-version} installed.packages()["fect", "Version"] ``` **panelView** for panel data visualization is highly recommended and will be used in the tutorial: -```{r eval=FALSE} +```{r install-panelview, eval=FALSE} devtools::install_github('xuyiqing/panelView') ``` **fect** depends on the following packages, which should be installed automatically when **fect** is being installed. You can also install them manually. -```{r eval=FALSE} +```{r install-dependencies, eval=FALSE} install_all <- function(packages) { installed_pkgs <- installed.packages()[, "Package"] for (pkg in packages) { @@ -57,17 +61,45 @@ install_all(packages) ## Datasets -The **fect** package ships five datasets. - -```{r message = FALSE, warning = FALSE} -library(fect) -data(fect) +The **fect** package ships several datasets. With `LazyData: true`, all datasets become available once the package is loaded. + +```{r load-data, message = FALSE, warning = FALSE} +data(simdata) +data(sim_base) +data(sim_gsynth) +data(sim_linear) +data(sim_trend) +data(sim_region) +data(hh2019) +data(gs2020) +data(turnout) ls() ``` -Below is a brief explanation of the datasets used in this book: +### Simulated datasets + +The package includes five simulated panel datasets. The first two (`simdata` and `sim_base`) are generated from the data-generating process (DGP) in @LWX2022. Both have $N = 200$ units and $T = 35$ time periods. Treatment switches on and off over time (99 of 150 treated units experience at least one reversal), reflecting a general treatment pattern rather than simple staggered adoption. The remaining three (`sim_trend`, `sim_region`, `sim_linear`) are block DID designs used to demonstrate CFE model components. + +The full DGP for `simdata` is: $$Y_{it} = \tau_{it} D_{it} + X_{1,it} + 3 X_{2,it} + \mu + 3\alpha_i + \xi_t + \lambda_i' f_t + \varepsilon_{it}$$ where $\alpha_i \sim N(0,1)$ are unit fixed effects, $\xi_t$ follows an AR(1) process with drift (time fixed effects), $X_{1,it}$ and $X_{2,it} \sim N(0,1)$ are observed covariates with coefficients 1 and 3, $\lambda_i \in \mathbb{R}^2$ are unit-specific factor loadings drawn from $N(0.5, 1)$, $f_t \in \mathbb{R}^2$ are latent time factors (one trending, one white noise), and $\varepsilon_{it} \sim N(0,2)$. The treatment effect is heterogeneous, i.e., $\tau_{it} \sim N(0.4 \cdot \text{tr\_cum}_{it}/T,\; 0.2)$, where $\text{tr\_cum}_{it}$ counts cumulative treatment periods. The grand mean is $\mu = 5$. + +Treatment assignment is correlated with unobservables: the latent index $D^*_{it}$ depends on the factor component $5 \lambda_i' f_t$, the unit fixed effect $2\alpha_i$, the time fixed effect $2\xi_t$, and an AR(1)-like persistence term $5 D_{i,t-1}$, passed through a logistic link. Units with larger factor loadings and fixed effects are more likely to be treated, creating confounding that correlates treatment with unobserved heterogeneity. This is why the FE estimator is biased when factors are present---the parallel trends assumption fails because treated units systematically differ in their factor loadings. + +- `simdata`: The main simulated dataset. The outcome includes two latent factors ($r = 2$), so the parallel trends assumption is violated and the FE estimator is biased. Because treatment assignment loads on the same factors and fixed effects that enter the outcome---units with larger $\lambda_i$ and $\alpha_i$ are more likely to be treated---the confounding is structural and cannot be removed by two-way fixed effects alone. Used in [Chapter @sec-ife-mc] and [Chapter @sec-cfe] to demonstrate factor-augmented approaches. + +- `sim_base`: A simplified version of `simdata` in which the latent factor contributions ($\lambda_i' f_t$) are removed from the outcome. The parallel trends assumption holds, and the FE estimator is consistent. Treatment assignment, covariates, fixed effects, and errors are identical to `simdata`---treatment still correlates with the factor loadings and fixed effects, but this no longer causes bias because the factors do not enter the outcome. Used in [Chapter @sec-fect] to demonstrate the imputation estimator. + +- `sim_trend`: A block DID dataset with unit-specific sinusoidal time trends. $N = 200$ units ($80$ treated, $120$ control), $T = 50$ periods, treatment starts at period 41 for all treated units. The DGP is: $$Y_{it} = \alpha_i + \xi_t + \kappa_i \sin(2\pi t / 2T) + \tau D_{it} + \varepsilon_{it}$$ where $\kappa_i \sim U(0.5, 1.0)$ for treated units and $\kappa_i \sim U(0.125, 0.375)$ for controls. Treatment is deterministic (block assignment), but confounding arises because treated units load more heavily on the sinusoidal time trend---the correlation between $\kappa_i$ and $D_i$ violates parallel trends. Used in [Chapter @sec-cfe] to demonstrate nonlinear unit-specific time trends with B-splines. + +- `sim_linear`: A block DID dataset with unit-specific linear time trends. Same structure as `sim_trend` ($N = 200$, $T = 50$, $T_0 = 41$) but the trend is $\kappa_i \cdot t/T$ rather than sinusoidal. Treated units have slopes $\kappa_i \sim U(2, 4)$, controls have $\kappa_i \sim U(0, 0.5)$. Used in [Chapter @sec-cfe] to demonstrate `Q.type = "linear"`. + +- `sim_region`: An unbalanced panel with region-specific time effects. $N = 500$ units in 5 regions, $T = 20$ periods. The DGP is: $$Y_{it}^{0} = \alpha_i + \xi_t + \delta_{g(i),t} + \varepsilon_{it}$$ where $\delta_{g(i),t}$ are region-specific linear time trends. Treatment probability and timing depend on region, and units in higher-numbered regions enter the panel later. Used in [Chapter @sec-cfe] to demonstrate additional fixed effects in the CFE estimator. + +- `sim_gsynth`: A simulated dataset with no treatment reversal, based on @Xu2017. Used in [Chapter @sec-gsynth] to demonstrate the never-treated estimation regime (generalized synthetic control). + +The scripts that generate simulated datasets are in `data-raw/`. + +### Empirical datasets -- `simdata`: Based on @LWX2022. Used in [Chapter @sec-fect] to demonstrate various counterfactual estimators.\ -- `gs2020`: Based on @GS2020, who examine the effect of minority candidate presence on the proportion of coethnic donations in U.S. House elections. Used in [Chapter @sec-plots] and [Chapter @sec-panel].\ -- `simgsynth` & `turnout`: Based on @Xu2017. Used in [Chapter @sec-gsynth] to demonstrate Gsynth. -- `hh2019`: Based on @HH2019, who study the effect of indirect versus direct democracy on naturalization rates in Switzerland. Used in [Chapter @sec-plots], [Chapter @sec-panel] and [Chapter @sec-panel-sens]. +- `turnout`: Based on @Xu2017. Used in [Chapter @sec-gsynth] alongside `sim_gsynth`. +- `gs2020`: Based on @GS2020, who examine the effect of minority candidate presence on the proportion of coethnic donations in U.S. House elections. Used in [Chapter @sec-plots] and [Chapter @sec-panel]. +- `hh2019`: Based on @HH2019, who study the effect of indirect versus direct democracy on naturalization rates in Switzerland. Used in [Chapter @sec-plots], [Chapter @sec-panel], and [Chapter @sec-panel-sens]. diff --git a/vignettes/02-fect.Rmd b/vignettes/02-fect.Rmd index 3abc0d66..be10b617 100644 --- a/vignettes/02-fect.Rmd +++ b/vignettes/02-fect.Rmd @@ -1,48 +1,50 @@ -# Fect Main Program {#sec-fect} +# The Imputation Estimator {#sec-fect} -In this chapter, we illustrate how to use the **fect** package to implement counterfactual estimators (or imputation estimators) and conduct diagnostic tests proposed by @LWX2022--\[Paper\]. Download the R code used in this chapter [here](rscript/02-fect.R). +In this chapter, we illustrate how to use the **fect** package to implement counterfactual estimators (or imputation estimators) and conduct diagnostic tests proposed by @LWX2022 [Paper]. R script used in this chapter can be downloaded [here](https://raw.githubusercontent.com/xuyiqing/fect/dev/vignettes/rscript/02-fect.R). ## Simulated data -In this demo, we will primarily be using `simdata`, in which the treatment is allowed to switch on and off. There are 200 units and 35 time periods. +In this chapter, we use `sim_base`, a simulated panel dataset in which the parallel trends assumption holds. The dataset has 200 units, 35 time periods, and treatment that switches on and off. The data are simulated with treatment switching on and off, capturing the general case of treatment reversal under strict exogeneity. The outcome is generated from a two-way fixed effects model with two covariates: $$Y_{it} = \tau_{it} D_{it} + \beta_1 X_{1,it} + \beta_2 X_{2,it} + \alpha_i + \xi_t + \epsilon_{it}$$ -```{r echo = FALSE} +Since there are no latent factors, the `"fe"` method (two-way fixed effects counterfactual estimator, or FEct) is correctly specified for this DGP. For settings where latent factors are present and the FE estimator is biased, see [Chapter @sec-ife-mc]. + +```{r .common, include = FALSE} +source("_common.R") +``` + +```{r setup-seed, echo = FALSE} set.seed(1234) -rm(list = ls()) ``` -```{r message = FALSE, warning = FALSE} -library(fect) -data(fect) -ls() +```{r load-packages, message = FALSE, warning = FALSE} +data(sim_base) +data(sim_gsynth) ``` -Before conducting any statistical analysis, we use the **panelView** package to visualize the treatment and outcome variables in `simdata`: +We use the **panelView** package to visualize the treatment and outcome variables: -```{r message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5} +```{r panelview-treatment, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5} library(panelView) -panelview(Y ~ D, data = simdata, index = c("id","time"), - axis.lab = "time", xlab = "Time", ylab = "Unit", +panelview(Y ~ D, data = sim_base, index = c("id","time"), + axis.lab = "time", xlab = "Time", ylab = "Unit", gridOff = TRUE, by.timing = TRUE, background = "white", main = "Simulated Data: Treatment Status") ``` -We then take a look at the outcome variable. In the figure below, blue and gray represent treatment and control conditions. +In the figure below, blue and gray represent treatment and control conditions. -```{r fig.width = 6, fig.height = 4.5} -panelview(Y ~ D, data = simdata, index = c("id","time"), - axis.lab = "time", xlab = "Time", ylab = "Unit", +```{r panelview-outcome, fig.width = 6, fig.height = 4.5, warning = FALSE} +panelview(Y ~ D, data = sim_base, index = c("id","time"), + axis.lab = "time", xlab = "Time", ylab = "Unit", theme.bw = TRUE, type = "outcome", main = "Simulated Data: Outcome") ``` ------------------------------------------------------------------------ -## The imputation estimator (FEct) - -Using the current version of **fect**, we can apply several different methods to make counterfactual predictions and estimate treatment effects by specifying the `method` option: `"fe"` (two-way fixed effects, default), `"ife"` (interactive fixed effects), `"mc"` (matrix completion method), `"cfe"` (complex fixed effects), and `"polynomial"` (fixed effects with group-specific time trends). First, we illustrate the main syntax of **fect** using the `"fe"` method. +## The FEct estimator -The two-way fixed effects counterfactual estimator (FEct) is also independently proposed by @BJS2024 and @gardnertwo, who refer to it as the "imputation method" and "two-stage DID," respectively. +The two-way fixed effects counterfactual (FEct) estimator imputes the counterfactual outcome $\hat{Y}(0)$ for treated observations using a two-way fixed effects model estimated on control observations. It is also independently proposed by @BJS2024 and @gardnertwo, who refer to it as the "imputation method" and "two-stage DID," respectively. ### Estimation @@ -51,599 +53,306 @@ We estimate the average treatment effect on the treated (ATT) using the followin The first variable on the right hand side of the formula is the treatment indicator $D$; the rest of the right-hand-side variables serve as controls. The `index` option specifies the unit and time indicators. The `force` option ("none", "unit", "time", and "two-way") specifies the additive component(s) of the fixed effects included in the model. The default option is "two-way" (including both unit and time fixed effects). ```{r simdata_fect_nose, eval=TRUE, cache = TRUE, message = FALSE, results = 'hide'} -out.fect <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), +out.fect <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id","time"), method = "fe", force = "two-way") ``` -### Visualization +::: {.callout-tip appearance="simple"} +### Model specification -We can use the `plot` function to visualize the estimation results. By default, the `plot` function produces a "gap" plot -- as if we type `plot(out.fect, type = "gap")` --- which visualizes the estimated period-wise ATT (dynamic treatment effects). For your reference, the true population average effects in `simdata` go from 1 to 3 from the 1st to the 10th post-treatment period. - -The bar plot at the bottom of the plot shows the number of treated units for each time period. The options `cex.main`, `cex.lab`, `cex.axis`, and `cex.text` adjust the font sizes of the title, axis labels, axis numbers, and in-graph text, respectively. +The key parameters that control the **model** are: `method` (estimator choice), `force` (fixed effects structure), and `r` (number of factors for IFE/MC methods; see [Chapter @sec-ife-mc]; by default, `r` is set to 0 when `method = "fe"`). These determine how $\hat{Y}(0)$ is imputed. +::: -Users can choose to plot only those periods whose **number of treated observations** exceeds a threshold, which is set as a proportion of the largest number of treated observations in a period (the default is `proportion = 0.3`). +We can use the `plot` function to visualize the estimation results. By default, it produces a "gap" plot --- `plot(out.fect, type = "gap")` --- which shows the estimated period-wise ATT (dynamic treatment effects). The true population average effects in `sim_base` go from 1 to 3 from the 1st to the 10th post-treatment period. ```{r fect_plot_nose, fig.width = 6, fig.height = 4.5} -plot(out.fect, main = "Estimated ATT (FEct)", ylab = "Effect of D on Y", +plot(out.fect, main = "Estimated ATT (FEct)", ylab = "Effect of D on Y", cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) ``` -The uncertainty estimates are unavailable in the plot above because, by default, `se = FALSE` to save computational power. +In the gap plot, pre-treatment estimates appear in gray (in-sample) while post-treatment estimates appear in black (out-of-sample). This visual distinction highlights that pre-treatment "effects" should be near zero if the model is well-specified, while post-treatment effects are the quantities of interest. When `loo = TRUE` is used in the plot call, all points appear in black because both pre- and post-treatment estimates are out-of-sample. The uncertainty estimates are unavailable in the plot above because, by default, `se = FALSE` to save computational power. See [Chapter @sec-plots] for more visualization options. -The graph is a **ggplot2** object; user can conveniently use the `ggsave` (preferred) function to export the resulting plot. See [Chapter @sec-plots] for more visualization options. +### Inference -### Uncertainty estimates +The package can produce uncertainty estimates when `se = TRUE`. The default is the non-parametric cluster-bootstrap (`vartype = "bootstrap"`), which works well when the number of units is relatively large and many units experience the treatment condition. The number of bootstrap runs can be set by `nboots`. -The package can produce uncertainty estimates when `se = TRUE`. One can use the non-parametric bootstrap procedure by setting `vartype = "bootstrap"`. Note that it only works well when the number of units is relatively large and many units in the data set experience the treatment condition. The number of bootstrap runs can be set by `nboots`. +::: {.callout-tip appearance="simple"} +### Inference parameters + +The key parameters that control **uncertainty estimation** are: `se` (enable standard errors), `vartype` (`"bootstrap"`, `"jackknife"`, or `"parametric"`), `nboots` (number of bootstrap replications), and `cl` (clustering variable). These parameters do not affect point estimates. `"parametric"` is only available in the Synth setting, i.e., when `time.component.from = "nevertreated"`. +::: ::: {.callout-note appearance="simple"} **Parallel computing** will speed up both cross-validation and uncertainty estimation significantly. We recommend that users manually set the number of cores using the `cores` option. If this is not supplied or is `NULL`, we will automatically select the smaller of `8` and the number of usable system cores minus `2`, to prevent excessive use of system resources. ::: -```{r simdata_fect, eval=TRUE, cache = TRUE, message = FALSE, results = 'hide'} -out.fect <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), - method = "fe", force = "two-way", se = TRUE, - cores = 8, parallel = TRUE, nboots = 1000) -``` - ::: {.callout-note appearance="simple"} **Clustering.** -- By default, `fect()` uses cluster-bootstrap at the `unit` level when `se = TRUE`, that is `vartype = "bootstrap"`. The uncertainty estimates thus account for arbitrary serial correlation within a unit over time—commonly understood as being "clustered" at the unit level. In the example above, the unit is `id`, hence, by default, `cl = "id"`. -- Alternatively, users can obtain uncertainty estimates using the cluster-jackknife method by specifying `vartype = "jackknife"`, also at the unit level. In this case, the algorithm calculates standard errors by iteratively dropping one unit (i.e., the entire time series) from the dataset. +- By default, `fect()` uses cluster-bootstrap at the `unit` level when `se = TRUE`, that is `vartype = "bootstrap"`. The uncertainty estimates thus account for arbitrary serial correlation within a unit over time---commonly understood as being "clustered" at the unit level. In the example above, the unit is `id`, hence, by default, `cl = "id"`. +- Alternatively, users can obtain uncertainty estimates using the cluster-jackknife method by specifying `vartype = "jackknife"`, also at the unit level. In this case, the algorithm calculates standard errors by iteratively dropping one unit (i.e., the entire time series) from the dataset. This can be particularly useful when the number of treated units is small. - To cluster standard errors at a different, usually higher, level, users can specify the clustering variable using the `cl` option. ::: -The `plot()` function can visualize the estimated period-wise ATTs as well as their uncertainty estimates. `stats = "F.p"` shows the p-value for the F test of no-pretrend (more details below). - -```{r fect_plot_nse, fig.width = 6, fig.height = 4.5} -plot(out.fect, main = "Estimated ATT (FEct)", ylab = "Effect of D on Y", - cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8, stats = "F.p") -``` - -### Save estiamtes - -Users can use the `print` function to take a look at a summary of the estimation results or retrieve relevant statistics by directly accessing the fect object. Specifically, `est.avg` and `est.avg.unit` show the ATT averaged over all periods -- the former weights each treated observation equally while the latter weights each treated unit equally. `est.beta` reports the coefficients of the time-varying covariates. `est.att` reports the average treatment effect on the treated (ATT) by period. Treatment effect estimates from each bootstrap run is stored in `eff.boot`, an array whose dimension = (#time periods \* #treated \* #bootstrap runs). - -```{r} -print(out.fect) -``` - -To save space, results are not shown here. - -```{r eval = FALSE} -out.fect$est.att -out.fect$est.avg -out.fect$est.beta -``` - -### Leave-on-out approach - -@li2025benchmarking show that in some applications, pre-trend estimates based on in-sample model fit can lead to the mistaken belief that no pre-trend exists, even when a non-parallel pre-trend is present. A simple fix is to use a leave-one-out method by setting `loo = TRUE` to obtain these estimates, although it is significantly more time-consuming. - -::: {.callout-note appearance="simple"} -We recommend setting `loo = TRUE` when (i) the event-study plot is intended as a critical piece of evidence to support the parallel trends assumption, which is often the case, or (ii) when implementing an equivalence test for the pre-trend estimates. For more discussion on the LOO pre-trend test, see [here](#loo-pre-trend-test) - -Our most preferred test, however, is the sensitivity analysis discussed in [Chapter @sec-panel-sens], which combines out-of-sample placebo estimates with post-treatment ATT estimates. -::: - -We can implement the leave-one-out pre-trend test by setting `loo = TRUE`. - -```{r fect_loo, eval=TRUE, cache = TRUE, message = FALSE} -out.fect.loo <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), - method = "fe", force = "two-way", se = TRUE, loo = TRUE, - cores = 8, parallel = TRUE, nboots = 200) -``` - -The event study plot utilizing leave-one-out for pretreatment estimates is shown below. This graph is fairly similar to the graphics we presented earlier without using leave-one-out. However, this is not always true. - -```{r, fig.width = 6, fig.height = 4.5} -plot(out.fect.loo,main = "Estimated ATT (FEct) -- LOO", - cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) -``` - -## Interactive fixed effects & matrix completion - -In addition to FEct, **fect** also supports the interactive fixed effects counterfactual (IFEct) method proposed by @Gobillon2016 and @Xu2017 and the matrix completion (MC) method proposed by @Athey2021---`method = "ife"` and `method = "mc"`, respectively. We use EM algorithm to impute the counterfactuals of treated observations. - -### IFE - -For the IFE approach, we need to specify the number of factors using option `r`. For the MC method, we need to specify the tuning parameter in the penalty term using option `lambda`. By default, the algorithm will select an optimal hyper-parameter via a built-in cross-validation procedure. - -**Choosing the number of factors.** We provide a cross-validation procedure (by setting `CV = TRUE`) to help determine the tuning parameter in IFE, MC, and Gsynth (new addition, see next chapter for further details) methods. By default, the cross-validation procedure is run for `k` rounds (`k = 10`) and the candidate tuning parameter corresponding to the minimal mean squared prediction error is selected (`criterion = "mspe"`). - -In each round, some untreated observations are removed as the testing set to evaluate the prediction performance of the model with a tuning parameter. The option `cv.prop` specifies the size of testing set comparing to the set of observations under control (default: `cv.prop = 0.1`). If we want to restrict the testing set to untreated observations only from treated units (those whose treatment statuses have changed), set `cv.treat = TRUE`. - -An additional issue is the serial correlation within a unit. We remove a consecutive number of observations from a unit as elements in the testing set in order to avoid over fitting caused by serial correlation. The consecutive number is specified in option `cv.nobs` (e.g. when `cv.nobs = 3`, the test set is a number of triplets). - -We can also remove triplets in the fitting procedure but only include the middle observation of each triplet in the test set using the option `cv.donut` (e.g. when `cv.donut = 1`, the first and the last observation in each removed triplet will **not** be included in the test set). - -**Hyper-parameter tuning** The package offers several criteria when tuning hyper-parameters. For the IFE method, we can set `criterion = "pc"` to select the hyper-parameter based on the information criterion. If we want to select the hyper-parameter based on mean-squared prediction errors from cross-validation to get a better prediction ability, set `criterion = "mspe"` (default), and to alleviate the impact of some outlier prediction errors, we allow the criterion of geometric-mean squared prediction errors (`criterion = "gmspe"`). If one wants to select the hyper-parameter that yields a better pre-trend fitting on test sets rather than a better prediction ability, set `criterion = "moment"` (we average the residuals in test sets by their relative periods to treatments and then average the squares of these period-wise deviations weighted by the number of observations at each period) . - -For the IFE method, we need to specify an interval of candidate number of unobserved factors in option `r` like `r=c(0,5)`. When cross-validation is switched off, the first element in `r` will be set as the number of factors. Below we use the MSPE criterion and search the number of factors from 0 to 5. - -```{r simdata_ife, eval=TRUE, cache = TRUE} -out.ife <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), - force = "two-way", method = "ife", CV = TRUE, r = c(0, 5), - se = TRUE, cores = 8, nboots = 1000, parallel = TRUE) -print(out.ife) -``` - -The figure below shows the estimated ATT using the IFE method. The cross-validation procedure selects the correct number of factors ($r=2$). - -```{r fig.width = 6, fig.height = 4.5} -plot(out.ife, main = "Estimated ATT (IFEct)") -``` - -### MC - -For the MC method, we also need to specify a sequence of candidate tuning parameters. For example, we can specify `lambda = c(1, 0.8, 0.6, 0.4, 0.2, 0.05)`. If users don't have any prior knowledge to set candidate tuning parameters, a number of candidate tuning parameters can be generated automatically based on the information from the outcome variable. We specify the number in option `nlambda`, e.g. `nlambda = 10`. - -```{r simdata_mc, eval=TRUE, cache = TRUE} -out.mc <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), - force = "two-way", method = "mc", CV = TRUE, - se = TRUE, cores = 8, nboots = 1000, parallel = TRUE) - -print(out.mc) -``` - -```{r fig.width = 6, fig.height = 4.5} -plot(out.mc, main = "Estimated ATT (MC)") -``` - -## Weighitng treatment effects - -After obtaining the individual treatment effects using one of the counterfactual estimators, we can weight these estimates using a constructed balanced treated sample or other user-supplied weighting schemes. - -### Balanced treated sample - -**fect** also provides the option `balance.period`, which allows the calculation of the average treatment effects only for *treated* units that exhibit complete data in specified pre- and post-treatment periods. For instance, if the option is set to `balance.period = c(-3,4)`, the algorithm will calculate the average treatment effects for units that have at least four consecutive non-missing observations in the pre-treatment periods `(-3, -2, -1, 0)` and at least four consecutive non-missing observations in the post-treatment periods `(1, 2, 3, 4)`. Note that this option does not affect whether a never-treated unit enters estimation. - -```{r simdata_bal, eval=TRUE, cache = TRUE} -out.bal <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), - balance.period = c(-3, 4), force = "two-way", method = "ife", - CV = FALSE, r = 2, se = TRUE, nboots = 200, parallel = TRUE) -``` - -We can then visualize the dynamic treatment effects using the inbuilt function `plot`. By default, it displays the dynamic treatment effects of the "balanced" sample. - -```{r fig.width = 6, fig.height = 4.5} -plot(out.bal, main = "Estimated ATT (Balanced Sample)") -``` - -The usual plotting options can be used to adjust the balanced plot as well. - -```{r fig.width = 6, fig.height = 4.5} -plot(out.bal, main = "Estimated ATT (Balanced Sample)", - color = "red", count.color = "blue") -``` - -### Weighted treatment effect - -The package offers the option `W` to calculate the weighted average treatment effects. The weighting variable does not affect the estimation of fixed effects or factors. Only the weighted average treatment effects or weighted dynamic treatment effects are obtained by aggregating the treatment effects using the weight `W`. - -```{r simdata_w, eval=TRUE, cache = TRUE} -simdata$Weight <- abs(rnorm(n = dim(simdata)[1])) -out.w <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), - force = "two-way", method = "ife", W = 'Weight', - CV = FALSE, r = 2, se = TRUE, nboots = 200, parallel = TRUE) -``` - -We can then visualize the weighted dynamic treatment effects using the inbuilt function `plot`, it by default shows the weighted dynamic treatment effects. - -```{r fig.width = 6, fig.height = 4.5} -plot(out.w, main = "Estimated Weighted ATT") +```{r simdata_fect, eval=TRUE, cache = TRUE, message = FALSE, results = 'hide'} +out.fect <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id","time"), + method = "fe", force = "two-way", se = TRUE, + parallel = TRUE, cores = 16, nboots = 1000) ``` -## Effect heterogeneity - -We provide several methods for researchers to explore heterogeneous treatment effects (HTE). +The `plot()` function can visualize the estimated period-wise ATTs as well as their uncertainty estimates. `stats = "F.p"` shows the p-value for the F test of no-pretrend. -### Box plots - -One way to understand HTE is to use a series of box plots to visualize the estimated individualistic treatment effects of observations under the treatment condition (by setting `type = "box"`). Although these effects are not identified at the individual observation level, their level of dispersion is informative of treatment effects heterogeneity at different (relative) time periods, as well as model performance. - -```{r hte, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.ife, type = "box", xlim = c(-15, 10)) +```{r fect_plot_nse, fig.width = 6, fig.height = 4.5} +plot(out.fect, main = "Estimated ATT (FEct)", ylab = "Effect of D on Y", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8, stats = "F.p") ``` -### CATT by calendar time +### Exiting the treatment -Another way to explore HTE is to investigate how the treatment effect evolves over time. In the plot below, the point estimates represents the ATTs by calendar time; the blue curve and band represent a lowess fit of the estimates and its 95% confidence interval, respectively; and the red horizontal dashed line represents the ATT (averaged over all time periods). +**fect** allows the treatment to switch back and forth and provides diagnostic tools for this setting. After the estimation, we can visualize the period-wise ATTs relative to the `exit` of treatments by setting `type = "exit"`. The x-axis is then realigned based on the timing of the treatment's exit, not onset, e.g., 1 represents 1 period after the treatment ends. In the exit plot, the color convention is reversed: pre-exit estimates appear in black (out-of-sample) and post-exit estimates appear in gray (in-sample). -```{r hte_time, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.ife, type = "calendar", xlim = c(1, 35)) +```{r exit_fect, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} +plot(out.fect, type = "exit", main = "Exit Plot (FEct)") ``` -### CATT by a covariate +### Save estimates -By setting `type = "hte"` or `type = "heterogeneous`, we can also plot the HTE by arbitrary covariates that are unaffected by the treatment. As before, the blue curve and band represent a lowess fit of the estimates and its 95% confidence interval, respectively. The red dashed line represents the ATT. The histogram at the bottom of the figure illustrates the distribution of the covariates, and can be turned off using `show.count = FALSE`. In our simulated case, the effect size is unrelated to the values of covariate `X1`. +Users can use the `print` function to view a summary of the estimation results or retrieve relevant statistics by directly accessing the fect object. Specifically, `est.avg` and `est.avg.unit` show the ATT averaged over all periods -- the former weights each treated observation equally while the latter weights each treated unit equally. `beta` reports the coefficients of the time-varying covariates. `est.att` reports the average treatment effect on the treated (ATT) by period. -```{r hte_X1, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.ife, type = "hte", covariate = "X1") +```{r print-fect} +print(out.fect) ``` -We can also plot the CATT when a covariate is discrete. To demonstrate this, we artificially create a moderating variable `X3`, which must be included in the outcome model and then specified in the heterogeneous treatment effect plot. +To save space, results are not shown here. -```{r hte_discrete, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -simdata$X3 <- sample(1:3, size = nrow(simdata), replace = TRUE) -out.ife.X3 <- fect(Y ~ D + X1 + X2 + X3, data = simdata, index = c("id","time"), - method = "ife", r = 2, se = TRUE, seed = 123, - cores = 8, nboots = 1000, parallel = TRUE) +```{r extract-estimates, eval = FALSE} +out.fect$est.att +out.fect$est.avg +out.fect$beta ``` -As expected, there is not much effect heterogeneity along `X3`. In the resulting figure, we can also assign labels to the discrete values in the moderator. +After estimation with `se = TRUE`, bootstrap treatment effect estimates from each run are stored in `eff.boot`, an array whose dimension = (#time periods \* #treated \* #bootstrap runs). Standard errors for the period-wise ATTs are available in the output object alongside the point estimates. -```{r, fig.width = 6, fig.height = 4.5} -plot(out.ife.X3, type="hte", covariate = "X3", - xlab = "", ylab = "Effet of D on Y", - covariate.labels = c("USA", "China", "UK"), - ylim = c(-2, 6)) +```{r extract-bootstrap, eval = FALSE} +out.fect$eff.boot ``` -Our next update will accommodate time-invariant covariates and allow users to explore effect heterogeneity around them. +For IFE and MC methods, see [Chapter @sec-ife-mc]. ------------------------------------------------------------------------ -## Diagnostic tests - -We provide three types of diagnostic tests: (1) a placebo test, (2) a test for (no) pretrend, and (3) a test for (no) carry-over effects. For each test, we support both the difference-in-means (DIM) approach and the equivalence approach. The details are provided in the paper. - -### Placebo tests - -We provide a placebo test for a settled model---hence, cross-validation is not allowed---by setting `placeboTest = TRUE`. We specify a range of pretreatment periods as "placebo periods" in option `placebo.period` to remove observations in the specified range for model fitting, and then test whether the estimated ATT in this range is significantly different from zero. Below, we set `c(-2, 0)` as the placebo periods. - -```{r placebo, eval = TRUE, cache = TRUE, message = FALSE, results='hide'} -out.fect.p <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), - force = "two-way", parallel = TRUE, se = TRUE, CV = 0, - nboots = 200, placeboTest = TRUE, placebo.period = c(-2, 0)) - -out.ife.p <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), - force = "two-way", method = "ife", r = 2, CV = 0, - parallel = TRUE, se = TRUE, - nboots = 200, placeboTest = TRUE, placebo.period = c(-2, 0)) - -out.mc.p <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), - force = "two-way", method = "mc", lambda = out.mc$lambda.cv, - CV = 0, parallel = TRUE, se = TRUE, - nboots = 200, placeboTest = TRUE, placebo.period = c(-2, 0)) -``` - -The placebo test conducts two types of tests: - -**t test.** If t-test p-value is smaller than a pre-specified threshold (e.g. 5%), we reject the null of no-differences. Hence, the placebo test is deemed failed. - -**TOST.** The TOST checks whether the 90% confidence intervals for estimated ATTs in the placebo period exceed a pre-specified range (defined by a threshold), or the equivalence range. A TOST p-value smaller than a pre-specified threshold suggests that the null of difference bigger than the threshold is rejected; hence, the placebo test is passed. - -By default, the plot will display the p-value of the $t$-test (`stats = "placebo.p"`). Users can also add the p-value of a corresponding TOST test by setting `stats = c("placebo.p","equiv.p")`. A larger placebo p-value from a t-test and a smaller placebo TOST p-value are preferred. - -```{r placebo_1, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.fect.p, cex.text = 0.8, stats = c("placebo.p","equiv.p"), - main = "Estimated ATT (TWFE)") -``` - -```{r placebo_2, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.ife.p, ylab = "Effect of D on Y", main = "Estimated ATT (IFE)", - cex.text = 0.8, stats = c("placebo.p","equiv.p")) -``` - -```{r placebo_3, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.mc.p, cex.text = 0.8, stats = c("placebo.p","equiv.p"), - main = "Estimated ATT (MC)") -``` - -The results in the placebo test confirm that IFEct is a better model than MC for this particular DGP. - -### Tests for (no) pre-trend - -We introduce two statistical tests for the presence of a pre-trend (or the lack thereof). The first test is an $F$ test for zero residual averages in the pretreatment periods. The second test is a two-one-sided $t$ (TOST) test, a type of equivalence tests. - -**F test.** We offer a goodness-of-fit test (a variant of the $F$ test) and to gauge the presence of pretreatment (differential) trends. A larger F-test p-value suggests a better pre-trend fitting. Users can specify a test range in option `pre.periods`. For example, `pre.periods = c(-4,0)` means that we test pretreatment trend of the last 5 periods prior to the treatment (from period -4 to period 0). If `pre.period = NULL` (default), all pretreatment periods in which the number of treated units exceeds the total number of treated units \* `proportion` will be included in the test. - -**TOST.** The TOST checks whether the 90% confidence intervals for estimated ATTs in the pretreatment periods (again, subject to the `proportion` option) exceed a pre-specified range, or the equivalence range. A smaller TOST p-value suggests a better pre-trend fitting. While users can check the values of confidence intervals, we give a visualization of the equivalence test. We can plot the pretreatment residual average with the equivalence confidence intervals by setting `type = "equiv"`. Option `tost.threshold` sets the equivalence range (the default is $0.36\sigma_{\epsilon}$ in which $\sigma_{\epsilon}$ is the standard deviation of the outcome variable after two-way fixed effects are partialed out). By setting `range = "both"`, both the minimum range (in gray) and the equivalence range (in red) are drawn. - -On the topleft corner of the graph, we show several statistics of the user's choice. User can choose which statistics to show by setting `stats = c("none", "F.stat", "F.p", "F.equiv.p", "equiv.p")` which corresponds to not showing any, the $F$ statistic, the p-value for the $F$ test, the p-value for the equivalence $F$ test, the (maximum) p-value for the the TOST tests, respectively. For the gap plot, the default is `stats = "none"`. For the equivalence plot, the default is `stats = c("equiv.p, F.p")`. Users can also change the labels of statistics using the `stats.labs` options. Users can adjust its position using the `stats.pos` option, for example `stats.pos = c(-30, 4)`. To turn off the statistics, set `stats = "none"`. +## Diagnostics -Below, we visualize the result of the equivalence test for each of the three estimators using our simulated data. These figures show that both the IFE and MC methods pass the equivalence test while the FE method does not. +Once we have point estimates and uncertainty estimates, we conduct diagnostic checks before interpreting results. This section covers the placebo test, the carryover test, and the leave-one-out approach. -```{r pretrend1, eval = TRUE, cache = TRUE, fig.width = 6, fig.height = 4.5} -plot(out.fect, type = "equiv", ylim = c(-4,4), - cex.legend = 0.6, main = "Testing Pre-Trend (FEct)", cex.text = 0.8) -``` - -```{r pretrend2, eval = TRUE, cache = TRUE, fig.width = 6, fig.height = 4.5} -plot(out.ife, type = "equiv", ylim = c(-4,4), - cex.legend = 0.6, main = "Testing Pre-Trend (IFEct)", cex.text = 0.8) -``` - -```{r pretrend3, eval = TRUE, cache = TRUE, fig.width = 6, fig.height = 4.5} -plot(out.mc, type = "equiv", ylim = c(-4,4), - cex.legend = 0.6, main = "Testing Pre-Trend (MC)", cex.text = 0.8) -``` - -From the above plots, we see that FEct fails both tests; IFEct passes both tests using a conventional test size (5%); and MC fails the F tests, but passes the TOST (equivalence) test. Hence, we may conclude that IFEct is a more suitable model. - -### LOO pre-trend test {#loo-pre-trend-test} +### Placebo test -Instead of using estimated ATTs for periods prior to the treatment to test for pre-trends, we recommend users employ a leave-one-out (LOO) approach (`loo = TRUE`) to consecutively hide one pretreatment period (relative to the timing of the treatment) and repeatedly estimate the pseudo treatment effects for that pretreatment period. The LOO approach can be understood as an extension of the placebo test. It has the benefit of providing users with a more holistic view of whether the identifying assumptions likely hold. However, as the program needs to conduct uncertainty estimates for each turn, it is much more time-consuming than the original one. +We provide a placebo test for a settled model by setting `placeboTest = TRUE`. We specify a range of pretreatment periods as "placebo periods" in option `placebo.period` to remove observations in the specified range for model fitting, and then test whether the estimated ATT in this range is significantly different from zero. Below, we set `c(-2, 0)` as the placebo periods. -```{r simdata_fect_loo, eval=FALSE, cache = TRUE, message = FALSE, results = 'hide'} -out.fect.loo <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), - method = "fe", force = "two-way", se = TRUE, parallel = TRUE, nboots = 200, loo = TRUE) -out.ife.loo <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), - method = "ife", force = "two-way", se = TRUE, parallel = TRUE, nboots = 200, loo = TRUE) -out.mc.loo <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), - method = "mc", force = "two-way", se = TRUE, parallel = TRUE, nboots = 200, loo = TRUE) +```{r fect_placebo, eval=TRUE, cache=TRUE, message=FALSE, results='hide', fig.width=6, fig.height=4.5} +out.fect.placebo <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id","time"), + force = "two-way", method = "fe", + se = TRUE, nboots = 1000, parallel = TRUE, cores = 16, + placeboTest = TRUE, placebo.period = c(-2, 0)) +plot(out.fect.placebo, cex.text = 0.8) ``` -After the LOO estimation, one can plot these LOO pre-trends in the gap plot or the equivalence plot by setting `loo = TRUE` in the `plot` function. +For more detail on placebo tests, including IFE and MC examples, see [Chapter @sec-ife-mc]. -```{r pretrend_loo1, eval = FALSE, cache = TRUE, fig.width = 6, fig.height = 4.5} -plot(out.fect.loo, type = "equiv", ylim = c(-4,4), loo = TRUE, - cex.legend = 0.6, main = "Testing Pre-Trend LOO (FEct)", cex.text = 0.8) - -plot(out.ife.loo, type = "equiv", ylim = c(-4,4), loo = TRUE, - cex.legend = 0.6, main = "Testing Pre-Trend LOO (IFEct)", cex.text = 0.8) - -plot(out.mc.loo, type = "equiv", ylim = c(-4,4), loo = TRUE, - cex.legend = 0.6, main = "Testing Pre-Trend LOO (MC)", cex.text = 0.8) -``` - -Note that the LOO test usually takes lots of computational power. For our example, we find that the IFE estimator still passes both the F test and the equivalence test based on its LOO pre-trends, while the MC estimator fails both tests. - -### Exiting the treatment +### Carryover test -**fect** allows the treatment to switch back and forth and provides diagnostic tools for this setting. After the estimation, we can visualize the period-wise ATTs relative to the `exit` of treatments by setting `type = "exit"` (one can still draw the classic gap plot by setting `type = "gap"`). The x-axis is then realigned based on the timing of the treatment’s exit, not onset, e.g., 1 represents 1 period after the treatment ends. +The idea of the placebo test can be extended to testing the presence of carryover effects. Instead of hiding a few periods right before the treatment starts, we hide a few periods right after the treatment ends. If carryover effects do not exist, we would expect the average prediction error in those periods to be close to zero. -```{r exit_1, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.fect, type = "exit", ylim = c(-2.5,4.5), main = "What Happens after the Treatment Switches Off?") -``` +To perform the carryover test, we set `carryoverTest = TRUE` and specify the range of exit-treatment periods in `carryover.period`. Below, we set `carryover.period = c(1, 3)`. Since `sim_base` is simulated without carryover effects, we expect the test to pass. -```{r exit_2, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.ife, type = "exit", ylim = c(-2.5,4.5), main = "Exit Plot (IFE)") +```{r fect_carryover, eval=TRUE, cache=TRUE, message=FALSE, results='hide'} +out.fect.carry <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id","time"), + force = "two-way", method = "fe", + se = TRUE, nboots = 1000, parallel = TRUE, cores = 16, + carryoverTest = TRUE, carryover.period = c(1, 3)) ``` -```{r exit_3, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.mc, type = "exit", ylim = c(-2.5,4.5), main = "Exit Plot (MC)") +```{r fect_carryover_plot, eval=TRUE, cache=TRUE, warning=FALSE, fig.width=6, fig.height=5} +plot(out.fect.carry, type = "exit", cex.text = 0.8, main = "Carryover Effects (FEct)") ``` -### Tests for (no) carryover effects - -The idea of the placebo test can be extended to testing the presence of carryover effects. Instead of hiding a few periods right before the treatment starts, we hide a few periods right after the treatment ends. If carryover effects do not exist, we would expect the average prediction error in those periods to be close to zero. To perform the carryover test, we set the option `carryoverTest = TRUE`. We can treat a range of exit-treatment periods in option `carryover.period` to remove observations in the specified range for model fitting, and then test whether the estimated ATT in this range is significantly different from zero. - -Below, we set `carryover.period = c(1, 3)`. As we deduct the treatment effect from the outcome in `simdata`, we expect the average prediction error for these removed periods to be close to zero. - -```{r carryover, eval = TRUE, cache = TRUE, message = FALSE, results='hide'} -out.fect.c <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), - force = "two-way", parallel = TRUE, se = TRUE, CV = 0, - nboots = 200, carryoverTest = TRUE, carryover.period = c(1, 3)) +For more detail on carryover tests with IFE and MC, see [Chapter @sec-ife-mc]. -out.ife.c <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), - force = "two-way", method = "ife", r = 2, CV = 0, - parallel = TRUE, se = TRUE, - nboots = 200, carryoverTest = TRUE, carryover.period = c(1, 3)) +### Leave-one-out approach -out.mc.c <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), - force = "two-way", method = "mc", lambda = out.mc$lambda.cv, - CV = 0, parallel = TRUE, se = TRUE, - nboots = 200, carryoverTest = TRUE, carryover.period = c(1, 3)) -``` +@li2025benchmarking show that in some applications, pre-trend estimates based on in-sample model fit can lead to the mistaken belief that no pre-trend exists, even when a non-parallel pre-trend is present. A simple fix is to use a leave-one-out method by setting `loo = TRUE` to obtain these estimates, although it is significantly more time-consuming. -Like the placebo test, the plot will display the p-value of the carryover effect test (`stats = "carryover.p"`). Users can also add the p-value of a corresponding TOST test by setting `stats = c("carryover.p","equiv.p")`. +::: {.callout-note appearance="simple"} +We recommend setting `loo = TRUE` when (i) the event-study plot is intended as a critical piece of evidence to support the parallel trends assumption, which is often the case, or (ii) when implementing an equivalence test for the pre-trend estimates. For more discussion on the LOO pre-trend test, see [Chapter @sec-ife-mc]. -```{r carryover1, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.fect.c, type = "exit", ylim = c(-2.5,4.5), - cex.text = 0.8, main = "Carryover Effects (FE)") -``` +Our most preferred tests, however, are the placebo test described above and the sensitivity analysis discussed in [Chapter @sec-panel-sens], which combines out-of-sample placebo estimates with post-treatment ATT estimates, but it requires a lot of power. +::: -```{r carryover2, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.ife.c, type = "exit", ylim = c(-2.5,4.5), - cex.text = 0.8, main = "Carryover Effects (IFE)") -``` +We can implement the leave-one-out pre-trend test by setting `loo = TRUE`. -```{r carryover3, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.mc.c, type = "exit", ylim = c(-2.5,4.5), - cex.text = 0.8, main = "Carryover Effects (MC)") +```{r fect_loo, eval=TRUE, cache = TRUE, message = FALSE} +out.fect.loo <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id","time"), + method = "fe", force = "two-way", se = TRUE, loo = TRUE, + parallel = TRUE, cores = 16, nboots = 1000) ``` -Once again, the IFE estimator outperforms the other two. - -Using real-world data, researchers will likely find that carryover effects exist. If such effects are limited, researchers can consider removing a few periods after the treatment ended for the treated units from the first-stage estimation (using the `carryover.period` option) and re-estimated the model (and re-conduct the test). We provide such an example in the paper. Here, we illustrate the option using `simdata`. - -```{r carryover_rm, eval = TRUE, cache = TRUE, message = FALSE, results='hide', fig.width = 6, fig.height = 4.5} -out.ife.rm.test <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), - force = "two-way", method = "ife", r = 2, CV = 0, - parallel = TRUE, se = TRUE, carryover.rm = 3, - nboots = 200, carryoverTest = TRUE, carryover.period = c(1, 3))# remove three periods +The event study plot utilizing leave-one-out for pretreatment estimates is shown below. This graph is fairly similar to the graphics we presented earlier without using leave-one-out. However, this is not always true. -plot(out.ife.rm.test, cex.text = 0.8, stats.pos = c(5, 2)) +```{r plot-gap-loo, fig.width = 6, fig.height = 4.5} +plot(out.fect.loo,main = "Estimated ATT (FEct) -- LOO", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) ``` -In the above plot, the three periods in blue are droppred from the first-stage estimation of the factor model while the periods in red are reserved for the (no) carryover effects test. - ------------------------------------------------------------------------ ## Cumulative effects -Users can use `effect()` to calculate cumulative treatment effects. The behavior of `effect()` is similar to the function of the same name in `gsynth`. - Calculation of cumulative effects will need unit-time level bootstrap results. Choose the option `keep.sims=TRUE` to record them. +Users can use `effect()` to calculate cumulative treatment effects. The behavior of `effect()` is similar to the function of the same name in `gsynth`. Calculation of cumulative effects will need unit-time level bootstrap results. Choose the option `keep.sims=TRUE` to record them. -```{r effect, cache = TRUE} -out <- fect(Y ~ D + X1 + X2, data = simgsynth, index = c("id","time"), - method = "gsynth", force = "two-way", CV = TRUE, r = c(0, 5), - se = TRUE, nboots = 200, vartype = 'bootstrap', - parallel = FALSE, keep.sims=TRUE) +::: {.callout-note appearance="simple"} +The example below uses `method = "ife"` with `time.component.from = "nevertreated"`, which is equivalent to `method = "gsynth"` (see [Chapter @sec-gsynth]). We use the explicit form here for clarity. +::: + +```{r cumu_effect, cache = TRUE} +out <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), + method = "ife", time.component.from = "nevertreated", + force = "two-way", CV = TRUE, r = c(0, 5), + se = TRUE, nboots = 1000, vartype = 'bootstrap', + parallel = TRUE, cores = 16, keep.sims=TRUE) cumu.out <- effect(out) ``` Print and plot cumulative effects -```{r effect.plot, cache = TRUE} +```{r cumu_effect_plot, cache = TRUE} print(cumu.out) plot(cumu.out) ``` Users can choose to calculate by-period average effects by setting `cumu=FALSE`. -```{r} +```{r cumu_effect_byperiod, cache = TRUE} effect(out, cumu=FALSE) ``` Calculate the cumulative effect of certain units at certain periods. -```{r} +```{r cumu_effect_subset, cache = TRUE} effect(out, cumu=TRUE, id=c(101,102,103), period=c(1,5)) ``` `effect()` also accepts results from other estimation and inference methods. For example, we can use matrix completion: ```{r effect-mc, cache = TRUE} -out_mc <- fect(Y ~ D + X1 + X2, data = simgsynth, index = c("id","time"), +out_mc <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), method = "mc", force = "two-way", CV = TRUE, r = c(0, 5), - se = TRUE, nboots = 200, vartype = 'bootstrap', - parallel = FALSE, keep.sims=TRUE) + se = TRUE, nboots = 1000, vartype = 'bootstrap', + parallel = TRUE, cores = 16, keep.sims=TRUE) plot(effect(out_mc)) ``` We can also use jackknife instead of bootstrap for inference: ```{r effect-jackknife, cache = TRUE} -out_jack <- fect(Y ~ D + X1 + X2, data = simgsynth, index = c("id","time"), +out_jack <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), method = "mc", force = "two-way", CV = TRUE, r = c(0, 5), - se = TRUE, nboots = 200, vartype = 'jackknife', - parallel = FALSE, keep.sims=TRUE) + se = TRUE, nboots = 1000, vartype = 'jackknife', + parallel = TRUE, cores = 16, keep.sims=TRUE) plot(effect(out_jack)) ``` ------------------------------------------------------------------------ -## Other estimators - -The counterfacutal/imputation estimator framework can be extended to more settings. - -**Complex Fixed Effects.** When there exists more dimensions of fixed effects in addition to the unit and time fixed effects, we can resort to the **"cfe"** (complex fixed effects) estimator to impute the counterfactual based on a linear model with multiple levels of fixed effects. - -Note, **fect** allows the method to run without requiring a baseline two-way fixed effects model. That is, users do not need to set `force = "two-way"` to impute. - -It accepts two options: `sfe` specifies simple (additive) fixed effects in addition to the unit and time fixed effects and `cfe` receives a *list* object and each component in the list is a vector of length 2. - -The value of the first element of each component is the name of `group` variable for which fixed effects are to be estimated (e.g. unit names); the value of the second element is the name of a regressor (e.g., a time trend). For example, we can estimate a model with an additional fixed effects **FE3** along with a unit-specific time trend. - -```{r simdata_cfe, eval=TRUE, cache = TRUE, message = FALSE, results = 'hide', fig.width = 6, fig.height = 4.5} -simdata[,"FE3"] <- sample(c(1,2,3,4,5), size = dim(simdata)[1], replace = TRUE) -out.cfe <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), - method = "cfe", force = "two-way", se = TRUE, parallel = TRUE, nboots = 200, - sfe = c("FE3"), cfe = list(c("id","time"))) -plot(out.cfe) -``` - -**Polynomial.** Sometimes researchers may want to include unit-specific time trends in the model estimated using non-treated data. We can set `method = "polynomial"` to achieve this. - -In addition, By setting `degree = 2`, we can estimate the ATT based on a linear model with unit and time fixed effects, along with a unit-specific quadratic time trend. Similar to **cfe** estimator, a two-way fixed effects model, while encouraged, is not required. - -```{r simdata_poly, eval=TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide', fig.width = 6, fig.height = 4.5} -out.poly <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), - method = "polynomial", force = "two-way", se = TRUE, parallel = TRUE, nboots = 200, - degree = 2) -plot(out.poly) -``` - ------------------------------------------------------------------------- - -## Other options - -We provide a few other options for estimation and visualization. - -### More visualization options - -The `plot` function shipped in **fect** offers some options that help to improve the visualization. - -We can remove the bar plot at the bottom of the plot by setting `show.count = FALSE` +## Other estimands -```{r bar_0, eval = TRUE, cache = TRUE, fig.width = 6, fig.height = 4.5} -plot(out.ife, show.count = FALSE) -``` - -By setting the option `type = "counterfactual"`, we visualize the period-wise average treated and counterfactual outcomes with shaded confidence intervals. - -```{r point_1, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.ife, type = "counterfactual") -``` - -By setting the option `type = "status"`, we can visualize the treatment status of all observations. We only present the label of the time by setting `axis.lab = "time"`. +After obtaining the individual treatment effects using one of the counterfactual estimators, we can weight these estimates using a constructed balanced treated sample or other user-supplied weighting schemes. -```{r status_0, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.fect, type = 'status', axis.lab = "time", cex.axis = 0.6) -``` +### Balanced treated sample -For the placebo test, the manually hided observations are marked in cyan. We can show only a sub-group's treatment status by specifying the option `id` to certain units. +**fect** also provides the option `balance.period`, which allows the calculation of the average treatment effects only for *treated* units that exhibit complete data in specified pre- and post-treatment periods. For instance, if the option is set to `balance.period = c(-3,4)`, the algorithm will calculate the average treatment effects for units that have at least four consecutive non-missing observations in the pre-treatment periods `(-3, -2, -1, 0)` and at least four consecutive non-missing observations in the post-treatment periods `(1, 2, 3, 4)`. Note that this option does not affect whether a never-treated unit enters estimation. -```{r status_p, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.fect.p, type = 'status', axis.lab = "both", id = c(101:120), cex.axis = 0.6) +```{r simdata_bal, eval=TRUE, cache = TRUE} +out.bal <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id","time"), + balance.period = c(-3, 4), force = "two-way", method = "ife", + CV = FALSE, r = 2, se = TRUE, nboots = 1000, parallel = TRUE, cores = 16) ``` -For the carryover test, the manually hidden observations are marked in light red. We can also remove grid lines by setting `gridOff = TRUE`. +We can then visualize the dynamic treatment effects using the inbuilt function `plot`. By default, it displays the dynamic treatment effects of the "balanced" sample. -```{r status_c, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.fect.c, type = 'status', axis.lab = "off", gridOff = TRUE) +```{r plot-balanced-att, fig.width = 6, fig.height = 4.5} +plot(out.bal, main = "Estimated ATT (Balanced Sample)") ``` -For the carryover test with removed observation, the removed observations are marked in yellow. +The usual plotting options can be used to adjust the balanced plot as well. -```{r status_cm, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.ife.rm.test, type = 'status', axis.lab = "off", gridOff = TRUE) +```{r plot-balanced-custom, fig.width = 6, fig.height = 4.5} +plot(out.bal, main = "Estimated ATT (Balanced Sample)", + post.color = "red", count.color = "blue") ``` ### Average cohort treatment effect **fect** allows us to estimate and visualize the ATTs for sub-groups of treated units. For example, it can draw the gap plot for units that adopt the treatment at the same time under staggered adoption, which is defined as "Cohort" in Sun & Abraham (2021). Our simulated dataset is not ideal to demonstrate this functionality because the treatment switches on and off. To improve feasibility, we define a cohort as a group of treated units that first adopt the treatment at the same time. -```{r simdata_panelview_cohort, fig.width = 6, fig.height = 4.5} -panelview(Y ~ D, data = simdata, index = c("id","time"), by.timing = TRUE, - axis.lab = "time", xlab = "Time", ylab = "Unit", +```{r simdata_panelview_cohort, fig.width = 6, fig.height = 4.5, warning = FALSE} +panelview(Y ~ D, data = sim_base, index = c("id","time"), by.timing = TRUE, + axis.lab = "time", xlab = "Time", ylab = "Unit", background = "white", main = "Simulated Data: Treatment Status") ``` -The `get.cohort()` function (originally from the **paneltools** package) can generate a new variable "Cohort" based on the timing when treated units first get treated. The new version of **fect** incorporates the feature: users no longer need to install any complementary packages to replicate the tutorial. +The `get.cohort()` function can generate a new variable "Cohort" based on the timing when treated units first get treated. ```{r get_cohort} -# devtools:: install_github("xuyiqing/paneltools" if not already installed -simdata.cohort <- get.cohort(data = simdata,D = 'D',index = c("id","time")) -print(table(simdata.cohort[,'Cohort'])) +sim_base.cohort <- get.cohort(data = sim_base,D = 'D',index = c("id","time")) +print(table(sim_base.cohort[,'Cohort'])) ``` We can also pass a list of **intervals** for first get-treated time into the `entry.time` option of `get.cohort()`. For example, we can categorize all treated units into the group that adopts the treatment between time 21 and 27, and the group that adopts the treatment in time 30 and 33. ```{r get_cohort2} -simdata.cohort2 <- get.cohort(data = simdata,D = 'D',index = c("id","time"), +sim_base.cohort2 <- get.cohort(data = sim_base,D = 'D',index = c("id","time"), entry.time = list(c(21,27),c(30,33))) -print(table(simdata.cohort2[,'Cohort'])) +print(table(sim_base.cohort2[,'Cohort'])) ``` By setting the option `group = "Cohort"`, **fect** estimates the ATT for each specified sub-group and saves it for further visualization. -```{r simdata_ife_cohort, eval = TRUE, cache = TRUE, message = FALSE, results='hide'} -out.ife.g <- fect(Y ~ D + X1 + X2, data = simdata.cohort, index = c("id","time"), - force = "two-way", method = "ife", CV = TRUE, r = c(0, 5), - se = TRUE, nboots = 200, parallel = TRUE, group = 'Cohort') -out.ife.g.p <- fect(Y ~ D + X1 + X2, data = simdata.cohort, index = c("id","time"), - force = "two-way", method = "ife", CV = FALSE, - placeboTest = TRUE, placebo.period = c(-2,0), - se = TRUE, nboots = 200, parallel = TRUE, group = 'Cohort') +```{r simdata_fe_cohort, eval = TRUE, cache = TRUE, message = FALSE, results='hide'} +out.fe.g <- fect(Y ~ D + X1 + X2, data = sim_base.cohort, index = c("id","time"), + force = "two-way", method = "fe", + se = TRUE, nboots = 1000, parallel = TRUE, cores = 16, group = 'Cohort') ``` -Then one can draw the gap plot, as well as the equivalence plot, for each sub-group. Here we present the gap plot for Cohort 22. +Then one can draw the gap plot for each sub-group. Here we present the gap plot for Cohort 22. ```{r cohort_plot1, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} -plot(out.ife.g, show.group = "Cohort:22", +plot(out.fe.g, show.group = "Cohort:22", xlim = c(-15, 10), ylim = c(-10, 10)) ``` +### User-supplied weights + +The package offers the option `W` to calculate the weighted average treatment effects. The weighting variable does not affect the estimation of fixed effects or factors. Only the weighted average treatment effects or weighted dynamic treatment effects are obtained by aggregating the treatment effects using the weight `W`. + +```{r simdata_w, eval=TRUE, cache = TRUE} +sim_base$Weight <- abs(rnorm(n = dim(sim_base)[1])) +out.w <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id","time"), + force = "two-way", method = "ife", W = 'Weight', + CV = FALSE, r = 2, se = TRUE, nboots = 1000, parallel = TRUE, cores = 16) +``` + +We can then visualize the weighted dynamic treatment effects using the inbuilt function `plot`, it by default shows the weighted dynamic treatment effects. + +```{r plot-weighted-att, fig.width = 6, fig.height = 4.5} +plot(out.w, main = "Estimated Weighted ATT") +``` + ------------------------------------------------------------------------ ## Additional notes -1. By default, the program will drop the units that have no larger than 5 observations under control, which is the reason why sometimes there are less available units in the placebo test or carryover test than in the original estimation. We can specify a preferred criteria in the option `min.T0` (default to 5). As a rule of thumb for the IFE estimator, the minimum number of observations under control for a unit should be larger than the specified number of factor `r`. +1. By default, the program will drop the units that have no larger than 5 observations under control, which is the reason why sometimes there are less available units in the placebo test or carryover test than in the original estimation. We can specify a preferred criterion in the option `min.T0` (default to 5). As a rule of thumb for the IFE estimator, the minimum number of observations under control for a unit should be larger than the specified number of factor `r`. 2. We can get replicable results by setting the option `seed` to a certain integer, no matter whether the parallel computing is used. -3. When `na.rm = FALSE` (default), the program allows observations to have missing outcomes $Y$ or covariates $X$ but decided treatment statuses $D$. Otherwise the program will drop all observations that have missing values in outcomes, treatments, or covariates. +3. When `na.rm = FALSE` (default), the program allows observations to have missing outcomes $Y$ but not $X$ or treatment statuses $D$. When `na.rm = TRUE` the program will drop all observations that have missing values in outcomes, treatments, or covariates. diff --git a/vignettes/03-ife-mc.Rmd b/vignettes/03-ife-mc.Rmd new file mode 100644 index 00000000..a79c0891 --- /dev/null +++ b/vignettes/03-ife-mc.Rmd @@ -0,0 +1,308 @@ +# Factor-Based Methods {#sec-ife-mc} + +```{r .common, include = FALSE} +source("_common.R") +``` + +```{r setup-ife-mc, echo = FALSE, message = FALSE, warning = FALSE} +set.seed(1234) +data(simdata) +``` + +When the parallel trends assumption is violated due to latent common factors with heterogeneous loadings, the FE estimator from [Chapter @sec-fect] is biased. This chapter introduces two methods that account for such latent factors: the **interactive fixed effects** (IFE) method, which explicitly models unit-specific factor loadings, and the **matrix completion** (MC) method, which uses nuclear-norm regularization to recover the low-rank structure of the untreated potential outcomes. R script used in this chapter can be downloaded [here](https://raw.githubusercontent.com/xuyiqing/fect/dev/vignettes/rscript/03-ife-mc.R). + +We use `simdata`, which includes two latent factors ($r = 2$). The FE estimator is biased on this dataset, while IFE and MC recover the correct ATT. + +## Interactive fixed effects + +In addition to FEct, **fect** supports the interactive fixed effects counterfactual (IFEct) method proposed by @Gobillon2016 and @Xu2017 and the matrix completion (MC) method proposed by @Athey2021---`method = "ife"` and `method = "mc"`, respectively. The EM algorithm is used to impute the counterfactuals of treated observations. + +For the IFE approach, we need to specify the number of factors using option `r`. By default, the algorithm will select an optimal hyper-parameter via a built-in cross-validation procedure (see the Cross-validation section below). + +We specify an interval of candidate number of unobserved factors in option `r` like `r=c(0,5)`. When cross-validation is switched off, the first element in `r` will be set as the number of factors. Below we use the MSPE criterion and search the number of factors from 0 to 5. + +```{r simdata_ife, eval=TRUE, cache = TRUE} +out.ife <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + force = "two-way", method = "ife", CV = TRUE, r = c(0, 5), + se = TRUE, nboots = 200, parallel = TRUE, cores = 16) +print(out.ife) +``` + +The figure below shows the estimated ATT using the IFE method. The cross-validation procedure selects the correct number of factors ($r=2$). + +```{r plot-att-ife, fig.width = 6, fig.height = 4.5} +plot(out.ife, main = "Estimated ATT (IFEct)") +``` + +------------------------------------------------------------------------ + +## Matrix completion + +For the MC method, we need to specify the tuning parameter in the penalty term using option `lambda`. If users don't have any prior knowledge to set candidate tuning parameters, a number of candidate tuning parameters can be generated automatically based on the information from the outcome variable. We specify the number in option `nlambda`, e.g. `nlambda = 10`. + +```{r simdata_mc, eval=TRUE, cache = TRUE} +out.mc <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + force = "two-way", method = "mc", CV = TRUE, + se = TRUE, nboots = 200, parallel = TRUE, cores = 16) + +print(out.mc) +``` + +```{r plot-att-mc, fig.width = 6, fig.height = 4.5} +plot(out.mc, main = "Estimated ATT (MC)") +``` + +::: {.callout-note appearance="simple"} +### The `em` parameter + +By default, `em = TRUE` and the EM algorithm is used to estimate the factor model when the estimation sample has missing entries. This is always the case in the default DID setting (`time.component.from = "notyettreated"`), where treated post-treatment cells are unobserved under control. For the synthetic control setting, see [Chapter @sec-gsynth]. +::: + +------------------------------------------------------------------------ + +## Cross-validation + +When using `method = "ife"` or `method = "mc"`, we need to choose a tuning parameter --- the number of factors `r` (IFE) or the regularization strength `lambda` (MC). Setting `CV = TRUE` activates the built-in cross-validation procedure. In each round, a subset of observations is masked (held out), the model is re-estimated on the remaining data, and the prediction error on the held-out set is scored. The tuning parameter that minimizes the chosen criterion is selected. + +```{r cv_ife_demo, eval=TRUE, cache=TRUE, message=FALSE, results='hide'} +out.cv <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + method = "ife", CV = TRUE, r = c(0, 5), + se = FALSE, parallel = TRUE, cores = 16) +``` + +```{r print-cv-selected-r} +cat("Selected r:", out.cv$r.cv, "\n") +``` + +### CV method + +The `cv.method` parameter controls *which observations are masked* during cross-validation. Different masking strategies test different aspects of the model. + +| `cv.method` | What is masked | What it tests | +|:---------------------|:-------------------------|:-----------------------| +| `"all_units"` | Random subsets of control observations across all units | Factor estimation quality on the full panel | +| `"treated_units"` | Random subsets from eventually-treated units' pre-treatment observations only | Counterfactual prediction quality for the target units | +| `"loo"` | One treated pre-treatment period at a time | Projection quality (legacy gsynth method) | + +::: {.callout-note appearance="simple"} +### Which `cv.method` should I use? + +`"all_units"` (default for `fect_cv` and `fect_mspe`) is the general-purpose choice --- it tests whether the model can predict held-out control observations well, and is suitable when the panel is large and the factor structure is the main concern. `"treated_units"` (default for nevertreated/gsynth) directly tests counterfactual quality for the treated group, and is preferred when prediction accuracy for treated units is the priority. `"loo"` (nevertreated only) is the original gsynth leave-one-out method; it can be unstable with few pre-treatment periods, in which case `"treated_units"` is a more robust alternative. +::: + +We provide an example below. +```{r cv_method_compare, eval=TRUE, cache=TRUE, message=FALSE, results='hide'} +out.all <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + method = "ife", CV = TRUE, r = c(0, 5), + cv.method = "all_units", se = FALSE, parallel = TRUE, cores = 16) + +out.tr <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + method = "ife", CV = TRUE, r = c(0, 5), + cv.method = "treated_units", se = FALSE, parallel = TRUE, cores = 16) +``` + +```{r print-cv-method-compare} +cat("cv.method = 'all_units': r.cv =", out.all$r.cv, "\n") +cat("cv.method = 'treated_units': r.cv =", out.tr$r.cv, "\n") +``` + +Both strategies often agree, but they can select different `r` when the factor structure matters more for one group than the other. + +### Sub-options + +The k-fold masking procedure is controlled by several sub-options that apply to both `"all_units"` and `"treated_units"`: + +| Option | Default | Description | +|:-------------------|:----------------------|:-----------------------------| +| `k` | 10 | Number of cross-validation rounds | +| `cv.prop` | 0.1 | Proportion of eligible observations held out per round | +| `cv.nobs` | 3 | Block size for structured removal (consecutive observations) | +| `cv.donut` | 0 | Periods excluded around treatment onset in the evaluation set to minimize contamination | +| `min.T0` | 5 | Minimum pre-treatment observations required per unit | + +The block removal (`cv.nobs`) addresses serial correlation: instead of masking individual observations, we mask blocks of consecutive observations. The `cv.donut` option further excludes observations near the treatment boundary from the test set to avoid contamination due to temporal correlation in data. + +### Scoring criteria + +The `criterion` parameter determines which scoring metric is used to select the best tuning parameter: + +| `criterion` | Description | When to use | +|:------------------------|:-----------------------|:-----------------------| +| `"mspe"` (default) | Mean squared prediction error | General-purpose; best for prediction accuracy | +| `"gmspe"` | Geometric mean SPE | Robust to outlier prediction errors | +| `"pc"` | Information criterion (Bai & Ng) | Model selection based on penalized fit; does not require CV rounds | +| `"moment"` | Weighted period-wise residual averages | Selects for best pre-trend fitting rather than prediction | + +```{r criterion_compare, eval=TRUE, cache=TRUE, message=FALSE, results='hide'} +out.mspe <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + method = "ife", CV = TRUE, r = c(0, 5), + criterion = "mspe", se = FALSE, parallel = TRUE, cores = 16) + +out.pc <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + method = "ife", CV = TRUE, r = c(0, 5), + criterion = "gmspe", se = FALSE, parallel = TRUE, cores = 16) +``` + +```{r print-criterion-compare} +cat("criterion = 'mspe': r.cv =", out.mspe$r.cv, "\n") +cat("criterion = 'gmspe': r.cv =", out.pc$r.cv, "\n") +``` + +::: {.callout-tip appearance="simple"} +### Selection rule + +A candidate `r` is selected over a smaller value only if its criterion score improves by more than 1%. This prevents overfitting to marginal improvements. +::: + +### Parallel computing + +Cross-validation can be computationally expensive, especially with `cv.method = "all_units"` in the nevertreated setting (which re-estimates the full factor model `k` times per candidate `r`). Parallel computing can be enabled by setting `parallel = TRUE, cores = 16` in `fect()`. + +For the nevertreated path, parallel CV auto-activates when the panel is large enough ($N_{co} \times T > 20{,}000$) and `cv.method = "all_units"`. The `"treated_units"` and `"loo"` methods are always sequential because the per-fold cost is too low to benefit from parallelization overhead. + +------------------------------------------------------------------------ + +## Diagnostics + +We provide three types of diagnostic tests: (1) a placebo test, (2) a joint test for (no) pretrend, and (3) a test for (no) carry-over effects. For each test, we support both the difference-in-means approach and the equivalence approach. The details are provided in the paper. We demonstrate each test using both the IFE and MC estimators. + +### Placebo tests + +We provide a placebo test for a settled model---hence, cross-validation is not allowed---by setting `placeboTest = TRUE`. We specify a range of pre-treatment periods as "placebo periods" in option `placebo.period` to remove observations in the specified range for model fitting, and then test whether the estimated ATT in this range is significantly different from zero. Below, we set `c(-2, 0)` as the placebo periods. + +```{r placebo_ife, eval = TRUE, cache = TRUE, message = FALSE, results='hide'} +out.ife.p <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), + force = "two-way", method = "ife", r = 2, CV = 0, + parallel = TRUE, cores = 16, se = TRUE, + nboots = 200, placeboTest = TRUE, placebo.period = c(-2, 0)) + +out.mc.p <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), + force = "two-way", method = "mc", lambda = out.mc$lambda.cv, + CV = 0, parallel = TRUE, cores = 16, se = TRUE, + nboots = 200, placeboTest = TRUE, placebo.period = c(-2, 0)) +``` + +The placebo test conducts two types of tests: + +**t test.** If t-test p-value is smaller than a pre-specified threshold (e.g. 5%), we reject the null of no-differences. Hence, the placebo test is deemed failed. + +**TOST.** The TOST checks whether the 90% confidence intervals for estimated ATTs in the placebo period exceed a pre-specified range (defined by a threshold), or the equivalence range. A TOST p-value smaller than a pre-specified threshold suggests that the null of difference bigger than the threshold is rejected; hence, the placebo test is passed. + +By default, the plot will display the p-value of the $t$-test (`stats = "placebo.p"`). Users can also add the p-value of a corresponding TOST test by setting `stats = c("placebo.p","equiv.p")`. A larger placebo p-value from a t-test and a smaller placebo TOST p-value are preferred. + +```{r placebo_ife_plot, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} +plot(out.ife.p, ylab = "Effect of D on Y", main = "Estimated ATT (IFE)", + cex.text = 0.8, stats = c("placebo.p","equiv.p")) +``` + +```{r placebo_mc_plot, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} +plot(out.mc.p, cex.text = 0.8, stats = c("placebo.p","equiv.p"), + main = "Estimated ATT (MC)") +``` + +The results in the placebo test confirm that IFEct is a better model than MC for this particular DGP. + +### LOO pre-trend test {#loo-pre-trend-test} + +Instead of using estimated ATTs for periods prior to the treatment to test for pre-trends, we recommend users employ a leave-one-out (LOO) approach (`loo = TRUE`) to consecutively hide one pre-treatment period (relative to the timing of the treatment) and repeatedly estimate the pseudo treatment effects for that pre-treatment period. The LOO approach can be understood as an extension of the placebo test. It has the benefit of providing users with a more holistic view of whether the identifying assumptions likely hold. However, as the program needs to conduct uncertainty estimates for each turn, it is much more time-consuming than the original one. + +```{r simdata_ife_loo, eval=TRUE, cache = TRUE, message = FALSE, results = 'hide'} +out.ife.loo <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + method = "ife", force = "two-way", se = TRUE, parallel = TRUE, cores = 16, nboots = 200, loo = TRUE) +out.mc.loo <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + method = "mc", force = "two-way", se = TRUE, parallel = TRUE, cores = 16, nboots = 200, loo = TRUE) +``` + +After the LOO estimation, one can plot these LOO pre-trends in the gap plot or the equivalence plot by setting `loo = TRUE` in the `plot` function. Since all pre-treatment estimates are now out-of-sample, the plot uses a uniform black color for all points (no gray/black distinction). The equivalence plots below use the LOO estimates directly. + +### Joint tests + +We now introduce two statistical tests for the presence of a pre-trend (or the lack thereof) that *jointly* assess pre-trend quality. The first test is an $F$ test for zero residual averages in the pre-treatment periods. The second test is a two-one-sided $t$ (TOST) test, a type of equivalence tests. + +**F test.** We offer a goodness-of-fit test (a variant of the $F$ test) and to gauge the presence of pre-treatment (differential) trends. A larger F-test p-value suggests a better pre-trend fitting. Users can specify a test range in option `pre.periods`. For example, `pre.periods = c(-4,0)` means that we test pre-treatment trend of the last 5 periods prior to the treatment (from period -4 to period 0). If `pre.period = NULL` (default), all pre-treatment periods in which the number of treated units exceeds the total number of treated units \* `proportion` will be included in the test. + +**TOST.** The TOST checks whether the 90% confidence intervals for estimated ATTs in the pre-treatment periods (again, subject to the `proportion` option) exceed a pre-specified range, or the equivalence range. A smaller TOST p-value suggests a better pre-trend fitting. While users can check the values of confidence intervals, we give a visualization of the equivalence test. We can plot the pre-treatment residual average with the equivalence confidence intervals by setting `type = "equiv"`. Option `tost.threshold` sets the equivalence range (the default is $0.36\sigma_{\epsilon}$ in which $\sigma_{\epsilon}$ is the standard deviation of the outcome variable after two-way fixed effects are partialed out). By setting `range = "both"`, both the minimum range (in gray) and the equivalence range (in red) are drawn. + +On the topleft corner of the graph, we show several statistics of the user's choice. User can choose which statistics to show by setting `stats = c("none", "F.stat", "F.p", "F.equiv.p", "equiv.p")` which corresponds to not showing any, the $F$ statistic, the p-value for the $F$ test, the p-value for the equivalence $F$ test, the (maximum) p-value for the TOST tests, respectively. For the gap plot, the default is `stats = "none"`. For the equivalence plot, the default is `stats = c("equiv.p, F.p")`. Users can also change the labels of statistics using the `stats.labs` options. Users can adjust its position using the `stats.pos` option, for example `stats.pos = c(-30, 4)`. To turn off the statistics, set `stats = "none"`. + +Below, we visualize the result of the joint pre-trend test for each of the two estimators using our simulated data. We use the LOO estimates computed above, which provide a more honest out-of-sample pre-trend assessments. + +::: {.callout-note appearance="simple"} +### Why LOO for pre-trend testing? + +In-sample pre-trend estimates can be misleadingly close to zero because the model is fitted to these same observations. LOO provides genuine out-of-sample estimates, giving a more honest assessment of whether the parallel trends assumption holds. +::: + +```{r pretrend_ife, eval = TRUE, cache = TRUE, fig.width = 6, fig.height = 4.5, warning = FALSE} +plot(out.ife.loo, type = "equiv", ylim = c(-4,4), loo = TRUE, + cex.legend = 0.6, main = "Testing Pre-Trend (IFEct)", cex.text = 0.8) +``` + +```{r pretrend_mc, eval = TRUE, cache = TRUE, fig.width = 6, fig.height = 4.5, warning = FALSE} +plot(out.mc.loo, type = "equiv", ylim = c(-4,4), loo = TRUE, + cex.legend = 0.6, main = "Testing Pre-Trend (MC)", cex.text = 0.8) +``` + +From the above plots, we see that IFEct passes both tests using a conventional test size (5%); and MC fails the F tests, but passes the TOST (equivalence) test. Hence, we may conclude that IFEct is a more suitable model. + +### Carryover effects + +The idea of the placebo test can be extended to testing the presence of carryover effects. Instead of hiding a few periods right before the treatment starts, we hide a few periods right after the treatment ends. If carryover effects do not exist, we would expect the average prediction error in those periods to be close to zero. To perform the carryover test, we set the option `carryoverTest = TRUE`. We can treat a range of exit-treatment periods in option `carryover.period` to remove observations in the specified range for model fitting, and then test whether the estimated ATT in this range is significantly different from zero. + +Below, we set `carryover.period = c(1, 3)`. As we deduct the treatment effect from the outcome in `simdata`, we expect the average prediction error for these removed periods to be close to zero. + +```{r carryover_ife, eval = TRUE, cache = TRUE, message = FALSE, results='hide'} +out.ife.c <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), + force = "two-way", method = "ife", r = 2, CV = 0, + parallel = TRUE, cores = 16, se = TRUE, + nboots = 200, carryoverTest = TRUE, carryover.period = c(1, 3)) + +out.mc.c <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), + force = "two-way", method = "mc", lambda = out.mc$lambda.cv, + CV = 0, parallel = TRUE, cores = 16, se = TRUE, + nboots = 200, carryoverTest = TRUE, carryover.period = c(1, 3)) +``` + +Like the placebo test, the plot will display the p-value of the carryover effect test (`stats = "carryover.p"`). Users can also add the p-value of a corresponding TOST test by setting `stats = c("carryover.p","equiv.p")`. In exit plots, pre-exit estimates are shown in black (out-of-sample) and post-exit estimates in gray (in-sample). + +```{r carryover_ife_plot, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 5} +plot(out.ife.c, type = "exit", ylim = c(-2.5,4.5), + cex.text = 0.8, main = "Carryover Effects (IFE)") +``` + +```{r carryover_mc_plot, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 5} +plot(out.mc.c, type = "exit", ylim = c(-2.5,4.5), + cex.text = 0.8, main = "Carryover Effects (MC)") +``` + +Once again, the IFE estimator outperforms the other two. + +Using real-world data, researchers will likely find that carryover effects exist. If such effects are limited, researchers can consider removing a few periods after the treatment ended for the treated units from the first-stage estimation (using the `carryover.period` option) and re-estimated the model (and re-conduct the test). We provide such an example in the paper. Here, we illustrate the option using `simdata`. + +```{r carryover_rm, eval = TRUE, cache = TRUE, message = FALSE, results='hide', fig.width = 6, fig.height = 4.5} +out.ife.rm.test <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), + force = "two-way", method = "ife", r = 2, CV = 0, + parallel = TRUE, cores = 16, se = TRUE, carryover.rm = 3, + nboots = 200, carryoverTest = TRUE, carryover.period = c(1, 3))# remove three periods + +plot(out.ife.rm.test, cex.text = 0.8, stats.pos = c(5, 2.5)) +``` + +In the above plot, the three periods in blue are dropped from the first-stage estimation of the factor model while the periods in red are reserved for the (no) carryover effects test. + +### Summary + +| Test | Purpose | Key arguments | Plot type | Statistics shown | +|:-------------|:-------------|:-------------|:-------------|:-----------------| +| Placebo test | Tests whether the model produces zero ATT in withheld pre-treatment periods | `placeboTest = TRUE`, `placebo.period = c(a, b)` | `"gap"` (default) | `placebo.p`, `equiv.p` | +| LOO pre-trend test | Out-of-sample check for pre-trends by leaving out one pre-treatment period at a time | `loo = TRUE` (in `fect()`), `loo = TRUE` (in `plot()`) | `"gap"` or `"equiv"` | `F.p`, `equiv.p` | +| Joint pre-trend test (F + TOST) | Joint assessment: F test for zero residual averages; TOST for equivalence within a threshold | `type = "equiv"` in `plot()`, `tost.threshold` | `"equiv"` | `F.p`, `F.equiv.p`, `equiv.p` | +| Carryover test | Tests whether treatment effects persist after treatment ends | `carryoverTest = TRUE`, `carryover.period = c(a, b)` | `"exit"` | `carryover.p`, `equiv.p` | + +::: {.callout-tip appearance="simple"} +- A **larger** F-test / placebo / carryover p-value suggests the model passes the test. +- A **smaller** TOST / equivalence p-value suggests the pre-trends or carryover effects are within an acceptable range. +- We recommend using `loo = TRUE` for pre-trend tests to avoid the false reassurance of in-sample fit. +- The `proportion` option controls which pre-treatment periods are included in the tests (default: periods where the number of treated units exceeds `proportion` $\times$ total treated units). +- The `tost.threshold` option sets the equivalence range for the TOST test (default: $0.36\hat{\sigma}_\epsilon$). Finding the "right" threshold is often a challenge in empirical research. +::: diff --git a/vignettes/03-plots.Rmd b/vignettes/03-plots.Rmd deleted file mode 100644 index 240a27ae..00000000 --- a/vignettes/03-plots.Rmd +++ /dev/null @@ -1,416 +0,0 @@ -# Fect Plot Options {#sec-plots} - -In this chapter, we explore various visualization options available in the **fect** package using data from @GS2020. Download the R code used in this chapter [here](rscript/03-plot.R). - ------------------------------------------------------------------------- - -`plot.fect` is an S3 method that offers various options for customizing data and results visualization. Below is a brief summary of the most commonly used options. - -- **Starting Period:** - - `start0`: If `TRUE`, shifts the time axis so that treatment begins at Period 0 instead of Period 1. -- **Confidence Intervals:** - - `plot.ci`: Options include `"none"`, `"0.9"`, or `"0.95"` to hide confidence intervals or display 90% or 95% confidence intervals. -- **Axis and Legend Customization:** - - `xlim` / `ylim`: Set the x- and y-axis ranges.\ - - `xlab` / `ylab`: Customize axis labels.\ - - `xbreaks` / `ybreaks`: Specify tick marks.\ - - `xangle` / `yangle`: Adjust the rotation angle of axis text.\ - - `legend.pos`, `legend.nrow`, `legend.labs`: Control legend placement, number of rows, and labels. -- **Theme and Text:** - - `theme.bw`: If `TRUE`, applies a black-and-white theme. - - `preset`: If `NULL`, will be the default color preset, which is mostly black and white with a bit of color. Other options include `"vibrant"` and `"grayscale"`.\ - - `cex.main`, `cex.axis`, `cex.lab`, `cex.text`: Adjust text sizes for the title, tick labels, axis labels, and annotations. -- **Lines and Bounds:** - - `color` / `est.lwidth`: Define the color and width of main lines.\ - - `lcolor` / `lwidth` / `ltype`: Set the color, width, and line type for the axes. Takes a vector, where the first value is applied the horizontal axis and the second is applied the vertical axis. If only one value is given, both axes will take on the same value. - -While these customization options are demonstrated using the default `gap` plot, they can be applied universally, with only a few exceptions. - -## Load Data - -We will be using two datasets in this chapter. As explained in @sec-panel, @GS2020 examines the mobilizing effect of minority candidates on coethnic support in U.S. congressional elections. The treatment variable indicates the presence of an Asian candidate, and the outcome variable represents the proportion of general election contributions from Asian donors. @HH2019 study the effects of indirect democracy versus direct democracy (treatment) on naturalization rates (outcome) in Switzerland using municipality-year panel data from 1991 to 2009. - -First, we load the required packages. The datasets, `hh2019` and `gs2020`, are included with the **fect** package and can be loaded using `data(fect)`. - -```{r load, message=FALSE} -# load libraries and data -library(ggplot2) -library(panelView) -library(fect) -data(fect) -ls() -``` - -## Gap Plot - -To create the gap plot, also known as the event study plot, we first apply `fect`, the fixed effects counterfactual estimator. For details, see @sec-fect. - -```{r est, cache = TRUE} -out <- fect(Y = "general_sharetotal_A_all", - D = "cand_A_all", - X = c("cand_H_all", "cand_B_all"), - index = c("district_final", "cycle"), - data = gs2020, method = "fe", - force = "two-way", se = TRUE, - parallel = TRUE, nboots = 1000) - -out.hh <- fect(nat_rate_ord ~ indirect, - data = hh2019, - index = c("bfs","year"), - method = 'fe', se = TRUE, - parallel = TRUE, nboots = 1000, - keep.sims = TRUE) -``` - -After running the model, we can plot the dynamic treatment effects over (relative) time, including confidence intervals if `se = TRUE` is specified in the estimation. Note that `type = "gap"` is the default option, so we omit it here. - -```{r} -plot(out) # the effect co-ethnic mobilization -plot(out.hh) # the effect of indirect democrazy on naturalization rate -``` - -### Starting Period - -By default, the first post-treatment period is set to 0, and the last pre-treatment period is set to -1. However, some researchers prefer to designate the former as 1 and the latter as 0. To achieve this, set `start0 = TRUE`. - -```{r begin-post-customization} -plot(out, start0 = TRUE, # Shift time so treatment begins at 0 - main = "Custom Starting Period") -``` - -### Axis and Legend - -Below, we customize the x- and y-axis ranges, labels, tick breaks, and legend. The x-axis labels are rotated for clarity. Moreover, by setting `xlim = c(-10, 1)`, the x-axis is restricted to time periods -8 to 1, with the treatment shifted to begin at period 0 instead of period 1. We also remove grid lines by setting `gridOff = TRUE`. - -```{r axis-legend-customization} -plot(out, - xlim = c(-10, 1), # only show time periods -8 to 1 - ylim = c(-0.15, 0.30), # set y-range - xlab = "Custom Time Axis", # x-axis label - ylab = "Estimated ATT", # y-axis label - xangle = 90, # rotate x-axis labels by 90° - xbreaks = seq(-10, 1, by = 2), - gridOff = TRUE, - # Label x-axis from -12 to 1 with a break of 2 - main = "Axis and Legend Customization") -``` - -### Confidence Intervals - -Below, we plot the treatment effect with 90% confidence intervals instead of 95%. - -```{r ci-raw-customization} -plot(out, plot.ci = "0.9", - main = "90% confidence intervals") -``` - -### Text and ggplot2 Theme - -This plot adjusts text sizes with a series of `cex` options and turns off the `theme.bw` option. - -```{r text-customization} -plot(out, - ylim = c(-0.15, 0.3), # set yrange - theme.bw = FALSE, # Change the color theme - cex.main = 1.25, # Scale for the main title - cex.axis = 1.2, # Axis tick label size - cex.lab = 1.2, # Axis label size - cex.legend = 1, # Legend text size - cex.text = 1.2, # Annotation text size - main = "Text and Theme Customization") -``` - -### Presets - -For convenience, we can use the `preset` argument to apply preset colors. The default is `"default"`, which is mostly black and white with a bit of color. Other options include `"vibrant"` and `"grayscale"`, which can be used to create more colorful or monochromatic plots, respectively. - -```{r preset-vibrant} -plot(out, - preset = "vibrant", # Use vibrant colors - main = "Vibrant Preset Colors: Grumbach and Sahn (2020)") -plot(out.hh, - preset = "vibrant", # Use vibrant colors - main = "Vibrant Preset Colors: Hainmueller and Hangartner (2019)") -``` - -We can change the color of the estimates (and their confidence intervals) using the `color` option. - -```{r preset-vibrant2} -plot(out.hh, - preset = "vibrant", # Use vibrant colors - color = "green4", # Color of the estimates and CIs - main = "Change Estimates' Color: Hainmueller and Hangartner (2019)") -``` - -```{r preset-grayscale} -plot(out, - preset = "grayscale", # Use grayscale colors - main = "Grayscale Preset Colors") -``` - -### Connected Estimates - -By default, the estimates are plotted as points. To connect the points with lines, set `connected = TRUE`. The width of the line and size of the points can be adjusted with `est.lwidth` and `est.pointsize`, respectively. - -```{r connected-estimates} -plot(out, - color = "green4", # color of the estimates and CIs - connected = TRUE, # Connect the points with lines - est.lwidth = 1.2, # Makes the lines thicker - est.pointsize = 3 # Makes the points larger -) -``` - -Moreover, in any plot that uses a shaded band to represent the CIs, we can set `ci.outline` to `TRUE` to draw an outline around the shaded band to improve visibility. - -```{r ci-outline} -plot(out, - connected = TRUE, - ci.outline = TRUE, - main = "The Effect of Coethnic Mobilization") # Outline the confidence interval band -plot(out.hh, - preset = "vibrant", - ci.outline = TRUE, - main = "The Effect of Indirect Democracy") # Outline the confidence interval band -``` - -### Line and Point Customization - -Here, we demonstrate how to change main and the horizontal reference lines, as well as the points and lines in the plot. - -```{r line-bound-customization} -plot(out, - est.lwidth = 1.5, # Makes the confidence intervals thicker - est.pointsize = 3, # Makes the points larger - lcolor = c("red","skyblue"), # Color for horizontal and vertical lines - lwidth = 2, # Widths of the horizontal and vertical lines - main = "Line Customization") -``` - -### Count Histogram Customization - -The count histogram on the bottom of the graph shows the number of treated units in each period. To customize its color, outline color, and opacity, we can use the `count.color`, `count.outline.color`, and `count.alpha` options, respectively. - -```{r count-histogram-customization} -plot(out, - count.color = "lightblue", # Color of the histogram bars - count.outline.color = "darkblue", # Outline color of the histogram bars - count.alpha = 0.2, # Opacity of the histogram bars - main = "Count Histogram Customization") -``` - -## Counterfactual Plot - -While the gap plot shows the *difference* (ATT) over time, researchers often want to see the **levels**: the observed outcome for the treated unit(s) and the model‑predicted counterfactual path side‑by‑side. To do this, set `type = "counterfactual"`: - -```{r counterfactual} -plot(out, type = "counterfactual", - main = "Grumbach & Sahn (2020): Treated vs. Counterfactuals", - ylab = "Proportion of Asian Donation", - legend.pos = "bottom") -``` - -```{r counterfactual_hh} -plot(out.hh, type = "counterfactual", - main = "Hainmueller & Hangartner (2019): Treated vs. Counterfactuals", - ylab = "Naturalization Rate", - legend.pos = "top") -``` - -We can change the color of the lines in this plot using `color`, which sets the color of the main line, and `counterfactual.color`, which sets the color of the counterfactual line (as well as the color of the confidence band but with more transparency). Additionally, we can add an outline to the CIs with `ci.outline = TRUE`. - -```{r counterfactual_colors} -plot(out.hh, type = "counterfactual", - main = "Hainmueller & Hangartner (2019): Treated vs. Counterfactuals", - ylab = "Naturalization Rate", - legend.pos = "bottom", - ci.outline = TRUE, # Outline the confidence interval band - color = "red3", # Color for the main line - counterfactual.color = "green4") # Color for the counterfactual line -``` - -We can also visualize the paths of the individual units by setting `raw = "all"` - -```{r counterfactual_rawall} -plot(out, type = "counterfactual", raw = "all") -``` - -Setting `raw = "band` displays the the 5-95 interpercentile range of the treated and control units. When adoption is staggered, only the band around the treated units is shown. - -```{r counterfactual_rawband} -plot(out, type = "counterfactual", raw = "band") -``` - -We can also change the colors in this plot using the same options as in the gap plot, as well as the `counterfactual.color`, `counterfactual.raw.controls.color`, `counterfactual.raw.treated.color`, and `counterfactual.linetype` options. - -```{r counterfactual_colors2} -plot(out, type = "counterfactual", - count.color = "black", # Color for the count histogram - count.alpha = 1, # Opacity for the count histogram - color = "red", # Color for the main line - counterfactual.color = "purple", # Color for the counterfactual line - counterfactual.raw.treated.color = "orange", # Color for the treated units - counterfactual.linetype = "dotted", # Line type for the counterfactual line - raw = "all", - main = "Counterfactual Plot with Custom Colors") -``` - -## Cumulative Effects - -We can also plot cumulative effects by plotting the output of the `effect()` function. Note that this is only well-defined when there are no treatment reversals, that is, all treated units remain treated for the duration of the study. Additionally, we must set `keep.sims = TRUE` to keep the unit-level bootstrap results. We first apply it to `hh2019`. - -```{r} -plot(effect(out.hh), main = "Cumulative Effect of Indirect Democracy", - ylab = "Cumulative Effect on Naturalization Rate") -``` - -Since the `gs2020` datset has treatment reversals, we will first subset the units that remained treated throughout the study period. We do this by checking for any instances where the treatment variable changes from 1 to 0 within a unit. - -```{r} -# flag units that ever have a 1 to 0 change in d -rev_flag <- tapply(gs2020[["cand_A_all"]], - gs2020[["district_final"]], - function(x) any(diff(x) < 0)) - -# units with no reversals -good_units <- names(rev_flag)[!rev_flag] - -# subset the desired rows -gs2020_no_reversals <- gs2020[gs2020[["district_final"]] %in% good_units, ] - -``` - -Next we will estimate the function again on these units only. - -```{r no-reversals-est, cache = TRUE} -out_no_reversals <- fect(Y = "general_sharetotal_A_all", - D = "cand_A_all" , - X = c("cand_H_all", "cand_B_all") , - index = c("district_final", "cycle"), - data = gs2020_no_reversals, - method = "fe", - force = "two-way", - se = TRUE, parallel = TRUE, - nboots = 100, - keep.sims = TRUE) -``` - -Finally, we will plot the cumulative effects. - -```{r cumulative-effects} -plot(effect(out_no_reversals), xlim = c(1, 2)) -``` - -## Pretrend Tests - -We can conduct several tests to shed light on (not directly test) the parallel trends (PT) assumption, including the equivalence test and the placebo test. For details, see @sec-fect or @LWX2022. - -### Equivalence Test - -In the equivalence plot (`type = "equiv"`), the equivalence bound is defined by the two-one-sided test (TOST) threshold. For example, in the plot below, the bound is set by `tost.threshold = 0.1`, with lines at -0.1 and 0.1. This threshold should be set based on the magnitude of the ATT or the standard deviation of the outcome (or residualized outcome). - -The `bound` option has four choices: `"none"`, `"min"`, `"equiv"`, or `"both"`. When set to `"none"`, no bound is displayed. - -```{r} -plot(out, type = "equiv", bound = "equiv", tost.threshold = 0.1, - ylim = c(-0.15, 0.15)) -``` - -The `"min"` displays the minimum range bound based on the maximum absolute pre‐treatment residual (e.g., if the largest pre-treatment estimate is 0.3, lines at -0.3 and 0.3). - -```{r} -plot(out, type = "equiv", bound = "min", ylim = c(-0.15, 0.15)) -``` - -We can plot both the minimum range and the equivalence bound with `bound = "both"`, which is also the default option. - -```{r} -plot(out, type = "equiv", tost.threshold = 0.1, ylim = c(-0.15, 0.15)) -``` - -We use the `stats` argument to select which results to display, label them with `stats.labs`, and position the legend with `stats.pos`. Setting `show.stats = FALSE` hides the test results entirely. - -```{r stats-customization} -plot(out, type = "equiv", - ylim = c(-0.25, 0.25), - stats = c("F.p", "equiv.p"), - stats.labs = c("F Test P-value", "Equivalence P-value"), - stats.pos = c(-8, 0.2), # (x, y) position for the stats text - show.stats = TRUE, # Can be switched off to hide all test stats - main = "Statistical Test Annotations") -``` - -### Placebo Test - -A placebo test evaluates whether the "fake" ATT is statistically distinguishable in a placebo period. It artificially assigns treatment during placebo periods and estimates the "placebo effect" in those periods. - -Therefore, the model must be re-run. Below, we set `placebo.period = c(-2, 0)`, specifying the pre-treatment periods used for the placebo test. - -```{r placebo, cache = TRUE} -out_fe_placebo <- fect(Y = "general_sharetotal_A_all", D = "cand_A_all", X = c("cand_H_all", "cand_B_all"), data = gs2020, - index = c("district_final", "cycle"), force = "two-way", - method = "fe", CV = FALSE, parallel = TRUE, - se = TRUE, nboots = 1000, placeboTest = TRUE, - placebo.period = c(-2, 0)) - -plot(out_fe_placebo) -``` - -The plot shows the estimated "effects" in the pre-treatment periods (placebo effects). The blue lines in the pre-treatment period suggest that we do not observe significant effects of the treatment in the pre-periods. - -We can also change the color of the placebo periods by using the `placebo.color` argument. Colors for many other plot types can also be adjusted in a similar way, including, but not limited to, the carryover and box plots. - -```{r} -plot(out_fe_placebo, placebo.color = "green4") -``` - -## Carryover Effects - -One type of plot rarely seen in the empirical literature is how the difference between treatment and control groups evolves after treatment ends. We call it the `"exit"` plot, where the x-axis represents time relative to treatment exit. In contrast, the `"gap"` plot focuses on treatment entry. The `"exit"` plot is essential for assessing potential carryover effects. - -```{r} -plot(out_fe_placebo, type = "exit") -``` - -The test for carryover effects examines whether the treatment effect persists after treatment ends. It artificially labels several post-treatment periods as treated and estimates the "placebo effect" in those periods. By setting `carryover.period = c(1, 3)`, we specify a placebo period that includes three post-treatment periods. If the treatment effect is purely contemporaneous (i.e., there are no carryover effects), the test will not reject the null hypothesis. In this application, the average carryover effect is close to zero and statistically indistinguishable from zero. - -```{r carryover, cache = TRUE} -out_fe_carryover <- fect(Y = "general_sharetotal_A_all", D = "cand_A_all", X = c("cand_H_all", "cand_B_all"), data = gs2020, - index = c("district_final", "cycle"), force = "two-way", - parallel = TRUE, se = TRUE, CV = FALSE, - nboots = 1000, carryoverTest = TRUE, - carryover.period = c(1, 3)) -plot(out_fe_carryover) -``` - -## Status Plot - -The status plot (`type = "status"`) displays the treatment status by period for all units in a similar fashion to `panelView`. Each of the indicator colors can be customized using the `status.*.color` options. - -```{r status} -plot(out_fe_carryover, type = "status", - status.treat.color = "#D55E00", # Color for treated units - status.control.color = "#0072B2", # Color for control units - status.carryover.color = "#CC79A7", # Color for carryover units - status.missing.color = "#009E73", # Color for missing data - status.background.color = "#F3EAD2", # Background color - main = "Status Plot") -``` - -## Effect Heterogeneity - -We provide two ways to visualize treatment effect heterogeneity: a `"box"` plot, which shows the distribution of individual treatment effects, and a `"calendar"` plot, which depicts the ATT conditional on calendar time. We plan to expand this functionality to allow for more pre-treatment covariates soon. - -In the box plot, the box in each period represents the range of the middle 50% of the individual effects, while the whiskers show the 2.5%–95% quantiles and the horizontal line represents the median. - -```{r} -plot(out, type = "box", xlim = c(-12, 3)) -``` - -In the calendar plot, the blue ribbon represents a loess fit of the conditional ATT, with 95% confidence intervals. - -```{r} -plot(out, type = "calendar", main = "The Effect of Coethnic Mobilization") -plot(out.hh, type = "calendar", xlim = c(1995, 2009), - main = "The Effect of Indirect Democracy") -``` diff --git a/vignettes/04-cfe.Rmd b/vignettes/04-cfe.Rmd new file mode 100644 index 00000000..08a34d9a --- /dev/null +++ b/vignettes/04-cfe.Rmd @@ -0,0 +1,448 @@ +# Complex Fixed Effects {#sec-cfe} + +The **Complex Fixed Effects (CFE)** estimator extends the standard two-way fixed effects counterfactual by incorporating additional model components: extra additive fixed effects, time-invariant covariates with time-varying coefficients, unit-specific loadings on known time trends, and interactive fixed effects (latent factors). Each component helps to relax specific assumptions about the data-generating process, and this chapter introduces them one at a time. R script used in this chapter can be downloaded [here](https://raw.githubusercontent.com/xuyiqing/fect/dev/vignettes/rscript/04-cfe.R). + +In [Chapter @sec-ife-mc], we introduced factor-based methods (IFE and MC) that model latent common factors. CFE generalizes this framework by allowing researchers to incorporate additional observed structure alongside latent factors. + +```{r .common, include = FALSE} +source("_common.R") +``` + +```{r setup-cfe, echo = FALSE} +set.seed(1234) +``` + +```{r load-packages-cfe, message = FALSE, warning = FALSE} +data(simdata) +data(sim_region) +data(sim_linear) +data(sim_trend) +``` + +------------------------------------------------------------------------ + +## When to use CFE + +The standard IFEct estimator (`method = "ife"`) assumes that the untreated potential outcome is driven by observed time-varying covariates, interactive fixed effects, plus unit and time fixed effects: + +$$Y_{it}^{0} = \mu + X_{it}'\beta + \sum_{m=1}^{r} \lambda_{im} f_{tm} + \alpha_i + \xi_t + e_{it}$$ + +This may be insufficient when: + +1. Units belong to groups with group-level time shocks (additional FEs needed), +2. Time-invariant characteristics have effects that change over time ($Z'\gamma$), +3. Units follow known time trends with heterogeneous intensity ($\kappa' Q$), + +The CFE estimator models the untreated potential outcome as: + +$$Y_{it}^{0} = \mu + X_{it}'\beta + \sum_{m=1}^{r} \lambda_{im} f_{tm} + \alpha_i + \xi_t + \omega_{k(i)} + Z_i'\gamma_{g(t)} + \kappa_i' Q_t + e_{it}$$ + +where: + +- $\mu$ is the grand mean, +- $X_{it}'\beta$ captures time-varying covariates with constant coefficients, +- $\lambda_{im} f_{tm}$ are interactive fixed effects (latent factors), +- $\alpha_i$ and $\xi_t$ are unit and time fixed effects, +- $\omega_{k(i)}$ are additional group-level fixed effects, +- $Z_i'\gamma_{g(t)}$ captures time-invariant covariates with coefficients that vary by time group, +- $\kappa_i' Q_t$ captures unit-specific loadings on known time trends, +- $e_{it}$ is the idiosyncratic error. + +::: {.callout-tip appearance="simple"} +Most of the above elements, except additional group-level fixed effects $\omega_{k(i)}$, can be captured by an interactive fixed effects model. Knowing $Z_i$ or $Q_t$, however, can improve efficiency. In practice, we often do not know the exact functional form of $Q$, and $Z$ may suffer from severe measurement error. Therefore, including these terms may not yield the expected gains and may instead inflate variance. + +In general, we recommend a data-driven approach to model selection, such as using `fect_mspe()`, which is based on cross-validation. +::: + +### Observed vs. unobserved + +| Component | What the researcher provides | What **fect** estimates | +|:----------------|:-----------------------------|:------------------------| +| $X_{it}$ (time-varying covariates) | Covariates in the formula (right-hand side) | $\beta$ --- constant coefficients | +| $Z_i$ (time-invariant covariates) | Column names via `Z` argument | $\gamma_{g(t)}$ --- coefficients that vary by time group | +| $Q_t$ (known time basis) | Either column names via `Q`, or auto-generated via `Q.type` | $\kappa_i$ --- unit-specific loadings | +| Group indicators | Extra elements in `index` | $\omega_k$ --- group fixed effects | +| Latent factors | Number of factors via `r` | Both $\lambda_i$ (loadings) and $f_t$ (factors) | + +### Algorithm + +The model is estimated via an EM-style block coordinate descent algorithm. The algorithm iterates between imputing missing or treated entries with current fitted values, updating each component block --- covariates ($\beta$), time-invariant covariate coefficients ($\gamma$), unit-specific trend loadings ($\kappa$), additive fixed effects ($\alpha$, $\xi$, $\omega$), and interactive fixed effects via SVD ($\lambda$, $f$) --- and checking convergence. The entire procedure is implemented in C++ for computational efficiency. + +::: {.callout-tip appearance="simple"} +The CFE estimator is activated by `method = "cfe"`. The key arguments that control the model components are: `index` (additional FEs), `Z` and `gamma` (time-invariant covariates), `Q.type` or `Q` and `kappa` (time trends), and `r` (latent factors). The rest of this chapter introduces each component individually. +::: + +------------------------------------------------------------------------ + +## Additional fixed effects + +When units belong to groups --- such as regions, industry sectors, or cohorts --- group-level time shocks can affect outcomes and correlate with treatment assignment. In unbalanced panels where units enter or exit at different times, the group structure becomes especially important: without accounting for group effects, the counterfactual imputation is biased and placebo tests fail. + +### Data-generating process + +The untreated potential outcome in this DGP follows: + +$$Y_{it}^{0} = \alpha_i + \xi_t + \delta_{g(i),t} + e_{it}$$ + +where $\alpha_i$ is a unit fixed effect, $\xi_t$ is a time fixed effect, and $\delta_{g(i),t}$ is a region-specific time effect that serves as the confounding source. Because treatment assignment and timing both depend on region, $\delta_{g(i),t}$ correlates with treatment status --- creating exactly the kind of bias that additional fixed effects are designed to absorb. + +We generate an unbalanced panel with 300 units belonging to 5 regions. Each region has a distinct linear time trend, and treatment probability and timing both depend on region. Units in higher-numbered regions enter the panel later, creating the kind of unbalancedness common in real datasets (e.g., firms entering markets, countries joining surveys). + +The `sim_region` dataset ships with the package. It is an unbalanced panel with 500 units belonging to 5 regions. The DGP generation script is in `data-raw/sim_region.R`. + +```{r cfe-42-load, eval = TRUE} +head(sim_region) +``` + +### Without additional fixed effects + +We first try the standard FE estimator, which ignores the region-level shocks: + +```{r cfe-42-fe-only, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'} +out.fe.only <- fect(Y ~ D, data = sim_region, + index = c("id", "time"), + method = "fe", force = "two-way", + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) +``` + +```{r cfe-42-fe-only-plot, fig.width = 6, fig.height = 4.5} +plot(out.fe.only, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "FE Only — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) +``` + +The placebo test detects significant pre-trends (low p-value) because the model fails to account for region-level confounding. In the unbalanced panel, group-level time trends $\delta_{g(i),t}$ interact with the differential entry times across regions to create spurious pre-trends that the standard two-way fixed effects model cannot absorb. + +### With additional fixed effects + +Now we interact region with time to create group×period fixed effects, which absorb the region-specific time shocks $\delta_{g(i),t}$. A simple region intercept FE cannot capture these shocks because they vary across both regions and time periods. By passing `region_time` as the third index element, the CFE estimator absorbs the full set of region-by-period effects: + +```{r cfe-42-with-region, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'} +out.cfe.region <- fect(Y ~ D, data = sim_region, + index = c("id", "time", "region_time"), + method = "cfe", force = "two-way", + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) +``` + +```{r cfe-42-with-region-plot, fig.width = 6, fig.height = 4.5} +plot(out.cfe.region, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "CFE with Region×Time FE — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) +``` + +The placebo test now passes (high p-value), confirming that the region×time fixed effects absorb the group-level confounding. By including the interacted `region_time` variable in the `index` argument, the CFE estimator absorbs the full set of region-specific time shocks $\delta_{g(i),t}$ during counterfactual imputation, removing the bias that was driving the spurious pre-trends. + +::: {.callout-note appearance="simple"} +In the `index` argument, elements beyond the first two (unit, time) are treated as additional fixed-effect grouping variables. You can include multiple additional groupings: `index = c("id", "time", "region_time", "sector_time")`. +::: + +------------------------------------------------------------------------ + +## Time-invariant covariates with time-varying coefficients + +When time-invariant unit characteristics $Z_i$ (e.g., baseline GDP, initial population) have effects that change over time, a simple additive control is insufficient. The CFE model allows $Z_i'\gamma_{g(t)}$, where $\gamma$ varies by time group, capturing the time-varying nature of these effects. + +### The challenge with interactive structures + +We use the existing `simdata` dataset. In the true DGP, $Y_{it}^{0}$ includes the interactive term $L_{1,i} \cdot F_{1,t}$ --- a product of the unit-level factor loading $L_1$ and the time-varying factor $F_1$. If we observe $L_1$ as a covariate but estimate it with a constant coefficient, we miss the time-varying nature of its effect. The CFE $Z/\gamma$ mechanism handles this. + +First, we estimate the FE model, which ignores the interactive structure entirely: + +```{r cfe-43-fe-baseline, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'} +out.fe.base <- fect(Y ~ D + X1 + X2, data = simdata, + index = c("id", "time"), + method = "fe", force = "two-way", + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) +``` + +```{r cfe-43-fe-baseline-plot, fig.width = 6, fig.height = 4.5} +plot(out.fe.base, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "FE Only (simdata) — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) +``` + +The FE estimator fails the placebo test because it cannot account for the interactive term $L_1 \cdot F_1$. Now we include $L_1$ as a time-invariant covariate with time-varying coefficients. We create a `gamma` variable --- here we use `time` itself as the grouping variable, which gives the most flexible specification (one coefficient per period): + +```{r cfe-43-gamma-setup, eval = TRUE} +simdata$gamma_t <- simdata$time +``` + +```{r cfe-43-with-z, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'} +out.cfe.z <- fect(Y ~ D + X1 + X2, data = simdata, + index = c("id", "time"), + method = "cfe", force = "two-way", + Z = "L1", gamma = "gamma_t", + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) +``` + +```{r cfe-43-with-z-plot, fig.width = 6, fig.height = 4.5} +plot(out.cfe.z, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "CFE with Z = L1 — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) +``` + +By allowing $L_1$'s coefficient to vary across time periods, CFE approximates the true interactive structure $L_{1,i} \cdot F_{1,t}$. The placebo test shows improvement. However, there is still a second latent factor $L_2 \cdot F_2$ that is not captured --- we will address this in Section 4.5. + +::: {.callout-note appearance="simple"} +The `Z` argument takes a character vector of column names for time-invariant covariates. The `gamma` argument specifies a column that defines the time grouping for the $Z$ coefficients. Using `time` as the gamma variable gives the most flexible specification; coarser groupings (e.g., early/middle/late) reduce the number of parameters. +::: + +------------------------------------------------------------------------ + +## Unit-specific time trends + +When all units follow the same temporal pattern but the amplitude differs across units --- and units with stronger trends are more likely to be treated --- the counterfactual imputation from a standard two-way FE model is biased. The CFE estimator can model unit-specific loadings on known time bases via the `Q.type` argument. Options include `"linear"`, `"quadratic"`, `"cubic"`, and `"bspline"`. We illustrate with two examples of increasing complexity. + +### Linear time trends + +The simplest case is a linear time trend with unit-specific slopes. We generate a block DID panel with 200 units (80 treated, 120 control), where treated units have systematically steeper linear trends. + +The `sim_linear` dataset ships with the package. It has 200 units (80 treated, 120 control), 50 time periods, with block treatment at period 41. The DGP generation script is in `data-raw/sim_linear.R`. + +```{r cfe-44-linear-load, eval = TRUE} +head(sim_linear) +``` + +Without accounting for the unit-specific linear trends, the FE estimator fails the placebo test: + +```{r cfe-44-lin-fe-only, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'} +out.fe.lin <- fect(Y ~ D, data = sim_linear, + index = c("id", "time"), + method = "fe", force = "two-way", + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) +``` + +```{r cfe-44-lin-fe-only-plot, fig.width = 6, fig.height = 4.5} +plot(out.fe.lin, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "FE Only (Linear Trend DGP) — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) +``` + +Now we use `Q.type = "linear"` to allow unit-specific loadings on a linear time basis. Because the true DGP is exactly linear, this specification matches perfectly: + +```{r cfe-44-lin-cfe, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'} +out.cfe.lin <- fect(Y ~ D, data = sim_linear, + index = c("id", "time"), + method = "cfe", force = "two-way", + Q.type = "linear", + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) +``` + +```{r cfe-44-lin-cfe-plot, fig.width = 6, fig.height = 4.5} +plot(out.cfe.lin, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "CFE with Linear Trend — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) +``` + +The placebo test passes because the linear basis exactly matches the true trend shape, and the CFE estimator correctly recovers each unit's slope $\kappa_i$. + +### Nonlinear time trends + +When the true trend is nonlinear, a linear basis is insufficient. We now generate a DGP with a sinusoidal time trend --- a half-cycle over the panel --- and unit-specific amplitudes: + +The `sim_trend` dataset ships with the package. It has the same block DID structure as `sim_linear` but with a sinusoidal (half-cycle) time trend instead of a linear one. The DGP generation script is in `data-raw/sim_trend.R`. + +```{r cfe-44-sin-load, eval = TRUE} +head(sim_trend) +``` + +The FE estimator fails the placebo test because it cannot capture the nonlinear trend: + +```{r cfe-44-sin-fe-only, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'} +out.fe.trend <- fect(Y ~ D, data = sim_trend, + index = c("id", "time"), + method = "fe", force = "two-way", + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) +``` + +```{r cfe-44-sin-fe-only-plot, fig.width = 6, fig.height = 4.5} +plot(out.fe.trend, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "FE Only (Sin Trend DGP) — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) +``` + +Now we use `Q.type = "bspline"`, which generates a B-spline basis that can approximate smooth nonlinear functions: + +```{r cfe-44-sin-bspline, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'} +out.cfe.bs <- fect(Y ~ D, data = sim_trend, + index = c("id", "time"), + method = "cfe", force = "two-way", + Q.type = "bspline", + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) +``` + +```{r cfe-44-sin-bspline-plot, fig.width = 6, fig.height = 4.5} +plot(out.cfe.bs, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "CFE with B-spline Trend — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) +``` + +The B-spline specification should pass or nearly pass the placebo test. Some bias may remain because B-splines approximate the true sinusoidal trend rather than matching it exactly. + +::: {.callout-note appearance="simple"} +The `Q.type` argument accepts `"linear"`, `"quadratic"`, `"cubic"`, or `"bspline"`. Multiple types can be combined: `Q.type = c("linear", "quadratic")`. For B-splines, `Q.bspline.degree` controls the polynomial degree (default chosen automatically based on the number of time periods). +::: + +------------------------------------------------------------------------ + +## CFE with factors + +Sometimes the data contains both observed structure (known covariates whose effects vary over time) and unobserved latent factors. The CFE estimator can combine $Z'\gamma$ and interactive fixed effects $\lambda' f$ in a single model. + +We use `simdata`, which has two latent factors ($L_1, F_1$ and $L_2, F_2$). We pretend $L_1$ is an observed time-invariant covariate and model the remaining factor structure with $r = 1$ latent factor. + +```{r cfe-45-gamma-setup, eval = TRUE} +simdata$gamma_t <- simdata$time +``` + +### Model comparison via `fect_mspe` + +We fit four models and compare their out-of-sample prediction accuracy: + +1. FE only (baseline), +2. CFE with $Z = L_1$ only (one observed loading, no factors), +3. CFE with $Z = L_1$ + 1 latent factor (correct specification), +4. IFE with 2 latent factors (correct for IFE, but less efficient than CFE). + +```{r cfe-45-fit-models, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'} +# Model 1: FE only +out.fe <- fect(Y ~ D + X1 + X2, data = simdata, + index = c("id", "time"), + method = "fe", force = "two-way", se = FALSE) + +# Model 2: CFE with Z = L1 only +out.cfe.z.only <- fect(Y ~ D + X1 + X2, data = simdata, + index = c("id", "time"), + method = "cfe", force = "two-way", + Z = "L1", gamma = "gamma_t", + se = FALSE) + +# Model 3: CFE with Z = L1 + 1 factor +out.cfe.z.f1 <- fect(Y ~ D + X1 + X2, data = simdata, + index = c("id", "time"), + method = "cfe", force = "two-way", + Z = "L1", gamma = "gamma_t", + r = 1, se = FALSE) + +# Model 4: IFE with 2 factors +out.ife.r2 <- fect(Y ~ D + X1 + X2, data = simdata, + index = c("id", "time"), + method = "ife", force = "two-way", + r = 2, se = FALSE) +``` + +```{r cfe-45-mspe, eval = TRUE, cache = TRUE} +mspe.out <- fect_mspe( + list(FE = out.fe, + CFE_Z = out.cfe.z.only, + CFE_Z_F1 = out.cfe.z.f1, + IFE_r2 = out.ife.r2), + seed = 1234) +print(mspe.out$summary[, c("Model", "MSPE", "RMSE", "MAD")]) +``` + +All three metrics consistently rank the CFE model with observed $Z$ and one latent factor as the best specification. CFE with $Z + 1$ factor has the lowest MSPE because it uses observed information ($L_1$) efficiently and estimates only the remaining unobserved factor ($L_2$). The IFE with $r=2$ should also do well but slightly worse, since it estimates both factors entirely from the data. + +### Best model: placebo test + +```{r cfe-45-best-placebo, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'} +out.cfe.best <- fect(Y ~ D + X1 + X2, data = simdata, + index = c("id", "time"), + method = "cfe", force = "two-way", + Z = "L1", gamma = "gamma_t", + r = 1, + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) +``` + +```{r cfe-45-best-placebo-plot, fig.width = 6, fig.height = 4.5} +plot(out.cfe.best, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "CFE (Z + 1 Factor) — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) +``` + +The placebo test passes, confirming that the CFE model with one observed loading and one latent factor adequately captures the DGP. + +::: {.callout-note appearance="simple"} +`fect_mspe()` compares out-of-sample prediction accuracy across different model specifications. A lower MSPE indicates better counterfactual prediction. This is useful for selecting among CFE configurations when the true model is unknown. +::: + +------------------------------------------------------------------------ + +## Grouped coefficients + +In some applications, the $Z$-$\gamma$ or $\kappa$-$Q$ coefficients should not all share the same grouping structure. For example: + +- **`Z.param`**: Suppose you have two time-invariant covariates --- baseline GDP and initial population --- and their effects change at different rates. GDP effects may shift by decade, while population effects shift by political era. `Z.param` allows you to assign different gamma groupings to different $Z$ variables. +- **`Q.param`**: Similarly, if you specify multiple $Q$ time bases, `Q.param` controls which unit-grouping variable is used for each $Q$ basis. + +### `Z.param` syntax + +```{r cfe-46-zparam-example, eval = FALSE} +# Example with Z.param (not run — requires appropriate data) +# out <- fect(Y ~ D, data = mydata, +# index = c("unit", "time"), +# method = "cfe", force = "two-way", +# Z = c("baseline_gdp", "baseline_pop"), +# gamma = c("decade", "political_era"), +# Z.param = list(decade = "baseline_gdp", +# political_era = "baseline_pop")) +``` + +This means: `baseline_gdp` uses `decade` for its time grouping, while `baseline_pop` uses `political_era`. + +### `Q.param` syntax + +``` +Q.param = list(sector = c("Q_linear", "Q_quadratic"), region = "Q_spline") +``` + +This means: the linear and quadratic trend bases are grouped by sector, while the spline basis is grouped by region. + +### When to use grouped coefficients + +- Use `Z.param` when different covariates' effects change at different timescales. +- Use `Q.param` when different trend components should vary by different unit groupings. +- Without `Z.param`/`Q.param`, all $Z$ variables share all gamma groupings (fully crossed), and all $Q$ variables share all kappa groupings --- this may overparameterize the model. + +::: {.callout-note appearance="simple"} +`Z.param` and `Q.param` are optional. Without them, all $Z$ (or $Q$) variables are assigned to all gamma (or kappa) groupings. Use them to impose structure when you have domain knowledge about which covariates should share which groupings. +::: + +------------------------------------------------------------------------ + +## Summary of CFE-specific arguments + +| Argument | Type | Description | +|:-----------------------|:----------------|:------------------------------| +| `method = "cfe"` | character | Activates the CFE estimator | +| `index` | character vector | `c(unit, time, ...)` --- elements beyond the first two are extra additive FEs | +| `Z` | character vector | Names of time-invariant covariate columns | +| `gamma` | character vector | Names of columns defining time grouping for $Z$ coefficients | +| `Q` | character vector | Names of known time trend columns (user-supplied) | +| `kappa` | character vector | Names of columns defining unit/group assignment for $Q$ loadings | +| `Q.type` | character vector | Auto-generate $Q$: `"linear"`, `"quadratic"`, `"cubic"`, `"bspline"` | +| `Q.bspline.degree` | integer | Degree of B-spline when `Q.type = "bspline"` (default: auto) | +| `Z.param` | named list | Block assignment: which $Z$ columns share which $\gamma$ grouping | +| `Q.param` | named list | Block assignment: which $Q$ columns share which $\kappa$ grouping | +| `r` | integer or vector | Number of latent factors; use `c(0, 5)` with `CV = TRUE` to select | +| `force` | character | Additive FE: `"none"`, `"unit"`, `"time"`, `"two-way"` | + +The CFE estimator combines all these components into a single unified model. Each component relaxes a specific assumption about the data-generating process. In practice, researchers should use `fect_mspe()` (Section 4.5) to compare specifications and placebo tests to validate the chosen model. diff --git a/vignettes/05-hte.Rmd b/vignettes/05-hte.Rmd index b6fcd477..99409115 100644 --- a/vignettes/05-hte.Rmd +++ b/vignettes/05-hte.Rmd @@ -1,303 +1,141 @@ # Effect Heterogeneity {#sec-hte} -In this chapter, we introduce the **causal moderation** framework in **fect**, which allows researchers to formally model how treatment effects vary with a moderator variable. We distinguish between two conceptually distinct quantities of interest --- *effect modification* and *causal moderation* --- and show how **fect** estimates each. This chapter draws on the theoretical framework in @ZQX2025. Download the R code used in this chapter [here](rscript/05-hte.R). - -```{r echo = FALSE} -set.seed(1234) -rm(list = ls()) +```{r .common, include = FALSE} +source("_common.R") ``` -```{r message = FALSE, warning = FALSE} -library(fect) -data(fect) +```{r setup-hte, echo = FALSE, message = FALSE, warning = FALSE} +set.seed(1234) +data(sim_base) ``` ------------------------------------------------------------------------- - -## Conceptual Framework - -A prevalent approach for examining treatment effect heterogeneity in political science is the two-way fixed effects (TWFE) model with a multiplicative interaction term --- the *linear interaction model*: -$$ -Y_{it} = \beta_0 + \tau\, D_{it}\, M_{it} + \beta_D\, D_{it} + \beta_M\, M_{it} + \beta_X\, X_{it} + \alpha_i + \lambda_t + \epsilon_{it} -$$ -where $Y_{it}$ is the outcome, $D_{it}$ is the treatment indicator, $M_{it}$ is the moderator of interest, $X_{it}$ is a vector of covariates, and $\alpha_i, \lambda_t$ are unit and time fixed effects. The coefficient $\tau$ is typically interpreted as the extent to which the treatment effect varies with $M$. - -Despite its widespread use, this model conflates two conceptually distinct questions: - -1. **Effect modification**: How does the *average treatment effect* vary across subpopulations defined by the moderator? -2. **Causal moderation**: Does exogenously changing the moderator *causally alter* the treatment effect? - -### Effect Modification - -Effect modification describes a *correlational* relationship: the moderator serves as a signal for different underlying conditions. For example, if we study how railway access ($D$) affects separatist conflict ($Y$), per capita GDP ($M$) might serve as an effect modifier. Regions with low GDP may respond differently to railway access, but this is because GDP proxies for deeper economic structures --- *experimentally increasing* GDP alone may not change the treatment effect. - -Formally, the **Conditional Marginal Effect** (CME) for effect modification is: -$$ -\tau_{\text{em},t}^{\text{CME}}(m) = \mathbb{E}\!\big[Y_t(1, M_t) - Y_t(0, M_t) \mid D_t = 1,\, M_t = m\big] -$$ -where $Y_t(d, M_t)$ is the potential outcome when treatment is set to $d$ while the moderator takes its naturally occurring value. The key feature is that $M_t$ remains at its *observed* level; we simply condition on it. - -### Causal Moderation - -Causal moderation describes a *mechanistic* relationship: the moderator has a direct causal influence on the treatment effect. For instance, the quality of democratic institutions ($M$) may directly protect minority rights, thereby *causing* a different reaction to a policy ($D$). Exogenously changing $M$ from "low" to "high" would genuinely alter the treatment effect. - -Formally, the CME for causal moderation is: -$$ -\tau_{\text{cm},t}^{\text{CME}}(m) = \mathbb{E}\!\big[Y_t(1, m) - Y_t(0, m) \mid D_t = 1\big] -$$ -where both the treatment $d$ and the moderator $m$ are *intervened upon*. Unlike effect modification, the moderator is set to a hypothetical value $m$, and we average over the treated population unconditionally. - -### When Do They Coincide? - -Under the **ignorability of $M$** assumption --- i.e., there are no causal links between unobserved confounders $U$ and the moderator $M$, nor between $D$ and $M$, nor between $X$ and $M$ --- the two estimands are numerically identical at the aggregate level. In practice, the divergence between the two estimates provides a diagnostic: if $\hat{\tau}_{\text{em}}^{\text{CME}}(m) \approx \hat{\tau}_{\text{cm}}^{\text{CME}}(m)$ for all $m$, the moderator primarily acts as a causal moderator; a large divergence suggests it functions more as an effect modifier (a proxy for other causal drivers). - -### Aggregation - -The period-specific CMEs are aggregated across time: -$$ -\tau_{\text{em}}^{\text{CME}}(m) = \sum_t \omega_t^{\text{em}}(m)\, \tau_{\text{em},t}^{\text{CME}}(m), \quad -\tau_{\text{cm}}^{\text{CME}}(m) = \sum_t \omega_t^{\text{cm}}(m)\, \tau_{\text{cm},t}^{\text{CME}}(m) -$$ -where $\omega_t^{\text{em}}(m)$ and $\omega_t^{\text{cm}}(m)$ are appropriate weights summing to one. The effect modification and causal moderation estimands are then: -$$ -\tau_{\text{em}}(m, m') = \tau_{\text{em}}^{\text{CME}}(m) - \tau_{\text{em}}^{\text{CME}}(m'), \quad -\tau_{\text{cm}}(m, m') = \tau_{\text{cm}}^{\text{CME}}(m) - \tau_{\text{cm}}^{\text{CME}}(m') -$$ - ------------------------------------------------------------------------- - -## Identification Assumptions - -The identification strategy for both estimands relies on a Directed Acyclic Graph (DAG) representation of the data-generating process. Following @Pearl2009 and @imai2019, we use a DAG to separate identification from functional form. +We provide several methods for researchers to explore heterogeneous treatment effects (HTE). These methods help distinguish between *effect modification* --- how the treatment effect varies across subpopulations --- and *causal moderation* --- whether changing the moderator causally alters the treatment effect. This chapter demonstrates both descriptive HTE tools and the formal causal moderation framework. R script used in this chapter can be downloaded [here](https://raw.githubusercontent.com/xuyiqing/fect/dev/vignettes/rscript/05-hte.R). -### The NPSEM - -Under the DGP assumption, the Non-Parametric Structural Equation Model (NPSEM) for any time period $t$ is: - -$$ -\begin{aligned} -Y_{it} &= f_Y^t(D_{it}, M_{it}, X_{it}, U_{it}, \epsilon_{it}) \\ -D_{it} &= f_D^t(D_{i1}, \ldots, D_{it-1}, M_{i1}, \ldots, M_{it}, X_{i1}, \ldots, X_{it}, U_{it}, \epsilon_{it}^D) \\ -M_{it} &= f_M^t(M_{i1}, \ldots, M_{it-1}, X_{i1}, \ldots, X_{it}, U_{it}, \epsilon_{it}^M) \\ -X_{it} &= f_X^t(X_{i1}, \ldots, X_{it-1}, U_{it}, \epsilon_{it}^X) \\ -U_{it} &= \alpha_i + \lambda_t -\end{aligned} -$$ - -where $\epsilon_{it}, \epsilon_{it}^D, \epsilon_{it}^M, \epsilon_{it}^X$ are mutually independent disturbances. The composite $U_{it} = \alpha_i + \lambda_t$ captures unobserved confounding through additive unit and time effects. - -Two key restrictions enable identification via differencing: - -1. **No Feedback**: Past outcomes do not directly influence current treatments, moderators, or covariates (strict exogeneity). -2. **Additive Separability**: Unobserved confounders enter additively as $\alpha_i + \lambda_t$. - -### Conditional Parallel Trends - -For **effect modification**, we require: -$$ -\mathbb{E}\!\big[Y_t(0, M_t) - Y_s(0, M_s) \mid \vec{M}, \vec{X}, \vec{D}\big] = \mathbb{E}\!\big[Y_t(0, M_t) - Y_s(0, M_s) \mid \vec{M}, \vec{X}, \vec{D} = \vec{0}\big] -$$ -That is, conditional on the full history of moderators and covariates, the trend in untreated potential outcomes is independent of the treatment history. - -For **causal moderation**, a stricter version is needed that extends parallel trends to both treated and untreated potential outcomes across moderator values. - -### The No-Mediator Restriction - -A critical restriction is that $D$ must not causally affect $M$. If $D \to M$ existed, the moderator would become a mediator, requiring formal mediation analysis. Our estimands are not defined for such scenarios. - ------------------------------------------------------------------------- - -## Estimation Strategy - -### Effect Modification - -The estimation proceeds in four steps: +## Basic HTE Visualization -1. **Fit the control model**: On untreated observations $\mathcal{C} = \{(i,t) : D_{it} = 0\}$, estimate $Y_{it} = f_C(M_{it}, X_{it}) + \hat{\alpha}_i^C + \hat{\lambda}_t^C$. -2. **Predict counterfactuals**: For each $(i,t) \in \mathcal{T}$, compute $\hat{Y}_{it}(0) = \hat{f}_C(M_{it}, X_{it}) + \hat{\alpha}_i^C + \hat{\lambda}_t^C$. -3. **Estimate individual effects**: $\hat{\delta}_{it} = Y_{it} - \hat{Y}_{it}(0)$. -4. **Project onto the moderator**: Estimate $\hat{\tau}_{\text{em}}^{\text{CME}}(m)$ via kernel regression of $\hat{\delta}_{it}$ on $M_{it}$. +We start with descriptive tools using `sim_base`. These work with any estimation method; here we demonstrate with the FE estimator. -This is what **fect** does by default with `type = "hte"`. +```{r hte_setup, eval=TRUE, cache=TRUE, message=FALSE, results='hide'} +out.fect <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id","time"), + method = "fe", force = "two-way", se = TRUE, + parallel = TRUE, cores = 16, nboots = 1000) +``` -### Causal Moderation +### Box plot -Causal moderation requires fitting *two* imputation models: +One way to understand HTE is to use a series of box plots to visualize the estimated individualistic treatment effects of observations under the treatment condition (by setting `type = "box"`). Although these effects are not identified at the individual observation level, their level of dispersion is informative of treatment effects heterogeneity at different (relative) time periods, as well as model performance. -1. **Control model** ($\hat{g}_0$): Fit on $\mathcal{C}$ to obtain $\hat{f}_C, \hat{\alpha}_i^C, \hat{\lambda}_t^C$. -2. **Treated model** ($\hat{g}_1$): Fit on $\mathcal{T}$ to obtain $\hat{f}_T, \hat{\alpha}_i^T, \hat{\lambda}_t^T$. +```{r hte, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} +plot(out.fect, type = "box", xlim = c(-15, 10)) +``` -Then for any hypothetical moderator value $m$: -$$ -\hat{\tau}_{\text{cm}}^{\text{CME}}(m) = \frac{1}{|\mathcal{T}|} \sum_{(i,t) \in \mathcal{T}} \hat{\omega}_{it}^{\text{adj}} \big[\hat{g}_1(m, X_{it}) - \hat{g}_0(m, X_{it})\big] -$$ +### By calendar time -This is what **fect** computes when `cm = TRUE`. +Another way to explore HTE is to investigate how the treatment effect evolves over time. In the plot below, the point estimates represent the ATTs by calendar time; the blue curve and band represent a lowess fit of the estimates and its 95% confidence interval, respectively; and the red horizontal dashed line represents the ATT (averaged over all time periods). ------------------------------------------------------------------------- +```{r hte_time, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} +plot(out.fect, type = "calendar", xlim = c(1, 35)) +``` -## Basic HTE Visualization +### By a covariate -We start with descriptive tools using `simdata`, which features treatment switching on and off over 35 periods for 200 units. +By setting `type = "hte"` or `type = "heterogeneous"`, we can also plot the HTE by arbitrary covariates that are unaffected by the treatment. As before, the blue curve and band represent a lowess fit of the estimates and its 95% confidence interval, respectively. The red dashed line represents the ATT. The histogram at the bottom of the figure illustrates the distribution of the covariates, and can be turned off using `show.count = FALSE`. In our simulated case, the effect size is unrelated to the values of covariate `X1`. -```{r setup_base, eval = TRUE, cache = TRUE, message = FALSE, results = 'hide'} -out.fe <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), - method = "fe", force = "two-way", se = TRUE, - parallel = TRUE, cores = 4, nboots = 200) +```{r hte_X1, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} +plot(out.fect, type = "hte", covariate = "X1") ``` -### Box Plots - -Box plots visualize the distribution of individual treatment effects $\hat{\delta}_{it}$ across relative time periods. While individual effects are not identified, their dispersion is informative about treatment effect heterogeneity. +We can also plot the CATT when a covariate is discrete. To demonstrate this, we artificially create a moderating variable `X3`, which must be included in the outcome model and then specified in the heterogeneous treatment effect plot. -```{r hte_box, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} -plot(out.fe, type = "box", xlim = c(-15, 10)) +```{r hte_discrete, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} +sim_base$X3 <- sample(1:3, size = nrow(sim_base), replace = TRUE) +out.fect.X3 <- fect(Y ~ D + X1 + X2 + X3, data = sim_base, index = c("id","time"), + method = "fe", se = TRUE, seed = 123, + nboots = 1000, parallel = TRUE, cores = 16) ``` -### Calendar Plots +As expected, there is not much effect heterogeneity along `X3`. In the resulting figure, we can also assign labels to the discrete values in the moderator. -Calendar plots show the ATT by calendar time. The blue curve is a loess fit with 95% confidence intervals; the red dashed line is the overall ATT. - -```{r hte_calendar, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} -plot(out.fe, type = "calendar", xlim = c(1, 35)) +```{r plot-hte-discrete, fig.width = 6, fig.height = 4.5} +plot(out.fect.X3, type="hte", covariate = "X3", + xlab = "", ylab = "Effect of D on Y", + covariate.labels = c("USA", "China", "UK"), + ylim = c(-2, 6)) ``` -### Effect Modification Plot +--- -Setting `type = "hte"` plots $\hat{\delta}_{it}$ against a covariate with a loess fit, visualizing $\hat{\tau}_{\text{em}}^{\text{CME}}(m)$. +## Causal Moderation -```{r hte_em, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} -plot(out.fe, type = "hte", covariate = "X1", - xlab = "Moderator (X1)", ylab = "Effect on Y") -``` +A prevalent approach for examining treatment effect heterogeneity in political science is the two-way fixed effects (TWFE) model with a multiplicative interaction term. Despite its widespread use, this model conflates two conceptually distinct quantities: -In the simulated data, the treatment effect is unrelated to $X_1$, so we expect a flat curve around the ATT. +1. **Effect modification**: How does the *average treatment effect* vary across subpopulations defined by the moderator? This is a *correlational* relationship. +2. **Causal moderation**: Does exogenously changing the moderator *causally alter* the treatment effect? This is a *mechanistic* relationship. ------------------------------------------------------------------------- +Under the ignorability of $M$ assumption --- i.e., there are no unobserved links between the moderator and confounders --- the two estimands are numerically identical. In practice, divergence between the two estimates provides a useful diagnostic. -## Causal Moderation with `cm = TRUE` +### Estimation with `cm = TRUE` -### Estimation +The `cm` (causal moderation) option in `fect()` fits two separate imputation models: one for untreated potential outcomes $\hat{g}_0$ and one for treated potential outcomes $\hat{g}_1$. The causal moderation CME is then $\hat{\theta}(m) = \hat{g}_1(m) - \hat{g}_0(m)$. -To estimate the causal moderation effect, set `cm = TRUE` in `fect()`: +Currently `cm` is available for the `"fe"` and `"ife"` methods. ```{r cm_fe, eval = TRUE, cache = TRUE, message = FALSE, results = 'hide'} -out.cm <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), +out.cm <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id", "time"), method = "fe", force = "two-way", se = TRUE, - cm = TRUE, parallel = TRUE, cores = 4, nboots = 200) + cm = TRUE, parallel = TRUE, cores = 16, nboots = 1000) ``` -When `cm = TRUE`, **fect** fits two separate imputation models: +### Effect modification vs. causal moderation -- A model for $Y(0)$ using control observations $\to$ `out.cm$est$fit` ($\hat{g}_0$) -- A model for $Y(1)$ using treated observations $\to$ `out.cm$est.cm$fit` ($\hat{g}_1$) +The default HTE plot shows the effect modification estimate (projecting $\hat{\delta}_{it}$ onto the moderator): -### Comparing Effect Modification vs. Causal Moderation - -The default HTE plot shows the effect modification estimate: - -```{r hte_standard, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} +```{r hte_em, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} plot(out.cm, type = "hte", covariate = "X1", - main = "Effect Modification", - xlab = "X1", ylab = "Effect on Y") + xlab = "Moderator (X1)", ylab = "Effect on Y") ``` -By adding `cm = TRUE` to the plot call, we get the causal moderation estimate $\hat{\theta}(m) = \hat{g}_1(m) - \hat{g}_0(m)$: +By adding `cm = TRUE` to the plot call, we get the causal moderation estimate: -```{r hte_cm, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} +```{r hte_cm, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} plot(out.cm, type = "hte", covariate = "X1", cm = TRUE, - main = "Causal Moderation", - xlab = "X1", ylab = "Effect on Y") + xlab = "Moderator (X1)", ylab = "Effect on Y", ylim = c(-0.5, 5)) ``` -When the ignorability assumption for $M$ holds (as in the simulated data), the two curves should be similar. A divergence between the two would indicate that the moderator functions primarily as a proxy (effect modifier) rather than a direct causal driver. +When the ignorability assumption holds (as in the simulated data), the two curves should be similar. -### Scatter Without Smoothing +### Scatter without smoothing -To inspect the raw relationship, set `loess.fit = FALSE`: +To inspect the raw relationship without loess smoothing, set `loess.fit = FALSE`: -```{r hte_cm_scatter, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} +```{r hte_scatter, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} plot(out.cm, type = "hte", covariate = "X1", cm = TRUE, loess.fit = FALSE, - main = "Causal Moderation (No Smoothing)") + xlab = "Moderator (X1)", ylab = "Effect on Y", ylim = c(-0.5, 5)) ``` ------------------------------------------------------------------------- - -## Diagnostic Tools +--- -Valid inference relies on the **conditional parallel trends assumption** (CPTA). We provide several diagnostic tools. +## Diagnostics -### Placebo Test (Pre-Treatment HTE) +### Placebo test (pre-treatment HTE) -The placebo test applies the HTE estimation to *pre-treatment* periods only. Under CPTA, there should be no relationship between the moderator and the "treatment effect" before treatment onset: +The placebo test applies the HTE estimation to *pre-treatment* periods only. Under the conditional parallel trends assumption (CPTA), there should be no relationship between the moderator and the "treatment effect" before treatment onset: -```{r hte_placebo, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} +```{r hte_placebo, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} plot(out.cm, type = "hte", covariate = "X1", pretreatment = TRUE, num.pretreatment = 3, - main = "Placebo Test (Pre-Treatment Periods)", xlab = "X1", ylab = "Placebo Effect") ``` -The `pretreatment = TRUE` option restricts the analysis to the last `num.pretreatment` event-time periods before treatment onset. If the curve is flat around zero, it supports CPTA. A significant non-zero pattern signals that the moderator co-varies with pre-existing trends. +If the curve is flat around zero, it supports CPTA. A significant non-zero pattern signals that the moderator co-varies with pre-existing trends. The same test can be applied to the causal moderation estimate: -The placebo test can also be applied to the causal moderation estimate: - -```{r hte_placebo_cm, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} +```{r hte_placebo_cm, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} plot(out.cm, type = "hte", covariate = "X1", cm = TRUE, pretreatment = TRUE, num.pretreatment = 3, - main = "Placebo Test: Causal Moderation", - xlab = "X1", ylab = "Placebo Effect") -``` - -### Subsample Dynamic Treatment Effects - -According to CPTA, parallel trends should hold for any conditional value of the moderator. We can partition the sample by the moderator and estimate separate `fect` models for each subsample: - -```{r sub_dyn_setup, eval = TRUE, cache = TRUE, message = FALSE, results = 'hide'} -x1_med <- median(simdata$X1[simdata$D == 1], na.rm = TRUE) - -# Subsample: X1 below median -sim_low <- simdata[simdata$X1 < x1_med, ] -out.low <- fect(Y ~ D + X1 + X2, data = sim_low, index = c("id", "time"), - method = "fe", force = "two-way", se = FALSE) - -# Subsample: X1 above median -sim_high <- simdata[simdata$X1 >= x1_med, ] -out.high <- fect(Y ~ D + X1 + X2, data = sim_high, index = c("id", "time"), - method = "fe", force = "two-way", se = FALSE) -``` - -```{r sub_dyn_low, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} -plot(out.low, main = paste0("Dynamic Effects: X1 < ", round(x1_med, 2)), - ylab = "Effect on Y", xlim = c(-10, 10)) -``` - -```{r sub_dyn_high, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} -plot(out.high, main = paste0("Dynamic Effects: X1 >= ", round(x1_med, 2)), - ylab = "Effect on Y", xlim = c(-10, 10)) -``` - -Under CPTA, both subsamples should exhibit flat pre-trends. Opposite-direction violations across subsamples strongly indicate moderator-dependent confounding. - -### Subsample Calendar Plots - -We can also compare calendar-time treatment effects across subgroups using `type = "calendar"` with the `covariate.value` filter: - -```{r cal_sub_low, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} -plot(out.cm, type = "calendar", - covariate = "X1", covariate.value = c(-Inf, x1_med), - covariate.value.range = TRUE, - main = paste0("Calendar Effects: X1 < ", round(x1_med, 2))) -``` - -```{r cal_sub_high, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} -plot(out.cm, type = "calendar", - covariate = "X1", covariate.value = c(x1_med, Inf), - covariate.value.range = TRUE, - main = paste0("Calendar Effects: X1 >= ", round(x1_med, 2))) + xlab = "X1", ylab = "Placebo Effect", ylim = c(-0.5, 1.5)) ``` ------------------------------------------------------------------------- +--- ## Over-Identification Test @@ -306,14 +144,10 @@ $$ H_0:\text{Nonlinear terms have no explanatory power} \quad \Rightarrow \quad n \times R^2 \sim \chi^2(\text{df}) $$ -### Usage - ```{r iden_test, eval = TRUE, cache = TRUE, message = FALSE} iden.test <- fect_iden(out.cm, moderator = "X1") ``` -### Results - ```{r iden_results, eval = TRUE} cat("=== Treated cells (e1) ===\n") cat(" n =", iden.test$e1$n, "\n") @@ -333,9 +167,9 @@ cat(" p-value =", round(iden.test$e0$p, 4), "\n") - **Large p-values** ($> 0.05$): No evidence against the linear specification. - **Small p-values** ($< 0.05$): Nonlinear effects present. Consider more flexible specifications. -### Controlling Test Components +You can also test quadratic and interaction terms separately: -```{r iden_quad, eval = TRUE, cache = TRUE, message = FALSE} +```{r iden_components, eval = TRUE, cache = TRUE, message = FALSE} # Quadratic terms only (no interactions) iden.quad <- fect_iden(out.cm, moderator = "X1", interaction = FALSE) cat("Quadratic-only: p =", @@ -349,104 +183,33 @@ cat("Interaction-only: p =", round(iden.inter$e0$p, 4), "(control)\n") ``` ------------------------------------------------------------------------- - -## Interactive Fixed Effects - -When the DGP involves latent factors, the FE method may be biased. The IFE method (`method = "ife"`) accommodates unobserved interactive effects: - -```{r cm_ife, eval = TRUE, cache = TRUE, message = FALSE, results = 'hide'} -out.cm.ife <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), - method = "ife", force = "two-way", r = 2, - cm = TRUE, se = FALSE, parallel = TRUE, cores = 4) -``` - -### Comparing FE and IFE +--- -```{r cm_ife_plot, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} -plot(out.cm.ife, type = "hte", covariate = "X1", cm = TRUE, - main = "Causal Moderation (IFE)") -``` - -Since `simdata` is generated with interactive fixed effects, the IFE method may provide better estimates: - -```{r cm_fe_compare, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} -plot(out.cm, type = "hte", covariate = "X1", cm = TRUE, - main = "Causal Moderation (FE)") -``` - -The over-identification test helps assess model adequacy: - -```{r iden_ife, eval = TRUE, cache = TRUE, message = FALSE} -iden.ife <- fect_iden(out.cm.ife, moderator = "X1") -cat("IFE over-identification test:\n") -cat(" Treated p-value:", round(iden.ife$e1$p, 4), "\n") -cat(" Control p-value:", round(iden.ife$e0$p, 4), "\n") -``` +## Discrete Moderator with Causal Moderation ------------------------------------------------------------------------- - -## Discrete Moderators - -For discrete moderators, the HTE plot automatically produces group-wise estimates: - -```{r discrete_setup, eval = TRUE, cache = TRUE, message = FALSE, results = 'hide'} -simdata$Region <- sample(c("North", "South"), nrow(simdata), replace = TRUE) -simdata$Region_num <- ifelse(simdata$Region == "North", 0, 1) -out.discrete <- fect(Y ~ D + X1 + X2 + Region_num, data = simdata, +```{r discrete_cm_setup, eval = TRUE, cache = TRUE, message = FALSE, results = 'hide'} +sim_base$X3 <- sample(1:3, size = nrow(sim_base), replace = TRUE) +out.discrete <- fect(Y ~ D + X1 + X2 + X3, data = sim_base, index = c("id", "time"), method = "fe", force = "two-way", se = TRUE, - cm = TRUE, parallel = TRUE, cores = 4, nboots = 200) -``` - -### Effect Modification with Discrete Moderator - -```{r discrete_em, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} -plot(out.discrete, type = "hte", covariate = "Region_num", - covariate.labels = c("North", "South"), - xlab = "", ylab = "Effect on Y", - main = "Effect Modification by Region") -``` - -### Causal Moderation with Discrete Moderator - -```{r discrete_cm, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} -plot(out.discrete, type = "hte", covariate = "Region_num", cm = TRUE, - covariate.labels = c("North", "South"), - xlab = "", ylab = "Effect on Y", - main = "Causal Moderation by Region") + cm = TRUE, parallel = TRUE, cores = 16, nboots = 1000) ``` -Since `Region` is randomly assigned and unrelated to the DGP, both estimates should show no significant heterogeneity. - ------------------------------------------------------------------------- - -## Empirical Application: Direct Democracy in Switzerland - -We replicate the analysis from @HH2019, who examine how the transition from direct democratic voting to representative committees affects naturalization rates in Swiss municipalities. The `hh2019` dataset contains 1,209 municipalities observed from 1991 to 2009. - -```{r hh_data, eval = TRUE, message = FALSE} -data(hh2019) -head(hh2019, 3) +```{r discrete_em_plot, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} +plot(out.discrete, type = "hte", covariate = "X3", + covariate.labels = c("USA", "China", "UK"), + xlab = "", ylab = "Effect on Y", ylim = c(-0.5, 5)) ``` -### Baseline Estimation - -```{r hh_est, eval = TRUE, cache = TRUE, message = FALSE, results = 'hide'} -out.hh <- fect(nat_rate_ord ~ indirect, data = hh2019, - index = c("bfs", "year"), - method = "fe", force = "two-way", se = TRUE, - parallel = TRUE, cores = 4, nboots = 200) +```{r discrete_cm_plot, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5} +plot(out.discrete, type = "hte", covariate = "X3", cm = TRUE, + covariate.labels = c("USA", "China", "UK"), + xlab = "", ylab = "Effect on Y", ylim = c(-0.5, 5)) ``` -```{r hh_gap, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 7, fig.height = 5} -plot(out.hh, main = "Effect of Indirect Democracy on Naturalization", - ylab = "Effect on Naturalization Rate", xlim = c(-6, 6)) -``` - -The results are consistent with the original findings: switching to representative committees increases naturalization rates. +Since `X3` is randomly assigned and unrelated to the DGP, both estimates should show no significant heterogeneity. ------------------------------------------------------------------------- +--- ## Summary of Parameters @@ -458,21 +221,6 @@ The results are consistent with the original findings: switching to representati | `covariate` | Specify the moderator variable | | `pretreatment = TRUE` | Restrict to pre-treatment periods (placebo test) | | `num.pretreatment` | Number of pre-treatment periods to include | -| `covariate.value` | Filter observations by moderator values | -| `covariate.value.range = TRUE` | Treat `covariate.value` as range $[\text{lo}, \text{hi}]$ | | `loess.fit = FALSE` | Scatter only, without loess smoothing | | `covariate.labels` | Labels for discrete moderator categories | | `fect_iden()` | Over-identification test for linearity | - ------------------------------------------------------------------------- - -## Summary - -This chapter introduced the causal moderation framework for heterogeneous treatment effects: - -- **Effect modification** describes how the treatment effect varies across observed levels of $M$; it is obtained by projecting individual treatment effects $\hat{\delta}_{it}$ onto the moderator. -- **Causal moderation** answers whether exogenously changing $M$ would alter the treatment effect; it requires separate models for treated and control outcomes (`cm = TRUE`). -- **Diagnostics**: The placebo test (`pretreatment = TRUE`) and subsample event-study plots (`covariate.value`) probe CPTA. The over-identification test (`fect_iden()`) checks linearity. -- **Comparison**: When the EM and CM estimates diverge, $M$ is primarily a proxy; when they converge, $M$ is likely a genuine causal moderator. - -For DID-based heterogeneous treatment effect analysis, see @sec-panel. diff --git a/vignettes/06-plots.Rmd b/vignettes/06-plots.Rmd new file mode 100644 index 00000000..c90c607f --- /dev/null +++ b/vignettes/06-plots.Rmd @@ -0,0 +1,616 @@ +# Plot Options {#sec-plots} + +```{r .common, include = FALSE} +source("_common.R") +``` + +In this chapter, we explore visualization options available in the **fect** package. Plots are organized by `type`: + +- the event study plot (`gap`) +- treated counterfactual plot (`counterfactual`) +- diagnostic plots (`equiv`, `placebo`, `carryover`) +- cumulative effects (`cumul`) +- treatment effect heterogeneity plots (`box`, `calendar`, and `hte`) +- special-purpose displays (`status`, `factors`, and `loadings`) +- standalone `esplot()` + +We begin with shared parameter conventions and then work through each plot type in turn. `plot.fect` is an S3 method that accepts a fitted `fect` object and a `type` argument. All customization parameters---axis limits, colors, text sizes, reference lines---are passed as additional arguments. Some parameters apply universally; others are type-specific. A parameter applicability table appears at the end of this chapter. R script used in this chapter can be downloaded [here](https://raw.githubusercontent.com/xuyiqing/fect/dev/vignettes/rscript/06-plots.R). + +------------------------------------------------------------------------ + +## Load Data + +We use two datasets throughout. @GS2020 examines the mobilizing effect of minority candidates on coethnic support in U.S. congressional elections. The treatment indicates the presence of an Asian candidate; the outcome is the proportion of general election contributions from Asian donors. @HH2019 study the effects of indirect democracy (treatment) on naturalization rates (outcome) in Swiss municipalities from 1991 to 2009. + +```{r load, message=FALSE} +# load libraries and data +library(ggplot2) +library(panelView) +data(gs2020) +data(hh2019) +ls() +``` + +------------------------------------------------------------------------ + +## Gap Plot {#sec-gap} + +The gap plot---also known as the event study plot---displays dynamic treatment effects over relative time. It is the default plot type. + +### Default gap plot + +We first estimate the model. For details on estimation, see @sec-fect. + +```{r est, cache = TRUE} +out <- fect(Y = "general_sharetotal_A_all", + D = "cand_A_all", + X = c("cand_H_all", "cand_B_all"), + index = c("district_final", "cycle"), + data = gs2020, method = "fe", + force = "two-way", se = TRUE, + parallel = TRUE, cores = 16, nboots = 1000) + +out.hh <- fect(nat_rate_ord ~ indirect, + data = hh2019, + index = c("bfs","year"), + method = 'fe', se = TRUE, + parallel = TRUE, cores = 16, nboots = 1000, + keep.sims = TRUE) +``` + +After running the model, we plot the dynamic treatment effects, including confidence intervals when `se = TRUE` is specified in estimation. Since `type = "gap"` is the default, we omit it. + +```{r plot-gap-default} +plot(out) # the effect of co-ethnic mobilization +plot(out.hh) # the effect of indirect democracy on naturalization rate +``` + +### Starting period + +By default, the first post-treatment period is labeled 1 and the last pre-treatment period is 0. Some researchers prefer to label these as 0 and -1, respectively. Set `start0 = TRUE` to shift accordingly. + +```{r begin-post-customization} +plot(out, start0 = TRUE, + main = "Custom Starting Period") +``` + +### Connected estimates + +By default, estimates are plotted as discrete points. Set `connected = TRUE` to connect them with lines. The line width and point size are controlled by `est.lwidth` and `est.pointsize`. + +```{r connected-estimates} +plot(out, + post.color = "green4", + connected = TRUE, + est.lwidth = 1.2, + est.pointsize = 3) +``` + +To outline the confidence interval band, add `ci.outline = TRUE`. This improves visibility when colors are similar to the background. + +```{r ci-outline} +plot(out, + connected = TRUE, + ci.outline = TRUE, + main = "The Effect of Coethnic Mobilization") +plot(out.hh, + preset = "vibrant", + ci.outline = TRUE, + main = "The Effect of Indirect Democracy") +``` + +### Presets + +The `preset` argument applies coordinated color schemes. Options are `"default"` (mostly black and white with accent color), `"vibrant"` (saturated colors), and `"grayscale"` (monochromatic, suitable for journals that charge for color). + +```{r preset-vibrant} +plot(out, + preset = "vibrant", + main = "Vibrant Preset Colors: Grumbach and Sahn (2020)") +plot(out.hh, + preset = "vibrant", + main = "Vibrant Preset Colors: Hainmueller and Hangartner (2019)") +``` + +```{r preset-grayscale} +plot(out, + preset = "grayscale", + main = "Grayscale Preset Colors") +``` + +### Colors + +The `color` parameter sets the master color for estimate lines, points, and CI bands. In gap plots, `color` controls the **post-treatment** color; pre-treatment defaults to gray. To control each phase independently, use `pre.color` and `post.color`. + +Default color conventions for gap plots: pre-treatment = gray (in-sample), post-treatment = black (out-of-sample). When `loo = TRUE`, all points are black (all out-of-sample). + +```{r preset-vibrant2} +plot(out.hh, + preset = "vibrant", + post.color = "green4", + main = "Change Estimates' Color: Hainmueller and Hangartner (2019)") +``` + +### Confidence intervals + +The `plot.ci` argument controls CI display. Options are `"0.95"` (default), `"0.9"`, and `"none"`. + +```{r ci-raw-customization} +plot(out, plot.ci = "0.9", + main = "90% confidence intervals") +``` + +### Count bars + +The bar chart at the bottom of the plot shows the number of treated units at each relative time period. Customize it with `count.color`, `count.outline.color`, and `count.alpha`. Set `show.count = FALSE` to hide the bars entirely. + +```{r count-histogram-customization} +plot(out, + count.color = "lightblue", + count.outline.color = "darkblue", + count.alpha = 0.2, + main = "Count Histogram Customization") +``` + +The `proportion` parameter controls which periods are displayed based on the count of treated units. Periods where the treated unit count falls below `proportion` $\times$ max(count) are trimmed. The default `proportion = 0.3` retains periods with at least 30% of the maximum treated unit count. When `xlim` is specified explicitly, `proportion` is overridden. + +### Axis customization + +Use `xlim`/`ylim` for axis ranges, `xbreaks`/`ybreaks` for tick marks, `xlab`/`ylab` for labels, and `xangle` for rotating x-axis text. Set `gridOff = TRUE` to remove grid lines. + +```{r axis-legend-customization} +plot(out, + xlim = c(-10, 1), + ylim = c(-0.15, 0.30), + xlab = "Custom Time Axis", + ylab = "Estimated ATT", + xangle = 90, + xbreaks = seq(-10, 1, by = 2), + gridOff = TRUE, + main = "Axis and Legend Customization") +``` + +### Text sizes + +The `cex.*` family of parameters controls text sizes: `cex.main` (title), `cex.axis` (tick labels), `cex.lab` (axis labels), `cex.text` (annotations), and `cex.legend` (legend text). The `theme.bw` option toggles the black-and-white ggplot2 theme. + +```{r text-customization} +plot(out, + ylim = c(-0.15, 0.3), + theme.bw = FALSE, + cex.main = 1.25, + cex.axis = 1.2, + cex.lab = 1.2, + cex.legend = 1, + cex.text = 1.2, + main = "Text and Theme Customization") +``` + +### Reference lines + +The `lcolor`, `lwidth`, and `ltype` parameters control the horizontal (zero) and vertical (treatment onset) reference lines. Each accepts a vector of length two; if a single value is given, it applies to both lines. + +```{r line-bound-customization} +plot(out, + est.lwidth = 1.5, + est.pointsize = 3, + lcolor = c("red","skyblue"), + lwidth = 2, + main = "Line Customization") +``` + +------------------------------------------------------------------------ + +## Counterfactual plot + +While the gap plot shows the ATT (difference), the counterfactual plot shows the **levels**: observed outcomes for treated units alongside model-predicted counterfactual paths. + +```{r counterfactual} +plot(out, type = "counterfactual", + main = "Grumbach & Sahn (2020): Treated vs. Counterfactuals", + ylab = "Proportion of Asian Donation", + legend.pos = "bottom") +``` + +```{r counterfactual-hh} +plot(out.hh, type = "counterfactual", + main = "Hainmueller & Hangartner (2019): Treated vs. Counterfactuals", + ylab = "Naturalization Rate", + legend.pos = "top") +``` + +Use `color` for the observed-outcome line and `counterfactual.color` for the counterfactual line (which also colors the CI band with more transparency). Add `ci.outline = TRUE` to outline the band. + +```{r counterfactual-colors} +plot(out.hh, type = "counterfactual", + main = "Hainmueller & Hangartner (2019): Treated vs. Counterfactuals", + ylab = "Naturalization Rate", + legend.pos = "bottom", + ci.outline = TRUE, + color = "red3", + counterfactual.color = "green4") +``` + +Setting `raw = "all"` overlays individual unit paths. + +```{r counterfactual-rawall} +plot(out, type = "counterfactual", raw = "all") +``` + +Setting `raw = "band"` displays the 5th--95th interpercentile range. When adoption is staggered, only the band around treated units is shown. + +```{r counterfactual-rawband} +plot(out, type = "counterfactual", raw = "band") +``` + +The individual-path colors are also customizable: + +```{r counterfactual-colors2} +plot(out, type = "counterfactual", + count.color = "black", + count.alpha = 1, + color = "red", + counterfactual.color = "purple", + counterfactual.raw.treated.color = "orange", + counterfactual.linetype = "dotted", + raw = "all", + main = "Counterfactual Plot with Custom Colors") +``` + +------------------------------------------------------------------------ + +## Pretrend Tests {#sec-pretrend} + +We provide two tests that shed light on the parallel trends assumption: the placebo test and the equivalence test. For methodological details, see @sec-fect or @LWX2022. + +### Placebo test---shape markers + +A placebo test artificially assigns treatment during pre-treatment periods and estimates the "placebo effect" in those periods. The model must be re-estimated with `placeboTest = TRUE`. + +```{r placebo, cache = TRUE} +out_fe_placebo <- fect(Y = "general_sharetotal_A_all", D = "cand_A_all", X = c("cand_H_all", "cand_B_all"), data = gs2020, + index = c("district_final", "cycle"), force = "two-way", + method = "fe", CV = FALSE, parallel = TRUE, cores = 16, + se = TRUE, nboots = 1000, placeboTest = TRUE, + placebo.period = c(-2, 0)) + +plot(out_fe_placebo) +``` + +When `placeboTest = TRUE` and a placebo period is active, the placebo test periods are automatically marked with **triangles** (▲, shape 17) in the plot, while regular estimates use **circles** (●). This distinction is especially important in grayscale, where color alone cannot differentiate test periods from ordinary estimates. A legend at the bottom of the plot identifies each symbol. To suppress the legend, set `legendOff = TRUE`. + +In connected mode, the triangle markers are scaled up so they remain visible alongside the line: + +```{r plot-placebo-connected} +plot(out_fe_placebo, connected = TRUE, preset = "grayscale", + main = "Placebo Test with Connected Estimates") +``` + +The color of the placebo period markers can be changed with the `placebo.color` argument. + +```{r plot-placebo-color} +plot(out_fe_placebo, placebo.color = "green4") +``` + +### Joint pre-trend test + +The equivalence plot displays only the **pre-treatment** period. The equivalence bound is defined by the two-one-sided test (TOST) threshold. Note: post-treatment estimates are not shown in this plot type---use the gap plot for those. + +The `bound` option controls which reference lines appear: `"none"`, `"min"` (maximum absolute pre-treatment residual), `"equiv"` (TOST threshold), or `"both"` (default). + +```{r plot-equiv-bound} +plot(out, type = "equiv", bound = "equiv", tost.threshold = 0.1, + ylim = c(-0.15, 0.15)) +``` + +The `"min"` bound displays the minimum range based on the maximum absolute pre-treatment residual. For example, if the largest pre-treatment estimate is 0.03, lines appear at \$\pm\$0.03. + +```{r plot-equiv-min} +plot(out, type = "equiv", bound = "min", ylim = c(-0.15, 0.15)) +``` + +With `bound = "both"` (the default), both the minimum range and the equivalence bound are shown. + +```{r plot-equiv-both} +plot(out, type = "equiv", tost.threshold = 0.1, ylim = c(-0.15, 0.15)) +``` + +Use `stats` to select test results to display, `stats.labs` to label them, and `stats.pos` to position the annotation. Set `show.stats = FALSE` to hide test results entirely. + +```{r stats-customization} +plot(out, type = "equiv", + ylim = c(-0.25, 0.25), + stats = c("F.p", "equiv.p"), + stats.labs = c("F Test P-value", "Equivalence P-value"), + stats.pos = c(-8, 0.2), + show.stats = TRUE, + main = "Statistical Test Annotations") +``` + +------------------------------------------------------------------------ + +## Carryover Test {#sec-exit} + +The exit plot shows how the difference between treatment and control groups evolves **after treatment ends**. The x-axis represents time relative to treatment exit, in contrast to the gap plot's focus on treatment entry. Exit plots are essential for assessing potential carryover effects. + +In exit plots, the color convention is reversed from gap plots: pre-exit estimates are black (out-of-sample) and post-exit estimates are gray (in-sample). + +```{r plot-exit-default} +plot(out_fe_placebo, type = "exit") +``` + +### Carryover test with shape markers + +The carryover test examines whether the treatment effect persists after treatment ends. By setting `carryoverTest = TRUE` and specifying `carryover.period`, we designate post-exit periods as a "placebo" window. + +```{r carryover, cache = TRUE} +out_fe_carryover <- fect(Y = "general_sharetotal_A_all", D = "cand_A_all", X = c("cand_H_all", "cand_B_all"), data = gs2020, + index = c("district_final", "cycle"), force = "two-way", + parallel = TRUE, cores = 16, se = TRUE, CV = FALSE, + nboots = 1000, carryoverTest = TRUE, + carryover.period = c(1, 3)) +plot(out_fe_carryover) +``` + +Carryover test periods are marked with **diamonds** (shape 18), while placebo test periods use triangles (shape 17). Both shapes are distinguishable in grayscale printing. When both tests are active in the same model, the legend differentiates all three symbol types (circles for regular estimates, triangles for placebo periods, diamonds for carryover periods). + +------------------------------------------------------------------------ + +## Cumulative Effects {#sec-cumul} + +The cumulative effect plot displays the running sum of per-period treatment effects. This is only well-defined when there are no treatment reversals---that is, all treated units remain treated for the duration of the study. The model must be estimated with `keep.sims = TRUE`. + +We first apply it to `hh2019`, which has no treatment reversals. + +```{r plot-cumulative-hh} +plot(effect(out.hh), main = "Cumulative Effect of Indirect Democracy", + ylab = "Cumulative Effect on Naturalization Rate") +``` + +Since the `gs2020` dataset has treatment reversals, we subset to units that remained treated throughout. + +```{r subset-no-reversals} +# flag units that ever have a 1 to 0 change in d +rev_flag <- tapply(gs2020[["cand_A_all"]], + gs2020[["district_final"]], + function(x) any(diff(x) < 0)) + +# units with no reversals +good_units <- names(rev_flag)[!rev_flag] + +# subset the desired rows +gs2020_no_reversals <- gs2020[gs2020[["district_final"]] %in% good_units, ] + +``` + +```{r no-reversals-est, cache = TRUE} +out_no_reversals <- fect(Y = "general_sharetotal_A_all", + D = "cand_A_all" , + X = c("cand_H_all", "cand_B_all") , + index = c("district_final", "cycle"), + data = gs2020_no_reversals, + method = "fe", + force = "two-way", + se = TRUE, parallel = TRUE, cores = 16, + nboots = 1000, + keep.sims = TRUE) +``` + +```{r cumulative-effects} +plot(effect(out_no_reversals), xlim = c(1, 2)) +``` + +------------------------------------------------------------------------ + +## Effect Heterogeneity {#sec-plots-hte} + +### Box plot (`type = "box"`) + +The box plot displays the distribution of individual treatment effects in each period. The box spans the interquartile range (middle 50%), whiskers extend to the 2.5th--97.5th percentiles, and the horizontal line marks the median. + +```{r plot-box-hte} +plot(out, type = "box", xlim = c(-12, 3)) +``` + +The `proportion` parameter controls the x-axis range for box plots. Periods where the number of treated units falls below `proportion` $\times$ max(count) are trimmed from the display. The default `proportion = 0.3` keeps periods with at least 30% of the maximum treated unit count. When there are many time periods, x-axis labels are automatically thinned and rotated for readability. + +### Calendar plot (`type = "calendar"`) + +The calendar plot depicts the ATT conditional on **calendar time** (rather than relative time). The ribbon represents a loess fit with 95% confidence intervals. + +```{r plot-calendar-hte} +plot(out, type = "calendar", main = "The Effect of Coethnic Mobilization") +plot(out.hh, type = "calendar", xlim = c(1995, 2009), + main = "The Effect of Indirect Democracy") +``` + +### HTE by covariate (`type = "hte"`) + +The `"hte"` plot (or equivalently `"heterogeneous"`) displays the conditional average treatment effect (CATT) as a function of a pre-treatment covariate. The `covariate` argument specifies which variable to use. The blue curve and band show a loess fit with 95% confidence intervals; the red dashed line marks the overall ATT; and a histogram at the bottom shows the covariate distribution. + +```{r plot-hte-covariate} +plot(out, type = "hte", covariate = "cand_B_all", + main = "HTE by Black Candidate Presence", + xlab = "Black Candidate Indicator", + ylab = "Effect on Asian Donation Share") +``` + +When the covariate is **discrete**, the plot automatically switches to a grouped display. Use `covariate.labels` to assign readable labels to the discrete values: + +```{r plot-hte-discrete-ch6} +plot(out, type = "hte", covariate = "cand_H_all", + covariate.labels = c("No Hispanic Candidate", "Hispanic Candidate"), + main = "HTE by Hispanic Candidate Presence", + ylab = "Effect on Asian Donation Share") +``` + +Key parameters for HTE plots: `covariate` (required), `covariate.labels` (for discrete covariates), `show.count` (toggle covariate histogram, default TRUE). Axis, text size, and color parameters apply as usual. For more on HTE analysis, see [Chapter @sec-hte]. + +------------------------------------------------------------------------ + +## Other Plot Types {#sec-other} + +### Status plot + +The status plot displays the treatment status by period for all units, similar to `panelView`. Each indicator color is customizable. + +```{r status} +plot(out_fe_carryover, type = "status", + status.treat.color = "#D55E00", + status.control.color = "#0072B2", + status.carryover.color = "#CC79A7", + status.missing.color = "#009E73", + status.background.color = "#F3EAD2", + main = "Status Plot") +``` + +Note: most styling parameters (`connected`, `plot.ci`, `pre.color`/`post.color`, `count.*`) do not apply to status plots. Only axis, text size, and status-specific color parameters are relevant. + +### Factors and loadings plots + +These plot types are available when the model is estimated with interactive fixed effects (`method = "ife"` or `method = "gsynth"`). We first fit an IFE model with two factors: + +```{r est-ife, cache = TRUE} +out_ife <- fect(nat_rate_ord ~ indirect, + data = hh2019, + index = c("bfs", "year"), + method = "ife", r = 2, + se = TRUE, parallel = TRUE, cores = 16, nboots = 1000) +``` + +The **factors** plot displays the estimated latent time factors. It uses the Okabe-Ito colorblind-safe palette with thinner lines for a clean, publication-ready appearance. Factor 0 (fixed effects, shown when `include.FE = TRUE`) appears in gray; subsequent factors appear in orange, blue, green, and so on. Use `nfactors` to limit the number of displayed factors. + +```{r plot-factors} +plot(out_ife, type = "factors", main = "Estimated Latent Factors") +``` + +To exclude fixed effects from the plot, set `include.FE = FALSE`: + +```{r plot-factors-nofe} +plot(out_ife, type = "factors", include.FE = FALSE, + main = "Factors without Fixed Effects") +``` + +The **loadings** plot displays the estimated factor loadings, comparing the distribution across treated and control units. It uses a navy/crimson color scheme with smaller, semi-transparent scatter points for cleaner visualization. When multiple factors are estimated, the plot is rendered as a pairs matrix via `GGally::ggpairs`. + +```{r plot-loadings} +plot(out_ife, type = "loadings", main = "Factor Loadings") +``` + +Note: `connected`, `plot.ci`, `show.count`, and most gap-plot styling parameters do not apply to factors or loadings plots. + +------------------------------------------------------------------------ + +## Standalone `esplot()` {#sec-esplot} + +The `esplot()` function creates event study plots directly from a data frame or a fitted `fect` object, without requiring the full `plot.fect` dispatch. This is useful when you have pre-computed ATT estimates (e.g., from another package) and want publication-quality event study figures with the same styling as **fect** plots. + +### Basic usage with data frames + +`esplot()` expects a data frame with columns for the time period, the point estimate, and optionally CI bounds and observation counts. By default, it looks for columns named `"ATT"`, `"CI.lower"`, and `"CI.upper"`. + +```{r esplot-basic, fig.width = 6, fig.height = 4.5} +# Create example data from a fect result +es_data <- data.frame( + Time = as.numeric(rownames(out$est.att)), + ATT = out$est.att[, "ATT"], + CI.lower = out$est.att[, "CI.lower"], + CI.upper = out$est.att[, "CI.upper"] +) + +esplot(es_data, Period = "Time", + main = "Event Study Plot with esplot()", + ylab = "Estimated ATT", + xlab = "Periods Since Treatment", + xlim = c(-15, 5), ylim = c(-0.3, 0.7)) +``` + +### Using fect objects directly + +When you pass a **fect object** directly to `esplot()`, it automatically extracts the ATT estimates and treated unit counts. This is the simplest usage: + +```{r esplot-fect-object, fig.width = 6, fig.height = 4.5} +esplot(out, main = "Direct from fect object") +``` + +### Connected line style + +Setting `connected = TRUE` displays estimates as a connected line with a confidence band: + +```{r esplot-connected, fig.width = 6, fig.height = 4.5} +esplot(es_data, Period = "Time", + connected = TRUE, + main = "Connected Event Study Plot", + ylab = "Estimated ATT", + xlim = c(-15, 5), ylim = c(-0.3, 0.7)) +``` + +### Highlighting periods + +The `highlight.periods` argument highlights specific time periods, which is useful for drawing attention to placebo or treatment windows: + +```{r esplot-highlight, fig.width = 6, fig.height = 4.5} +esplot(es_data, Period = "Time", + highlight.periods = c(-2, -1, 0), + highlight.colors = c("orange", "orange", "red"), + main = "Highlighting Key Periods", + ylab = "Estimated ATT", + xlim = c(-15, 5), ylim = c(-0.3, 0.7)) +``` + +### Count bar behavior + +When you pass a **fect object** to `esplot()`, count bars appear by default because the function auto-extracts treated unit counts from the object: + +``` r +esplot(out) # count bars shown automatically +``` + +When passing a **data frame**, you must include a count column and specify its name: + +``` r +esplot(df, Count = "n_treated") +``` + +If no count data is available, `show.count` silently degrades to `FALSE`---no error is thrown, but count bars are not displayed. Set `show.count = FALSE` to suppress count bars explicitly regardless of data availability. + +------------------------------------------------------------------------ + +## Shared Parameters Reference {#sec-param-ref} + +### Legend behavior + +Legends appear automatically when multiple visual encodings are present (e.g., shape markers from placebo or carryover tests). The dashed/solid pre-treatment vs. post-treatment line distinction does not generate a legend entry, as this convention is standard in causal inference plots. Use `legendOff = TRUE` to suppress all legends. Use `legend.pos` to control placement (default is `"bottom"` for shape legends). + +### Parameter applicability table + +The table below summarizes which parameters apply to each plot type. Parameters not listed are either universal (e.g., `main`, `theme.bw`) or internal. + +| Parameter | gap | equiv | exit | box | calendar | counterfactual | status | factors | loadings | esplot | +|:------|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:| +| `bound` / `tost.threshold` | --- | Yes | --- | --- | --- | --- | --- | --- | --- | --- | +| `carryover.color` | --- | --- | Yes | --- | --- | --- | --- | --- | --- | --- | +| `cex.main` / `cex.axis` / `cex.lab` | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | --- | Yes | +| `ci.outline` | Yes | Yes | Yes | --- | --- | Yes | --- | --- | --- | Yes | +| `color` | Yes | Yes | Yes | --- | --- | Yes | --- | --- | --- | Yes | +| `connected` | Yes | Yes | Yes | --- | --- | --- | --- | --- | --- | Yes | +| `count.color` / `count.alpha` | Yes | Yes | Yes | --- | --- | Yes | --- | --- | --- | Yes | +| `counterfactual.color` | --- | --- | --- | --- | --- | Yes | --- | --- | --- | --- | +| `est.lwidth` / `est.pointsize` | Yes | Yes | Yes | --- | --- | --- | --- | --- | --- | Yes | +| `gridOff` | Yes | Yes | Yes | Yes | Yes | Yes | --- | Yes | --- | Yes | +| `highlight.periods` / `highlight.shapes` | --- | --- | --- | --- | --- | --- | --- | --- | --- | Yes | +| `lcolor` / `lwidth` / `ltype` | Yes | Yes | Yes | Yes | Yes | Yes | --- | Yes | --- | Yes | +| `legendOff` | Yes | Yes | Yes | --- | --- | Yes | --- | --- | --- | Yes | +| `nfactors` / `include.FE` | --- | --- | --- | --- | --- | --- | --- | Yes | Yes | --- | +| `placebo.color` | Yes | --- | --- | --- | --- | --- | --- | --- | --- | --- | +| `plot.ci` | Yes | Yes | Yes | --- | --- | Yes | --- | --- | --- | --- | +| `pre.color` / `post.color` | Yes | Yes | Yes | --- | --- | --- | --- | --- | --- | Yes | +| `preset` | Yes | Yes | Yes | --- | --- | --- | --- | --- | --- | --- | +| `proportion` | Yes | Yes | Yes | Yes | --- | --- | --- | --- | --- | Yes | +| `raw` | --- | --- | --- | --- | --- | Yes | --- | --- | --- | --- | +| `show.count` | Yes | Yes | Yes | --- | --- | Yes | --- | --- | --- | Yes | +| `start0` | Yes | Yes | Yes | --- | --- | --- | --- | --- | --- | Yes | +| `stats` / `stats.pos` / `show.stats` | Yes | Yes | Yes | --- | --- | --- | --- | --- | --- | Yes | +| `status.*.color` | --- | --- | --- | --- | --- | --- | Yes | --- | --- | --- | +| `xbreaks` / `ybreaks` | Yes | Yes | Yes | Yes | Yes | Yes | --- | --- | --- | Yes | +| `xlim` / `ylim` | Yes | Yes | Yes | Yes | Yes | Yes | --- | Yes | --- | Yes | diff --git a/vignettes/04-gsynth.Rmd b/vignettes/07-gsynth.Rmd similarity index 61% rename from vignettes/04-gsynth.Rmd rename to vignettes/07-gsynth.Rmd index 79228a0a..09ef64be 100644 --- a/vignettes/04-gsynth.Rmd +++ b/vignettes/07-gsynth.Rmd @@ -1,52 +1,73 @@ # Gsynth Program {#sec-gsynth} -This chapter demonstrate the generalized synthetic control method, or Gsynth, proposed in @Xu2017 (\[Paper\]). Download the R code used in this chapter [here](rscript/04-gsynth.R). +```{r .common, include = FALSE} +source("_common.R") +``` + +This chapter demonstrates the generalized synthetic control method, or Gsynth, proposed in @Xu2017 \[Paper\]. Gsynth was originally implemented in the **gsynth** package but has now been fully integrated into the **fect** package. Gsynth (`method = "gsynth"`) and FEct/IFEct/MC (`method = "fe"/"ife"/"mc"`) are different in the following ways: - Gsynth is designed to handle block and staggered DID settings **without** treatment reversal, whereas other methods allow for treatment reversal under the assumption of limited carryover effects. -- Gsynth is particularly suited for cases where the number of treated units is small, including scenarios with only one treated unit. By setting `vartype = "parametric"`, we can use a two-stage parametric bootstrapping procedure to produce uncertainty estimates. In contrast, other methods rely on large samples, particularly a large number of treated units, to obtain reliable standard errors and confidence intervals using `"bootstrap"` or `"jackknife"`. -- Compared with IFEct (`method = "ife"`), Gsynth *does not* rely on pre-treatment data from the treated units to impute $\hat{Y}(0)$. This approach significantly speeds up computation and improves stability. +- Gsynth is particularly suited for the Synth setting, where the number of treated units is small, including scenarios with only one treated unit. By setting `vartype = "parametric"`, we can use a two-stage parametric bootstrapping procedure to produce uncertainty estimates. In contrast, other methods rely on large samples, particularly a large number of treated units, to obtain reliable standard errors and confidence intervals using `"bootstrap"` or `"jackknife"`. +- Compared with IFEct (`method = "ife"`), Gsynth *does not* rely on pre-treatment data from the treated units to estimate time components (e.g., factors). Hence, `time.component.from = "nevertreated"`. This approach speeds up computation, improves stability, and is more suitable for predictive inference based on row exchangeability. + +Therefore, we recommend setting `method = "gsynth"` in **fect** for the synthetic control setting, where the treatment does not reverse (or is coded accordingly) and the number of treated units is small. R script used in this chapter can be downloaded [here](https://raw.githubusercontent.com/xuyiqing/fect/dev/vignettes/rscript/07-gsynth.R). + +::: {.callout-important appearance="simple"} +### Key Equivalence +`fect(..., method = "gsynth")` is equivalent to `fect(..., method = "ife", time.component.from = "nevertreated")`. + +Gsynth is a procedure based on an IFE model estimated using only never-treated controls. The gsynth package was developed in the synthetic control setting, where a small number of treated units are compared with never-treated controls to learn latent time effects. +::: + +Use `method = "gsynth"` (or equivalently `time.component.from = "nevertreated"`) when: + +- The treatment does *not* reverse (block or staggered adoption only). +- The number of treated units is *small*, including $N_{tr} = 1$. +- You want *parametric bootstrap* (`vartype = "parametric"`) for uncertainty estimation — this is particularly useful with few treated units. +- You do *not* want pre-treatment data from treated units to influence time component (i.e., factor) estimation. -Therefore, we recommend setting `method = "gsynth"` in **fect** for scenarios where the treatment does not reverse (or is coded accordingly) and the number of treated units is small . +In contrast, the default regime (`time.component.from = "notyettreated"`, used in [Chapter @sec-fect]) accommodates treatment reversal and uses EM to estimate time components (time fixed effects, latent factors, and temporal dynamics) from all not-yet-treated observations. -We will use two datasets, `simgsynth` and `turnout`, to perform analyses in block and staggered DID settings. First, we load the two datasets: `simgsynth` and `turnout`: +We will use two datasets, `sim_gsynth` and `turnout`, to perform analyses in block and staggered DID settings. First, we load the two datasets: `sim_gsynth` and `turnout`: -```{r echo = FALSE} +```{r setup-seed, echo = FALSE} set.seed(1234) -rm(list = ls()) ``` -```{r, warning=FALSE, message=FALSE} -library(fect) -data(fect) +```{r load-packages, warning=FALSE, message=FALSE} +data(sim_gsynth) +data(turnout) ls() ``` -## Simulated Data +## Basic Usage -We start with the first example, `simgsynth`, a simulated dataset described in @Xu2017. +### Simulated Data -There are 5 treated units, 45 control units, and 30 time periods. The treatment kicks at Period 21 for all treated units, hence, a multiperiod DID set up. +We start with the first example, `sim_gsynth`, a simulated dataset described in @Xu2017. -```{r} -head(simgsynth) +There are 5 treated units, 45 control units, and 30 time periods. The treatment kicks at Period 21 for all treated units, hence a block DID setup. + +```{r head-sim_gsynth} +head(sim_gsynth) ``` Before we conduct any statistical analysis, it is helpful to visualize the data structure and spot missing values (if there are any). We can easily do so with the help of the **panelView** package. The figure below shows that: (1) there are 5 treated units and 45 control units; (2) the treated units start to be treated in period 21; and (3) there are no missing values, which is a rare case. -```{r cache = FALSE, fig.height=7, fig.width=7} +```{r sim-panelview-status, cache = FALSE, fig.height=7, fig.width=7, warning=FALSE} library(panelView) -panelview(Y ~ D, data = simgsynth, index = c("id","time"), pre.post = TRUE) +panelview(Y ~ D, data = sim_gsynth, index = c("id","time"), pre.post = TRUE) ``` The code chunk below visualizes the trends of outcome variable over the full panel across groups; different colors correspond to unique treatment statuses. -```{r cache = FALSE,fig.height=5, fig.width=7} -panelview(Y ~ D, data = simgsynth, index = c("id","time"), type = "outcome") +```{r sim-panelview-outcome, cache = FALSE,fig.height=5, fig.width=7} +panelview(Y ~ D, data = sim_gsynth, index = c("id","time"), type = "outcome") ``` -## Estimation +### Estimation We estimate the model using only the outcome variable ($Y$), treatment indicator ($D$), covariates ($X_1$) and ($X_2$), and group indicators ($id$) and ($time$). @@ -54,19 +75,17 @@ To implement the Gsynth algorithm proposed in Xu (2017), set `method = "gsynth"` ```{r sim2_onecore, cache = TRUE} system.time( -out <- fect(Y ~ D + X1 + X2, data = simgsynth, index = c("id","time"), +out <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), method = "gsynth", force = "two-way", CV = TRUE, r = c(0, 5), se = TRUE, nboots = 1000, vartype = 'parametric', - parallel = FALSE)) + parallel = TRUE, cores = 16)) ``` The first variable on the right-hand side is the binary treatment indicator, with remaining variables as controls. The `index` option designates unit and time indicators for fixed effects analysis. -The `force` option (`"none"`, `"unit"`, `"time"`, and `"two-way"`) specifies the additive component(s) of the fixed effects included in the model. The default option is `"two-way"` (including additive unit fixed effects). A cross-validation procedure is provided (when `CV = TRUE`) to select the number of unobserved factors within the interval of `r=c(0,5)`. When cross-validation is switched off, the first element in r will be set as the number of factors. Alternatively, for example, when `CV = FALSE`, r can take in a numeric value (`r= 0`). - -Setting `se = TRUE`, the algorithm can produce uncertainty measurements. When the number of treated units is small, a parametric bootstrap procedure is preferred (`vartype = "parametric"`). The default number of parametric bootstrap runs is set to 200. Alternatively, non-parametric bootstrap procedure is also available. (`vartype = "bootstrap"`); note, it only works well when the treatment group is relatively large, (e.g. $Ntr>40$). The number of bootstrap runs can be set by `nboots`. +The `force` option (`"none"`, `"unit"`, `"time"`, and `"two-way"`) specifies the additive component(s) of the fixed effects included in the model. The default option is `"two-way"` (including additive unit fixed effects). A cross-validation procedure is provided (when `CV = TRUE`) to select the number of unobserved factors within the interval of `r=c(0,5)`. When cross-validation is switched off, the first element in r will be set as the number of factors. Alternatively, for example, when `CV = FALSE`, r can take in a numeric value ($r = 0$). -Because the core function of Gsynth in **fect** is written in C++, the algorithm runs relatively fast. The entire procedure (including cross-validation and 1,000 bootstrap runs) takes only a few seconds on a 2023 Macbook Pro Max. +Setting `se = TRUE`, the algorithm can produce uncertainty measurements. When the number of treated units is small, a parametric bootstrap procedure is preferred (`vartype = "parametric"`). The default number of parametric bootstrap runs is set to 200. The number of bootstrap runs can be set by `nboots`. The algorithm prints out the results automatically. `sigma2` stands for the estimated variance of the error term; `IC` represents the Bayesian Information Criterion; and `MPSE` is the Mean Squared Prediction Error. The cross-validation procedure selects an `r*` that minimizes the MSPE. @@ -74,41 +93,51 @@ Users can also use the `print` function to directly retrieve specific results. H - `est.att` reports the average treatment effect on the treated (ATT) by period - `est.avg` shows the ATT averaged over all periods -- `est.beta` represents the coefficients of the time-varying covariates +- `beta` represents the coefficients of the time-varying covariates -```{r eval = FALSE} +```{r print-results, eval = FALSE} print(out) out$est.att out$est.avg -out$est.beta +out$beta ``` -Treatment effect estimates from each bootstrap run is stored in `att.boot`, an array whose dimension = (#time periods \* #treated \* #bootstrap runs). +Treatment effect estimates from each bootstrap run are stored in `att.boot`, an array whose dimension = (#time periods \* #treated \* #bootstrap runs). -The full list of generated estimates of a **fect** object can be found on [GitHub](https://github.com/xuyiqing/fect/blob/master/man/fect.Rd) (Line 134-239). - -Parallel computing will speed up the bootstrap procedure significantly. When `parallel = TRUE` (default) and `cores` options are omitted, the algorithm will detect the number of available cores on your computer automatically. - -(Warning: it may consume most of your computer's computational power if all cores are being used.) +::: {.callout-note appearance="simple"} +**Parallel computing** will speed up both cross-validation and uncertainty estimation significantly. We recommend that users manually set the number of cores using the `cores` option. If this is not supplied or is `NULL`, we will automatically select the smaller of `8` and the number of usable system cores minus `2`, to prevent excessive use of system resources. +::: ```{r sim2, cache = TRUE, warning = FALSE} system.time( -out <- fect(Y ~ D + X1 + X2, data = simgsynth, index = c("id","time"), method = "gsynth", force = "two-way", CV = TRUE, r = c(0, 5), se = TRUE, nboots = 1000,vartype = 'parametric', parallel = TRUE, cores = 16) +out <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), method = "gsynth", force = "two-way", CV = TRUE, r = c(0, 5), se = TRUE, nboots = 1000, vartype = 'parametric', parallel = TRUE, cores = 16) ) ``` Gsynth in **fect** also incorporates jackknife method for uncertainty estimates. ```{r simJack, cache = TRUE, message = FALSE} -out2 <- fect(Y ~ D + X1 + X2, data = simgsynth, index = c("id","time"), - method = "gsynth", force = "two-way", +out2 <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), + method = "gsynth", force = "two-way", CV = TRUE, r = c(0, 5), se = TRUE, - vartype = "jackknife", + vartype = "jackknife", parallel = TRUE, cores = 16) ``` -## Visualizing Results +### Cross-validation + +Not all `cv.method` options are available in the Synth/Gsynth settings: + +| | `fect_cv` (DID/TWFE) | CV in Gsynth | `fect_mspe` (post-hoc) | +|:--|:--|:--|:--| +| `"all_units"` | Default | Available | Default | +| `"treated_units"` | Available | Default | Available | +| `"loo"` | --- | Available | --- | + +The `"loo"` method is exclusive to Gsynth (`time.component.from = "nevertreated"`) because it requires factor estimation to be independent of the held-out treated observations. In the `"nevertreated"` setting, factors are estimated from control units only, so holding out treated pre-treatment periods does not compromise estimation. In the `"notyettreated"` setting, all units contribute to estimation, making LOO less clean; use `"treated_units"` instead. + +### Visualizing Results By default, the `print` function produces a *gap* plot, equivalent to using `plot(out, type = "gap")`, and visualizes the estimated Average Treatment Effect on the Treated (ATT) by period. For reference, the true effects in `gsynth` range from 1 to 10 (with some added white noise) and take effect during periods 21 to 30. @@ -127,14 +156,14 @@ plot(out, theme.bw = FALSE) By switching on `connected`, the confidence interval of the ATT estimates will be represented by a shaded area. -```{r fig.height=5, fig.width=7} -plot(out, connected = TRUE) +```{r sim-gap-connected, fig.height=5, fig.width=7} +plot(out, connected = TRUE) ``` Moreover, by switching off `show.points`, ATT estimates can be displayed as a line plot. -```{r fig.height=5, fig.width=7} -plot(out, connected = TRUE, show.points = FALSE) +```{r sim-gap-line, fig.height=5, fig.width=7} +plot(out, connected = TRUE, show.points = FALSE) ``` Aesthetic options for ggplot2 objects are compatible for all **fect** plots. @@ -142,49 +171,44 @@ Aesthetic options for ggplot2 objects are compatible for all **fect** plots. For demonstration, we use `main`, `xlim`, and `ylim` to set the plot title and axis labels. ```{r sim_gap2, fig.height=5, fig.width=7} -plot(out, type = "gap", ylim = c(-3,12), xlab = "Period", - main = "Estimated ATT (FEct)") +plot(out, type = "gap", ylim = c(-6,12), xlab = "Period", + main = "Estimated ATT (Gsynth)") ``` **fect** objects can generate eight types of plots for diverse demonstration purposes. The `type` option includes the following: - `"gap"`: Reports ATT by period (default). +- `"counterfactual"`: Generates the observed treated outcome alongside the imputed counterfactual averages. The style can be modified with `raw = "all"` for linear or `raw = "band"` for graphic representation of all estimated counterfactual results. +- `"factors"` and `"loadings"`: Plot the estimated factors and loadings, respectively. - `"status"`: Plots treatment status of all observations, similar to `panelview()`. The time labels are displayed only when `axis.lab = "time"` is set. - `"box"`: Displays the estimated individual treatment effects with box plot. - `"calendar"`: Shows how the treatment effect evolves over time. -- `"factors"` and `"loadings"`: Plot the estimated factors and loadings, respectively. -- `"counterfactual"`: Generates the observed treated outcome alongside the imputed counterfactual averages. The style can be modified with `raw = "all"` for linear or `raw = "band"` for graphic representation of all estimated counterfactual results. -- `"equiv"`: Exhibits the average pretreatment residuals with equivalence confidence intervals. +- `"equiv"`: Exhibits the average pre-treatment residuals with equivalence confidence intervals. -The figure below illustrates the treatment status of all observations. Users can specify the exact values shown on the x- and y-axes using the `xticklabels` and `yticklabels` parameters (i.e., `xticklabels=c("1","5","10", "15","20", "25", "30")`). Setting a nonexistent value to the option removes all numeric labels for that axis (i.e., `yticklabels="0"`). The `status` plot has now been incorporated into a standalone package, **panelView**. +To visualize the estimated counterfactual outcomes, we use `type = "counterfactual"`. If no argument is assigned to `raw`, the plot will display the average of the *treated* and of the *estimated counterfactual* outcomes. When `method = "gsynth"` and `vartype = "parametric"`, the 95% confidence intervals around the counterfactual outcomes will be the same as for the event-study ATT (though flipped in shape). -```{r sim_status, cache = FALSE,fig.height=7, fig.width=7} -plot(out, type = "status", yticklabels="0", - xticklabels=c("5", "10", "15","20", "25", "30") ) +```{r sim-counterfactual, cache = FALSE, fig.height=5, fig.width=7} +plot(out, type = "counterfactual") ``` -The box plot visualize the estimated individual treatment effects for the treated units. While these effects are not identified at the individual level, their dispersion provides insight into heterogeneous treatment effect across different time periods and informs model performance. By default, the number of total treated units is labeled on the graph. Note, if the number of treated units is small, the box plot will reduce to a scatter plot, as shown below. - -We also provide the `xangle` and `yangle` options to allow users to tilt the labels for better display. We will demonstrate the functionality in later examples. +**fect** offers two options for displaying all estimated counterfactual estimates: `raw = "all"` for linear and `raw = "band"` for a graphical (shaded band) representation. -```{r sim_box, cache = FALSE,fig.height=5, fig.width=8} -plot(out, type = "box", xlab = "time", - xticklabels=c("-19", "-15", "-10", "-5","0","5","10") ) +```{r sim-counterfactual-all, cache = FALSE,fig.height=5, fig.width=7} +plot(out, type = "counterfactual", raw = "all") ``` -If we want to focus on specific periods within the full panel, the `xlim` option is useful. Here, we narrow the time frame to fifteen periods before and ten periods after the treatment. +Note that in the plot below, the shaded areas represent the 5-95% quantiles of the treated and estimated counterfactual trajectories, not their uncertainty estimates. -```{r sim_box2, eval = FALSE, fig.height=7, fig.width=7} -plot(out, type = "box", xlim = c(-15, 10), - xticklabels=c( "-15", "-10", "-5","0","5","10")) +```{r sim-counterfactual-band, cache = FALSE,fig.height=5, fig.width=7} +plot(out, type = "counterfactual", raw = "band") ``` -To explore how the treatment effect evolves over time, we can set `type = "calendar"`. -In the plot below, the points represent the ATTs by calendar time. The blue curve shows a lowess fit of these estimates, with the shaded band indicating the 95% confidence interval. The red horizontal dashed line marks the overall average ATT across all time periods. +The figure below illustrates the treatment status of all observations. Users can specify the exact values shown on the x- and y-axes using the `xticklabels` and `yticklabels` parameters (i.e., `xticklabels=c("1","5","10", "15","20", "25", "30")`). Setting a nonexistent value to the option removes all numeric labels for that axis (i.e., `yticklabels="0"`). The `status` plot has now been incorporated into a standalone package, **panelView**. -```{r calendar, cache = FALSE,fig.height=5, fig.width=7} -plot(out,type = "calendar") +```{r sim_status, cache = FALSE,fig.height=7, fig.width=7} +plot(out, type = "status", yticklabels="0", + xticklabels=c("5", "10", "15","20", "25", "30") ) ``` The next two figures plot the estimated factors and factor loadings, respectively. @@ -197,79 +221,49 @@ plot(out, type = "loadings") plot(out, type = "factors", xlab = "Time") ``` -To visualize the estimated counterfactual outcomes, we use `type = "counterfactual"`. If no argument is assigned to `raw`, the plot will display the average of the *treated* and of the *estimated counterfactual* outcomes. When `method = "gsynth"` and `vartype = "parametric"`, the 95% confidence intervals around the counterfactual outcomes will be the same as for the event-study ATT (though flipped in shape). -```{r cache = FALSE, fig.height=5, fig.width=7} -plot(out, type = "counterfactual") +The box plot visualizes the estimated individual treatment effects for the treated units. While these effects are not identified at the individual level, their dispersion provides insight into heterogeneous treatment effects across different time periods and informs model performance. By default, the number of total treated units is labeled on the graph. Note, if the number of treated units is small, the box plot will reduce to a scatter plot, as shown below. + +We also provide the `xangle` and `yangle` options to allow users to tilt the labels for better display. We will demonstrate the functionality in later examples. + +```{r sim_box, cache = FALSE,fig.height=5, fig.width=8} +plot(out, type = "box", xlab = "time", + xticklabels=c("-19", "-15", "-10", "-5","0","5","10") ) ``` -**fect** offers two options for displaying all estimated counterfactual estimates: `raw = "all"` for linear and `raw = "band"` for a graphical (shaded band) representation. +If we want to focus on specific periods within the full panel, the `xlim` option is useful. Here, we narrow the time frame to fifteen periods before and ten periods after the treatment. -```{r cache = FALSE,fig.height=5, fig.width=7} -plot(out, type = "counterfactual", raw = "all") +```{r sim_box2, eval = FALSE, fig.height=7, fig.width=7} +plot(out, type = "box", xlim = c(-15, 10), + xticklabels=c( "-15", "-10", "-5","0","5","10")) ``` -Note that in the plot below, the shaded areas represent the 5-95% quantiles of the treated and estimated counterfactual trajectories, not their uncertainty estimates. +To explore how the treatment effect evolves over time, we can set `type = "calendar"`. -```{r cache = FALSE,fig.height=5, fig.width=7} -plot(out, type = "counterfactual", raw = "band") +In the plot below, the points represent the ATTs by calendar time. The blue curve shows a lowess fit of these estimates, with the shaded band indicating the 95% confidence interval. The red horizontal dashed line marks the overall average ATT across all time periods. + +```{r calendar, cache = FALSE,fig.height=5, fig.width=7} +plot(out,type = "calendar") ``` -Lastly, `type = "equiv"` helps visualize the average pretreatment residuals with equivalence confidence intervals. +Lastly, `type = "equiv"` helps visualize the average pre-treatment residuals with equivalence confidence intervals. -```{r cache = FALSE, fig.height=5, fig.width=7} +```{r sim-equiv, cache = FALSE, fig.height=5, fig.width=7} plot(out, type = "equiv", ylim = c(-5, 5)) ``` The floating legend displaying the F-test p-value and Equivalence test p-value can be removed by setting `show.stats = FALSE`. -```{r cache = FALSE, fig.height=5, fig.width=7} +```{r sim-equiv-no-stats, cache = FALSE, fig.height=5, fig.width=7} plot(out, type = "equiv", show.stats = FALSE) ``` Alternatively, it can be repositioned by providing coordinates to `stats.pos`. -```{r cache = FALSE, fig.height=5, fig.width=7} +```{r sim-equiv-reposition, cache = FALSE, fig.height=5, fig.width=7} plot(out, type = "equiv", stats.pos = c(-19, 4.5), ylim = c(-5, 5)) ``` -## Comparing w/ IFEct & MC - -Since we have now merged **gysnth** into **fect**, the original `ife` and `mc` methods in **gysnth** now can be directly implemented in **fect**. Please refer to the previous chapter for implementation details. - -Here, we apply these two methods to the simulated dataset `simgsynth` and compare the results from those from setting `method = "gysnth"`. Note that not only the algorithms are (slightly) different, the inferential methods are different, too. Both `ife` and `mc` use large-sample inferential method such as nonparametric bootstrap or jackknife while `gsynth` employs a two-step parametric bootstrap procedure, analogous to conformal inference, which accommodates a small number of treated units. - -### IFEct - -For the `ife` method, we need to specify an interval of candidate number of unobserved factors in option r like `r=c(0,5)`. When cross-validation is switched off, the first element in r will be set as the number of factors. Below we use the MSPE criterion and search the number of factors from 0 to 5. - -```{r sim_ife, cache = TRUE, message = FALSE} -out.ife <- fect(Y ~ D + X1 + X2, data = simgsynth, index = c("id","time"), - force = "two-way", method = "ife", CV = TRUE, r = c(0, 5), - se = TRUE, nboots = 200, parallel = TRUE) -``` - -The figure below shows the estimated ATT using the IFE method. The cross-validation procedure selects the correct number of factors $(r=2)$. - -```{r sim_ife2, fig.height=5, fig.width=7} -plot(out.ife, main = "Estimated ATT (EM)") -``` - -### Matrix completion - -To implement the MC method, a sequence of candidate tuning parameters must be specified. For example, users can set `lambda = c(1, 0.8, 0.6, 0.4, 0.2, 0.05)`. If `lambda` is not specified, **fect** uses an algorithm to automatically generate a set of candidate tuning parameters based on the outcome variable. Users can adjust the number of tuning parameters with `nlambda`, which defaults to `nlambda = 10`. - -```{r sim_mc, cache = TRUE} -out.mc <- fect(Y ~ D + X1 + X2, data = simgsynth, - index = c("id","time"), - force = "two-way", method = "mc", CV = TRUE, - se = TRUE, nboots = 200, parallel = TRUE) - -``` - -```{r sim_MC, fig.height=5, fig.width=7} -plot(out.mc, main = "Estimated ATT (MC)") -``` ------------------------------------------------------------------------ @@ -281,7 +275,7 @@ The second example examines the impact of Election-Day Registration (EDR) reform First, we use **panelView** to visualize the data structure. The following figure shows that (1) we have a balanced panel with 9 treated units and (2) the treatment starts at different time periods. -```{r cache = FALSE, warning=FALSE, fig.height=10, fig.width=7} +```{r turnout-panelview-status, cache = FALSE, warning=FALSE, fig.height=10, fig.width=7} panelview(turnout ~ policy_edr, data = turnout, index = c("abb","year"), pre.post = TRUE, by.timing = TRUE) @@ -289,8 +283,8 @@ panelview(turnout ~ policy_edr, data = turnout, **panelView** can visualize the outcome variable by group, using colored lines to represent changes in treatment status. -```{r cache = FALSE, warning =FALSE, fig.height=5, fig.width=7} -panelview(turnout ~ policy_edr, data = turnout, +```{r turnout-panelview-outcome, cache = FALSE, warning =FALSE, fig.height=5, fig.width=7} +panelview(turnout ~ policy_edr, data = turnout, index = c("abb","year"), type = "outcome", main = "EDR Reform and Turnout", by.group = TRUE) @@ -309,7 +303,7 @@ out0 <- fect(turnout ~ policy_edr + policy_mail_in + policy_motor, ``` ```{r turnout_did_gap, fig.height=5, fig.width=7} -plot(out0, type = "gap", xlim = c(-15, 15)) +plot(out0, type = "gap", xlim = c(-15, 5), ylim=c(-15, 10)) ``` ### Estimation w/ factors @@ -328,7 +322,7 @@ out_turnout <- fect(turnout ~ policy_edr + policy_mail_in + policy_motor, `out$wgt.implied` ($N_{co}\times N_{tr}$) stores the implied weights of all control units for each treated unit. Different from the synthetic control method, the weights can take both positive and negative values. Below we show the control unit weights for Wisconsin. -```{r} +```{r turnout-implied-weights} dim(out_turnout$wgt.implied) sort(out_turnout$wgt.implied[,8]) ``` @@ -338,12 +332,12 @@ sort(out_turnout$wgt.implied[,8]) Like we have demonstrated with the multiperiod DID analysis, we also use the gap (default) plot to visualize the ATT. ```{r turnout_gap, fig.height=5, fig.width=7} -plot(out_turnout, xlim = c(-10, 5), ylim=c(-15,15)) +plot(out_turnout, xlim = c(-10, 5), ylim=c(-15, 10)) ``` For staggered DID, a status plot can be generated after estimation. Here, we use `xlab`, `ylab`, and `main` to modify the axis labels and the graph title, respectively. Additionally, `xangle` and `yangle` can tilt the numeric labels to improve readability. Note that the values provided for these options specify the degree of tilt. -```{r, fig.height=12, fig.width=7} +```{r turnout-status-plot, fig.height=12, fig.width=7} plot(out_turnout, type = "status",xlab = "Year", ylab = "State", main = "Treatment Status", xticklabels=c(1920, 1928, 1936, 1944, 1952, 1960, 1968, 1976, 1984, 1992, 2000, 2008), xangle=10) @@ -374,13 +368,13 @@ Here, we visualize the estimated average treatment effects by calendar time. plot(out_turnout, type = "calendar", ylim = c(-15,15)) ``` -Estimated factors and factor loadings are ploted below. +Estimated factors and factor loadings are plotted below. -```{r turnout_F, message = FALSE, results = 'hide', fig.height=5, fig.width=7} +```{r turnout_F, message = FALSE, results = 'hide', fig.height=5, fig.width=7, warning=FALSE} plot(out_turnout, type = "factors", xlab = "Year") ``` -```{r turnout_L, message = FALSE, results = 'hide', fig.height=7, fig.width=7} +```{r turnout_L, message = FALSE, results = 'hide', fig.height=7, fig.width=7, warning=FALSE} plot(out_turnout, type = "loadings") ``` @@ -390,7 +384,7 @@ plot(out_turnout, type = "loadings") Gsynth in **fect** can accommodate unbalanced panels. To illustrate how it works, we randomly remove 50 observations as well as the first 15 observations of Wyoming from the turnout dataset and then re-estimate the model. -```{r} +```{r create-unbalanced-data} set.seed(123456) turnout.ub <- turnout[-c(which(turnout$abb=="WY")[1:15], sample(1:nrow(turnout),50,replace=FALSE)),] @@ -411,7 +405,7 @@ out_ub <- fect(turnout ~ policy_edr + policy_mail_in + policy_motor, data = turnout.ub, index = c("abb","year"), se = TRUE, method = "gsynth", r = c(0, 5), CV = TRUE, force = "two-way", - parallel = TRUE, min.T0 = 8, + parallel = TRUE, cores = 16, min.T0 = 8, nboots = 1000, seed = 02139) ``` @@ -445,23 +439,67 @@ We re-produce the gap plot with the unbalanced panel, here, we set the range of plot(out_ub, type = "gap", ylim = c(-10, 20)) ``` -### Matrix completion -Finally, we re-estimate the model using the matrix completion method. +------------------------------------------------------------------------ + +## CFE with GSynth + +Gsynth is not limited to the IFE method. The complex fixed effects (CFE) estimator can also be used with `time.component.from = "nevertreated"`. This is useful when the data generating process involves not only latent interactive fixed effects but also additional additive fixed effects (e.g., region, industry), unit-specific time trends (via `Q.type`), or time-invariant covariates with time-varying coefficients (`Z`/`gamma`). See [Chapter @sec-cfe] for the full CFE tutorial. -```{r turnout_ub_est2, cache = TRUE, message = FALSE} -out.mc_ub <- fect(turnout ~ policy_edr + policy_mail_in + policy_motor, - min.T0 = 8, data = turnout.ub, - index = c("abb","year"), method = "mc", - se = TRUE, nboots = 1000, seed = 02139) +### Example: CFE + nevertreated + +```{r cfe_nt_demo, eval=TRUE, cache=TRUE, message=FALSE, results='hide'} +out.cfe.nt <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), + method = "cfe", force = "two-way", + time.component.from = "nevertreated", + Q.type = "linear", + se = FALSE, CV = TRUE, r = c(0, 5), + parallel = TRUE, cores = 16) +``` + +```{r cfe-nt-summary} +cat("CFE + nevertreated: r.cv =", out.cfe.nt$r.cv, + ", ATT =", round(out.cfe.nt$att.avg, 3), "\n") ``` -Again, we use a gap plot to visualize estimated ATT. +::: {.callout-note appearance="simple"} +Under `time.component.from = "nevertreated"`, the CFE estimator uses only never-treated controls for factor estimation — the same principle as gsynth. The additional CFE components (extra FE, time trends, Z/gamma) are estimated on the control panel and projected onto treated units alongside the interactive factors. See [Chapter @sec-cfe] for the full CFE tutorial. +::: + +### Comparing gsynth and CFE + nevertreated + +Both approaches share the same estimation regime (never-treated controls only), but CFE can add structural components on top. Whether the extra flexibility helps depends on the data generating process. We compare three specifications using `fect_mspe()`: + +```{r cfe_nt_vs_gsynth, eval=TRUE, cache=TRUE, message=FALSE, results='hide'} +# Model 1: gsynth (pure IFE, r = 2) +out.gsynth.comp <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), + method = "gsynth", force = "two-way", + r = 2, se = FALSE, CV = FALSE) -```{r turnout_ub_mc_gap, fig.height=5, fig.width=7} -plot(out.mc_ub, main = "Estimated ATT (MC)", ylim = c(-10, 20)) +# Model 2: CFE + nevertreated with r = 2 only (equivalent to gsynth) +out.cfe.nt.comp <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), + method = "cfe", force = "two-way", + time.component.from = "nevertreated", + r = 2, se = FALSE, CV = FALSE) + +# Model 3: CFE + nevertreated with r = 2 and linear trend (overspecified) +out.cfe.nt.lin <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), + method = "cfe", force = "two-way", + time.component.from = "nevertreated", + Q.type = "linear", r = 2, se = FALSE, CV = FALSE) +``` + +```{r cfe_nt_mspe, eval=TRUE, cache=TRUE} +mspe.comp <- fect_mspe(list(gsynth_r2 = out.gsynth.comp, + CFE_r2 = out.cfe.nt.comp, + CFE_linear_r2 = out.cfe.nt.lin), seed = 1234) +print(mspe.comp$summary[, c("Model", "MSPE", "RMSE", "MAD")]) ``` +Since `sim_gsynth` follows a pure IFE data generating process with two factors, Models 1 and 2 should produce identical MSPE --- confirming that CFE with `time.component.from = "nevertreated"` and no additional structure is numerically equivalent to gsynth. Model 3, which adds unnecessary linear trends, should produce similar or slightly worse MSPE because the extra parameters add noise without benefit when the true DGP has no unit-specific trends. + +------------------------------------------------------------------------ + ## Additional Notes 1. **Unbalanced Panels**: Running Gsynth within **fect** on unbalanced panels will take significantly more time compared to balanced panels, often by a factor of 100:1 or more. This is because the EM algorithm, which fills in missing values (implemented in C++), requires many more iterations to converge. To reduce run-time, users can remove units or time periods with extensive missing values. Understanding the data structure before running any regressions is always helpful. Note that this approach differs from setting `method = "ife"`, as no pre-treatment data from the treated units is used to impute $\hat{Y}(0)$. diff --git a/vignettes/06-panel.Rmd b/vignettes/08-panel.Rmd similarity index 95% rename from vignettes/06-panel.Rmd rename to vignettes/08-panel.Rmd index 67de15ca..c64e6516 100644 --- a/vignettes/06-panel.Rmd +++ b/vignettes/08-panel.Rmd @@ -4,11 +4,14 @@ editor: wrap: sentence --- -# New DID Methods {#sec-panel} +# Modern DID Methods {#sec-panel} + +```{r .common, include = FALSE} +source("_common.R") +``` This chapter, authored by Ziyi Liu and Yiqing Xu, complements @CLLX2025 ([paper](https://yiqingxu.org/papers/english/2023_panel/CLLX.pdf), [slides](https://yiqingxu.org/papers/english/2023_panel/CLLX_slides.pdf)). -Rivka Lipkovitz also contributes to this tutorial. -Download the R code used in this chapter [here](rscript/06-panel.R). +Rivka Lipkovitz also contributes to this tutorial. R script used in this chapter can be downloaded [here](https://raw.githubusercontent.com/xuyiqing/fect/dev/vignettes/rscript/08-panel.R). ------------------------------------------------------------------------ @@ -22,13 +25,19 @@ In the process, we will present a recommended pipeline for analyzing panel data, We illustrate these methods with two empirical examples: @HH2019 (without treatment reversals) and @GS2020 (with treatment reversals). +::: {.callout-note appearance="simple"} +## macOS Users + +Some packages in this chapter (e.g., `DIDmultiplegtDYN`) depend on `rgl`, which requires an X11 system. If you encounter graphics-related errors, install [XQuartz](https://www.xquartz.org/), the X11 implementation for macOS, then restart R. +::: + ## Install Packages To begin, you will need to install the necessary packages from CRAN and GitHub. -```{r message = FALSE, warning = FALSE} +```{r install-packages, message = FALSE, warning = FALSE} # install packages from CRAN -packages <- c("dplyr", "fixest", "did", "didimputation", +packages <- c("dplyr", "fixest", "did", "didimputation", "panelView", "ggplot2", "bacondecomp", "HonestDiD", "DIDmultiplegtDYN", "PanelMatch", "readstata13") install.packages(setdiff(packages, rownames(installed.packages()))) @@ -46,22 +55,24 @@ if ("HonestDiDFEct" %in% rownames(installed.packages()) == FALSE) { Load libraries: -```{r message = FALSE, warning = FALSE} +```{r load-libraries, message = FALSE, warning = FALSE} library(dplyr) library(readstata13) library(fixest) library(did) -library(fect) library(panelView) library(PanelMatch) library(ggplot2) library(bacondecomp) -library(fect) library(didimputation) library(doParallel) library(HonestDiD) library(HonestDiDFEct) -library(DIDmultiplegtDYN) # may require XQuartz +has_polars <- requireNamespace("polars", quietly = TRUE) +if (has_polars) { + library(polars) + library(DIDmultiplegtDYN) # requires polars; may require XQuartz for rgl +} ``` ## No Treatment Reversals @@ -69,8 +80,8 @@ library(DIDmultiplegtDYN) # may require XQuartz We begin with an empirical example from @HH2019, who investigate the effects of indirect democracy versus direct democracy on naturalization rates in Switzerland using municipality-year panel data from 1991 to 2009. The study finds that switching from direct to indirect democracy increased naturalization rates by an average of 1.22 percentage points (Model 1, Table 1). -```{r message = FALSE, warning = FALSE} -data(fect) +```{r load-hh2019, message = FALSE, warning = FALSE} +data(hh2019) data <- hh2019 head(data) ``` @@ -273,10 +284,10 @@ The stacked DID plot closely resembles that of the TWFE model, suggesting that p ### Interaction Weighted @sun2021-event proposes an interaction-weighted (IW) estimator for estimating the ATT. -the IW estimator computes a weighted average of ATT estimates for each cohort, obtained from a TWFE regression where cohort dummies are fully interacted with indicators of relative time to treatment onset. +The IW estimator computes a weighted average of ATT estimates for each cohort, obtained from a TWFE regression where cohort dummies are fully interacted with indicators of relative time to treatment onset. See a simple illustration below. -```{r, echo=FALSE, out.width="50%", fig.align="center"} +```{r fig-iw-illustration, echo=FALSE, out.width="50%", fig.align="center"} knitr::include_graphics("fig/fig_iw.png") ``` @@ -294,7 +305,7 @@ model.sa.1 <- feols(nat_rate_ord~sunab(FirstTreat,year)|bfs+year, summary(model.sa.1,agg = "ATT") ``` -The estimation results are saved `coeftable` for plotting. +The estimation results are saved in `coeftable` for plotting. We can make an event study plot as before. The results are quite similar to TWFE and stacked DID. @@ -381,7 +392,7 @@ p.cs.1.u One advantage of CSDID is the ability to use **not-yet-treated** units as the comparison group by setting `control_group = "notyettreated"`. See a simple illustration below. -```{r, echo=FALSE, out.width="50%", fig.align="center"} +```{r fig-cs-illustration, echo=FALSE, out.width="50%", fig.align="center"} knitr::include_graphics("fig/fig_cs.png") ``` @@ -439,13 +450,13 @@ For each placebo period $k$, the command compares the outcome evolution of switc See a simple illustration below with $l = 2$ (number of lags, capturing post-treatment effects) and $k = 2$ (number of leads, capturing pre-treatment placebo effects). -```{r, echo=FALSE, out.width="50%", fig.align="center"} +```{r fig-pm-illustration, echo=FALSE, out.width="50%", fig.align="center"} knitr::include_graphics("fig/fig_pm.png") ``` In the following example, we set $l = 12$ and $k = 9$. -```{r hh_didm, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE, eval=requireNamespace("polars", quietly=TRUE)} +```{r hh_didm, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE, eval=has_polars} didm.results <- did_multiplegt_dyn( df = df.use, outcome = "nat_rate_ord", @@ -463,7 +474,7 @@ print(didm.results) Again, we make an event study plot using `esplot`. -```{r hh_didm.dynamic, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE, eval=requireNamespace("polars", quietly=TRUE)} +```{r hh_didm.dynamic, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE, eval=has_polars} T.post <- dim(didm.results$results$Effects)[1] T.pre <- dim(didm.results$results$Placebos)[1] didm.vis <- rbind(didm.results$results$Placebos,didm.results$results$Effects) @@ -489,7 +500,7 @@ Note that the terms *lead* and *lag* here are opposite to their traditional usag To assign equal weights to all control units in each matched set, we set `refinement.method = "none"` (without matching on controls). Notably, by matching on treatment history and defining the lead window, `PanelMatch` uses only the subset of treated units with three pre-treatment periods and four post-treatment periods to compute the average treatment effects. -The PanelMatch estimator is equivalent to the DIDmulitple estimator proposed by @CDH2020 if we only match on the last pre-treatment period and only estimate the treatment effect at the first post-treatment period. +The PanelMatch estimator is equivalent to the DIDmultiple estimator proposed by @CDH2020 if we only match on the last pre-treatment period and only estimate the treatment effect at the first post-treatment period. ```{r hh_pm, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE} df.pm <- df.use @@ -526,7 +537,7 @@ PM.results.placebo <- PanelMatch(lag=3, We can estimate the ATT and dynamic treatment effects using the function `PanelEstimate`. To obtain the ATT, we set the option `pooled = TRUE`. The standard error is calculated using a block bootstrapping method. -This is different from the standard boostrapping method, which is not valid for matching estimators \cite{abadie2008failure}. +This is different from the standard boostrapping method, which is not valid for matching estimators [@abadie2008failure]. ```{r hh_pm1, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE} # ATT @@ -563,7 +574,7 @@ Now, we return to the imputation method, which **fect** is originally designed t See a simple illustration below. For details, see @sec-fect. -```{r, echo=FALSE, out.width="50%", fig.align="center"} +```{r fig-fect-illustration, echo=FALSE, out.width="50%", fig.align="center"} knitr::include_graphics("fig/fig_fect.png") ``` @@ -667,8 +678,8 @@ In the original paper, the authors use district by election-cycle panel data fro House general elections between 1980 and 2012, arguing that the presence of Asian (Black/Latino) candidates in general elections increases the proportion of campaign contributions from Asian (Black/Latino) donors. Here, we focus specifically on the effects of Asian candidates, as shown in the top left panel of Figure 5 in the paper. -```{r message = FALSE, warning = FALSE} -data(fect) +```{r load-gs2020, message = FALSE, warning = FALSE} +data(gs2020) data <- gs2020 data$cycle <- as.integer(as.numeric(data$cycle/2)) head(data) @@ -739,7 +750,7 @@ twfe.est <- feols(general_sharetotal_A_all ~ data = data_cohort, cluster = "district_final") ``` -We then visualize the estimated dynamic treatment. +We then visualize the estimated dynamic treatment effects. ```{r gb_twfeplot, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE} twfe.output <- as.data.frame(twfe.est$coeftable[c(1:25),]) @@ -832,12 +843,12 @@ Both are very close to the TWFE estimates. model.fect <- fect(Y = "general_sharetotal_A_all", D = "cand_A_all", X= c("cand_H_all", "cand_B_all"), data = data, method = "fe", index = index, se = TRUE, - parallel = TRUE, seed = 1234, force = "two-way") + parallel = TRUE, cores = 16, seed = 1234, force = "two-way") print(model.fect$est.avg) ``` -We visualize the estimated dynamic treatment for the counterfactual estimator in the same way. +We visualize the estimated dynamic treatment effects for the counterfactual estimator in the same way. ```{r gb_fectplot, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE} fect.output <- as.data.frame(model.fect$est.att) @@ -948,7 +959,9 @@ res_st <- did_wrapper( D = "indirect", index = c("bfs", "year"), method = "st", - se = "boot" + se = "boot", + nboots = 200, + parallel = TRUE ) print(res_st) ``` diff --git a/vignettes/07-sens.Rmd b/vignettes/09-sens.Rmd similarity index 66% rename from vignettes/07-sens.Rmd rename to vignettes/09-sens.Rmd index c13f0226..c1e96ee4 100644 --- a/vignettes/07-sens.Rmd +++ b/vignettes/09-sens.Rmd @@ -1,20 +1,22 @@ # Sensitivity Analysis {#sec-panel-sens} -Download the R code used in this chapter [here](rscript/07-sens.R). +```{r .common, include = FALSE} +source("_common.R") +``` ------------------------------------------------------------------------ -@rambachan2023more propose a partial identification approach that relaxes the PT assumption in the post-treatment period by allowing violations that do not exceed the magnitude of those observed in the pre-treatment period. This framework enables sensitivity analysis of estimates from **fect** or similar methods by comparing pre-treatment deviations from parallel trends (PT) to potential post-treatment deviations. +@rambachan2023more propose a partial identification approach that relaxes the parallel trends assumption in the post-treatment period by allowing violations that do not exceed the magnitude of those observed in the pre-treatment period. This framework enables sensitivity analysis of estimates from **fect** or similar methods by comparing pre-treatment deviations from parallel trends to potential post-treatment deviations. -The key intuition is that if an event study demonstrates strong post-treatment effects yet only minor PT deviations before treatment, any post-treatment departure large enough to reverse these findings must be substantially larger than those observed in the pre-treatment period. Consequently, this approach quantifies how sensitive the estimated dynamic treatment effects are to possible PT violations, using pretrend estimates as the benchmark. +The key intuition is that if an event study demonstrates strong post-treatment effects yet only minor parallel trends deviations before treatment, any post-treatment departure large enough to reverse these findings must be substantially larger than those observed in the pre-treatment period. Consequently, this approach quantifies how sensitive the estimated dynamic treatment effects are to possible parallel trends violations, using pretrend estimates as the benchmark. -Below, we illustrate how to apply this sensitivity analysis with **fect**. We focus on two restrictions from @rambachan2023more: the relative magnitude (RM) restriction and the smoothness restriction, both of which connect pre-treatment PT violations to potential post-treatment counterfactual deviations. +Below, we illustrate how to apply this sensitivity analysis with **fect**. We focus on two restrictions from @rambachan2023more: the relative magnitude (RM) restriction and the smoothness restriction, both of which connect pre-treatment parallel trends violations to potential post-treatment counterfactual deviations. R script used in this chapter can be downloaded [here](https://raw.githubusercontent.com/xuyiqing/fect/dev/vignettes/rscript/09-sens.R). ## Install Packages To begin, you will need to install the necessary packages from CRAN and GitHub. -```{r message = FALSE, warning = FALSE} +```{r install-packages, message = FALSE, warning = FALSE} # install packages from CRAN packages <- c("dplyr", "panelView", "ggplot2") # Removed HonestDiD, doParallel install.packages(setdiff(packages, rownames(installed.packages()))) @@ -32,9 +34,8 @@ if ("HonestDiDFEct" %in% rownames(installed.packages()) == FALSE) { Load libraries: -```{r message = FALSE, warning = FALSE} +```{r load-libraries, message = FALSE, warning = FALSE} library(dplyr) -library(fect) library(panelView) library(ggplot2) library(HonestDiDFEct) # Required for fect_sens to work @@ -44,25 +45,32 @@ library(HonestDiDFEct) # Required for fect_sens to work We begin with an empirical example from @HH2019, who investigate the effects of indirect democracy versus direct democracy on naturalization rates in Switzerland using municipality-year panel data from 1991 to 2009. The study finds that switching from direct to indirect democracy increased naturalization rates by an average of 1.22 percentage points (Model 1, Table 1). -```{r message = FALSE, warning = FALSE} -data(fect) +```{r load-hh2019, message = FALSE, warning = FALSE} +data(hh2019) data <- hh2019 head(data) ``` ### Implement with Placebo Tests -To implement this method with the imputation estimator, we use the dynamic treatment effects from pre-treatment *placebo tests* to gauge PT violations and determine whether post-treatment effects remain significant under similar violations. This requires symmetric estimation of dynamic treatment effects in both pre- and post-treatment periods. As [@roth2024interpreting] notes, some estimators (e.g., CSDID without `base_period = "universal"`) may not produce symmetrical estimates. +To implement this method with the imputation estimator, we use the dynamic treatment effects from pre-treatment *placebo tests* to gauge parallel trends violations and determine whether post-treatment effects remain significant under similar violations. This requires symmetric estimation of dynamic treatment effects in both pre- and post-treatment periods. As [@roth2024interpreting] notes, some estimators (e.g., CSDID without `base_period = "universal"`) may not produce symmetrical estimates. Below, we designate placebo periods using **fect**. These placebo periods are excluded during model fitting, and counterfactuals are imputed for both placebo and post-treatment intervals to compute dynamic treatment effects, ensuring consistent estimation across all periods. -By setting `placeboTest = TRUE` and `placebo.period = c(-2, 0)`, we define three pre-treatment periods as placebo periods. Their dynamic treatment effects serve as the benchmark for PT violations in the post-treatment phase. In the code chunk below, we fit **fect** with these placebo settings, then use the `fect_sens` function to perform the sensitivity analysis. This function wraps the procedures from **HonestDiDFEct**, preparing the output for plotting. Note that not all of `Mbarvec`, `periodMbarvec`, `Mvec`, or `periodMvec` need to be specified; only the ones you want to use for the sensitivity analysis. +By setting `placeboTest = TRUE` and `placebo.period = c(-2, 0)`, we define three pre-treatment periods as placebo periods. Their dynamic treatment effects serve as the benchmark for parallel trends violations in the post-treatment phase. In the code chunk below, we fit **fect** with these placebo settings, then use the `fect_sens` function to perform the sensitivity analysis. This function wraps the procedures from **HonestDiDFEct**, preparing the output for plotting. Note that not all of `Mbarvec`, `periodMbarvec`, `Mvec`, or `periodMvec` need to be specified; only the ones you want to use for the sensitivity analysis. -```{r, hh_honest_placebo, warning=FALSE, message=FALSE, cache=TRUE} -out.fect.placebo <- fect(nat_rate_ord~indirect, data = hh2019, +```{r, hh_honest_placebo, warning=FALSE, message=FALSE, cache=FALSE} +out.fect.placebo <- fect(nat_rate_ord~indirect, data = hh2019, index = c("bfs","year"), - method = 'fe', se = TRUE, - placeboTest = TRUE, placebo.period = c(-2,0)) + method = 'fe', se = TRUE, + placeboTest = TRUE, placebo.period = c(-2,0), + parallel = TRUE, cores = 16) + +# Ensure att.vcov is a valid matrix (compute from bootstrap samples if needed) +if (!is.matrix(out.fect.placebo$att.vcov) && is.matrix(out.fect.placebo$att.boot)) { + out.fect.placebo$att.vcov <- cov(t(out.fect.placebo$att.boot), + use = "pairwise.complete.obs") +} # Define post-treatment periods and sensitivity parameters for fect_sens T.post <- 10 # Number of post-treatment periods based on original analysis @@ -85,13 +93,13 @@ out.fect.placebo <- fect_sens( periodMbarvec = Mbar_vec_period_rm, Mvec = M_vec_avg_smooth, periodMvec = M_vec_period_smooth, - parallel = TRUE # Set to TRUE for parallel processing if desired + parallel = FALSE # Set to TRUE for parallel processing if desired ) ``` ### RM Restriction -We first explore the Relative Magnitude (RM) restriction. Let $\delta$ represent potential PT violations for placebo and post-treatment periods. Unlike a standard event study that assumes $\delta_t=0$ for $t>0$, RM allows PT deviations as long as they do not exceed $\bar{M}$ times the maximum deviation between consecutive placebo periods. +We first explore the Relative Magnitude (RM) restriction. Let $\delta$ represent potential parallel trends violations for placebo and post-treatment periods. Unlike a standard event study that assumes $\delta_t=0$ for $t>0$, RM allows PT deviations as long as they do not exceed $\bar{M}$ times the maximum deviation between consecutive placebo periods. The **fect** package, through `fect_sens`, utilizes a forked version of **HonestDiD**, called **HonestDiDFEct**, which is adapted for **fect**'s output structure. The RM restriction is defined as: @@ -106,7 +114,7 @@ Here, $max(|\delta_{-1}-\delta_{-2}|,|\delta_0-\delta_{-1}|)$ matches the larges **Robust Confidence Set for the ATT** -We begin by constructing a robust confidence set for the overall ATT. The `fect_sens` function has already computed these results, using count-based weights for the ATT by default. We specified `Mbarvec = seq(0, 1, by=0.1)` in our call to `fect_sens`. Increasing $\bar{M}$ allows proportionally larger PT violations in the post-treatment window. When $\bar{M}=0$, the resulting confidence set behaves as a "de-biased" interval that corrects post-treatment estimates based on the observed PT violation at $t=0$. +We begin by constructing a robust confidence set for the overall ATT. The `fect_sens` function has already computed these results, using count-based weights for the ATT by default. We specified `Mbarvec = seq(0, 1, by=0.1)` in our call to `fect_sens`. Increasing $\bar{M}$ allows proportionally larger parallel trends violations in the post-treatment window. When $\bar{M}=0$, the resulting confidence set behaves as a "de-biased" interval that corrects post-treatment estimates based on the observed PT violation at $t=0$. We can now plot the robust confidence intervals for the ATT using the RM restriction with `plot()` and `type = "sens"`: @@ -117,7 +125,7 @@ plot(out.fect.placebo, main = "Relative Magnitude Restriction") ``` -If the robust confidence set excludes zero at $\bar{M}=0.4$ but includes zero at $\bar{M}=0.5$, we infer that post-treatment PT violations must be at least half of the maximum observed placebo violation to overturn the estimated effect. +If the robust confidence set excludes zero at $\bar{M}=0.4$ but includes zero at $\bar{M}=0.5$, we infer that post-treatment parallel trends violations must be at least half of the maximum observed placebo violation to overturn the estimated effect. **Period-by-Period Robust Confidence Set** @@ -134,7 +142,7 @@ plot(out.fect.placebo, show.count = TRUE) ``` -In the figure, different lines/bands represent the robust confidence intervals for $\bar{M}=0$ and $\bar{M}=0.5$. The interval for $\bar{M}=0$ treats the observed violation at $t=0$ as persisting into all post-treatment periods, whereas the interval for $\bar{M}=0.5$ allows added PT violations up to half of the largest placebo discrepancy. These are compared against the original confidence intervals. You can also change the colors using the `sens.colors` argument in the `plot()` function. This works for the regular `type = "sens"` plot as well, but with a vector of only one color. +In the figure, different lines/bands represent the robust confidence intervals for $\bar{M}=0$ and $\bar{M}=0.5$. The interval for $\bar{M}=0$ treats the observed violation at $t=0$ as persisting into all post-treatment periods, whereas the interval for $\bar{M}=0.5$ allows added parallel trends violations up to half of the largest placebo discrepancy. These are compared against the original confidence intervals. You can also change the colors using the `sens.colors` argument in the `plot()` function. This works for the regular `type = "sens"` plot as well, but with a vector of only one color. ```{r,hh_honest.placebo.honest.gap.plot.colors, fig.width=7, fig.height=5, cache=TRUE} plot(out.fect.placebo, @@ -150,7 +158,7 @@ plot(out.fect.placebo, ### Smoothness Restriction -A second approach to bounding PT violations is the **smoothness restriction**, which prevents the post-treatment violation from diverging too sharply from a linear extrapolation of the pre-trend. This restriction is particularly relevant if we suspect a gradually varying or near-linear trend in the potential violation. +A second approach to bounding parallel trends violations is the **smoothness restriction**, which prevents the post-treatment violation from diverging too sharply from a linear extrapolation of the pre-trend. This restriction is particularly relevant if we suspect a gradually varying or near-linear trend in the potential violation. Formally, one assumes $\delta\in\Delta^{SD}(M)$ where diff --git a/vignettes/_common.R b/vignettes/_common.R new file mode 100644 index 00000000..ce7d8c0b --- /dev/null +++ b/vignettes/_common.R @@ -0,0 +1,18 @@ +# Shared setup for all vignette chapters +# When rendering locally from the source tree, devtools::load_all() +# ensures the LATEST R functions (including new features like cm) and +# all datasets are available, even if the installed package is outdated. +# During R CMD check the package is freshly installed, so library() suffices. +if (file.exists("../DESCRIPTION") && + requireNamespace("devtools", quietly = TRUE)) { + tryCatch( + suppressMessages(devtools::load_all("..", quiet = TRUE)), + error = function(e) { + message("devtools::load_all() failed: ", conditionMessage(e)) + message("Falling back to library(fect)") + suppressMessages(library(fect)) + } + ) +} else { + suppressMessages(library(fect)) +} diff --git a/vignettes/_quarto.yml b/vignettes/_quarto.yml index 6a5c5abb..99d67234 100644 --- a/vignettes/_quarto.yml +++ b/vignettes/_quarto.yml @@ -10,32 +10,35 @@ book: - index.qmd - 01-start.Rmd - 02-fect.Rmd - - 03-plots.Rmd - - 04-gsynth.Rmd + - 03-ife-mc.Rmd + - 04-cfe.Rmd - 05-hte.Rmd - - 06-panel.Rmd - - 07-sens.Rmd + - 06-plots.Rmd + - 07-gsynth.Rmd + - 08-panel.Rmd + - 09-sens.Rmd - aa-cheatsheet.Rmd - references.qmd - -delete_merged_file: true + - bb-updates.Rmd bibliography: references.bib nocite: | @* +suppress-bibliography: true + format: html: theme: cosmo code-fold: false code-tools: true code-link: true + toc-depth: 3 knitr: - opts_chunk: + opts_chunk: collapse: true - comment: "#>" + comment: "#>" editor: visual - diff --git a/vignettes/aa-cheatsheet.Rmd b/vignettes/aa-cheatsheet.Rmd index a42cbdb1..ceeec962 100644 --- a/vignettes/aa-cheatsheet.Rmd +++ b/vignettes/aa-cheatsheet.Rmd @@ -11,7 +11,6 @@ In this chapter, we provide an overview of the main functionalities of the **fec | **fe** | Standard two-way fixed effects model | Liu, Wang & Xu (2022), Borusyak, Jaravel & Spiess (2024) | | **ife** | Interactive fixed effects model | Gobillon & Magnac (2016), Xu (2017) | | **mc** | Matrix completion method | Athey et al. (2021) | -| **polynomial** | Two-way fixed effects model with unit-specific trends | – | | **cfe** | "Complex" or multi-level fixed effects | – | | **gsynth** | Interactive fixed effects model | Xu (2017) | @@ -21,19 +20,19 @@ In this chapter, we provide an overview of the main functionalities of the **fec | **Type** | **Description** | **Applicable Methods** | |------------------------|------------------------|------------------------| -| **box** | Box plot of ATT by period. | fe, ife, mc, gsynth, polynomial, cfe | -| **calendar** | CATT by calendar time. | fe, ife, mc, gsynth, polynomial, cfe | -| **heterogenous** or **hte** | CATT by a covariate. | fe, ife, mc, gsynth, polynomial, cfe | +| **box** | Box plot of ATT by period. | fe, ife, mc, gsynth, cfe | +| **calendar** | CATT by calendar time. | fe, ife, mc, gsynth, cfe | +| **heterogeneous** or **hte** | CATT by a covariate. | fe, ife, mc, gsynth, cfe | | **counterfactual** or **ct** | Observed vs. imputed outcome for treated units. | fe, ife, mc, gsynth | -| **equiv** | Pretreatment residuals with equivalence intervals. | fe, ife, mc, gsynth, polynomial, cfe | -| **exit** | Period-wise ATT relative to treatment exit. | fe, ife, mc, polynomial, cfe | +| **equiv** | Pretreatment residuals with equivalence intervals. | fe, ife, mc, gsynth, cfe | +| **exit** | Period-wise ATT relative to treatment exit. | fe, ife, mc, cfe | | **factors** | Estimated factors (factor-based methods). | ife, gsynth | -| **gap** or **es** | Event-study plot: ATT by pre- and post-treatment periods. | fe, ife, mc, gsynth, polynomial, cfe | +| **gap** or **es** | Event-study plot: ATT by pre- and post-treatment periods. | fe, ife, mc, gsynth, cfe | | **loadings** | Estimated factor loadings (factor-based methods). | ife, gsynth | -| **status** | Treatment status by period for all units. | fe, ife, mc, gsynth, polynomial, cfe | -| **sens** | Rambachan & Roth (2023) sensitivity analysis for treatment effects. | `fect_sens()` function applied to fe, ife, mc, gsynth, polynomial, cfe | -| **sens_es** | Event-study sensitivity analysis for treatment effects. | `fect_sens()` function applied to fe, ife, mc, gsynth, polynomial, cfe | -| **cumul** | Cumulative treatment effects over time. | `effect()` function applied to fe, ife, mc, gsynth, polynomial, cfe | +| **status** | Treatment status by period for all units. | fe, ife, mc, gsynth, cfe | +| **sens** | Rambachan & Roth (2023) sensitivity analysis for treatment effects. | `fect_sens()` function applied to fe, ife, mc, gsynth, cfe | +| **sens_es** | Event-study sensitivity analysis for treatment effects. | `fect_sens()` function applied to fe, ife, mc, gsynth, cfe | +| **cumul** | Cumulative treatment effects over time. | `effect()` function applied to fe, ife, mc, gsynth, cfe | ## Tests as Options @@ -42,8 +41,8 @@ In this chapter, we provide an overview of the main functionalities of the **fec | Input | **Description** | **Applicable Methods** | |------------------------|------------------------|------------------------| | `loo` | Leave-one-period-out goodness-of-fit test (`TRUE/FALSE`). | fe, ife, mc, gsynth | -| `carryoverTest` | Tests for carryover effects in post-treatment periods (`TRUE/FALSE`). | fe, ife, mc | -| `carryover.period` | Range of post-treatment periods for the carryover test (vector). | fe, ife, mc | +| `carryoverTest` | Tests for carryover effects in post-treatment periods (`TRUE/FALSE`). | fe, ife, mc, cfe | +| `carryover.period` | Range of post-treatment periods for the carryover test (vector). | fe, ife, mc, cfe | | `permute` | Permutation test (`TRUE/FALSE`). | fe, ife, mc, gsynth | | `m` | Block length for permutation test (`m=2` by default). | fe, ife, mc, gsynth | | `CV` | Cross-validation for factor-based methods. | ife, mc, gsynth | @@ -54,48 +53,61 @@ A check mark (✓) indicates that the method requires or accepts the input. ### Required Inputs -| **Input** | **fe** | **ife** | **mc** | **gsynth** | **polynomial** | cfe | -|-----------|:---------:|:---------:|:---------:|:---------:|:---------:|:---------:| -| `Y` (outcome) | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | -| `D` (treatment) | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | -| `X` (covariates) | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | -| `data` | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | -| `index` (unit & time IDs) | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | -| `sfe` (simple additive FEs) | – | – | – | – | – | ✓ | -| `cfe` (complex FEs) | – | – | – | – | – | ✓ | +| **Input** | **fe** | **ife** | **mc** | **gsynth** | **cfe** | +|-----------|:---------:|:---------:|:---------:|:---------:|:---------:| +| `Y` (outcome) | ✓ | ✓ | ✓ | ✓ | ✓ | +| `D` (treatment) | ✓ | ✓ | ✓ | ✓ | ✓ | +| `X` (covariates) | ✓ | ✓ | ✓ | ✓ | ✓ | +| `data` | ✓ | ✓ | ✓ | ✓ | ✓ | +| `index` (unit & time IDs) | ✓ | ✓ | ✓ | ✓ | ✓ | +| `sfe` (simple additive FEs) | – | – | – | – | ✓ | +| `cfe` (complex FEs) | – | – | – | – | ✓ | +| `Z` (time-invariant covariates) | – | – | – | – | ✓ | +| `Q` (known time trends) | – | – | – | – | ✓ | +| `Q.type` (auto time trends) | – | – | – | – | ✓ | ### Optional Inputs -| **Input** | **fe** | **ife** | **mc** | **gsynth** | **polynomial** | **cfe** | -|:----------|:----------|:---------:|:---------:|:---------:|:---------:|:---------:| -| `W` (weight) | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | -| `se` (uncertainty) | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | -| `nboots` (# bootstrap reps) | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | -| `force` (FE structure) | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | -| `lambda`(Hyper-parameter sequence) | – | – | ✓ | ✓ | – | – | -| `nlambda`(Length of hyper-parameter sequence) | – | – | ✓ | ✓ | – | – | -| `r` (# factor) | – | ✓ | – | ✓ | – | – | -| `k` ( \# cross-validation rounds) | – | ✓ | ✓ | ✓ | – | – | -| `degree` (degree of the interacted fixed effects) | – | – | – | – | ✓ | – | +| **Input** | **fe** | **ife** | **mc** | **gsynth** | **cfe** | +|:----------|:----------|:---------:|:---------:|:---------:|:---------:| +| `W` (weight) | ✓ | ✓ | ✓ | ✓ | ✓ | +| `time.component.from` (time component source) | – | ✓ | ✓ | ✓ | ✓ | +| `em` (EM algorithm) | – | ✓ | – | – | ✓ | +| `se` (uncertainty) | ✓ | ✓ | ✓ | ✓ | ✓ | +| `nboots` (# bootstrap reps) | ✓ | ✓ | ✓ | ✓ | ✓ | +| `force` (FE structure) | ✓ | ✓ | ✓ | ✓ | ✓ | +| `lambda` (Hyper-parameter sequence) | – | – | ✓ | ✓ | – | +| `nlambda` (Length of hyper-parameter sequence) | – | – | ✓ | ✓ | – | +| `r` (# factor) | – | ✓ | – | ✓ | ✓ | +| `k` ( \# cross-validation rounds) | – | ✓ | ✓ | ✓ | – | +| `gamma` (time grouping for Z) | – | – | – | – | ✓ | +| `kappa` (unit grouping for Q) | – | – | – | – | ✓ | +| `Z.param` (Z block assignment) | – | – | – | – | ✓ | +| `Q.param` (Q block assignment) | – | – | – | – | ✓ | +| `Q.bspline.degree` (B-spline degree) | – | – | – | – | ✓ | ### Important Options | **Input** | **Options** | **Applicable Methods** | |:-----------------------|:-----------------------|:-----------------------| -| `force` | Fixed effects structure: "none", "unit", "time", "two-way" (default) | fe, ife, mc, poly, cfe, gsynth | +| `force` | Fixed effects structure: "none", "unit", "time", "two-way" (default) | fe, ife, mc, cfe, gsynth | +| `time.component.from` | Time component source: "notyettreated" (default), "nevertreated". Controls which units provide the time-varying model components (time fixed effects, latent factors, temporal dynamics). When "nevertreated", only never-treated controls estimate these components. `method = "gsynth"` auto-sets to "nevertreated". | ife, mc, cfe, gsynth | +| `em` | EM algorithm: `TRUE` (default), `FALSE`. Required `TRUE` for "notyettreated"; auto-set to `FALSE` by `method = "gsynth"`. | ife, cfe | | `vartype` (when `method = "gsynth"`) | Uncertainty estimator: "parametric" (default), "bootstrap", "jackknife" | gsynth | -| `vartype` (otherwise) | Uncertainty estimator: "bootstrap" (default), "jackknife" | fe, ife, mc, polynomial, cfe | -| `criterion` | Model selection criterion: "mspe" (default), "gmspe", "moment", "pc". | ife, mc, polynomial, cfe, gsynth | +| `vartype` (otherwise) | Uncertainty estimator: "bootstrap" (default), "jackknife" | fe, ife, mc, cfe | +| `criterion` | Model selection criterion: "mspe" (default), "gmspe", "moment", "pc". | ife, mc, cfe, gsynth | ## Notes - Most methods share core inputs (`Y`, `D`, `X`, `data`, `index`). -- Method `polynomial` and `cfe` allow either "jackknife" or "bootstrap" for uncertainty estimates. +- Method `cfe` allows either "jackknife" or "bootstrap" for uncertainty estimates. -- Factor-based methods (`ife`, `gsynth`) can use `r`and `k` for factor selection and cross validation. We can implement a similar process using `mc` or `gsynth` with `lambda` and`nlambda`. +- Factor-based methods (`ife`, `gsynth`) can use `r` and `k` for factor selection and cross-validation. We can implement a similar process using `mc` or `gsynth` with `lambda` and `nlambda`. -- `carryoverTest` and `loo` are available in `fe`, `ife`, `mc`, `polynomial`, and `cfe` but not in `gsynth`. +- `carryoverTest` is available in `fe`, `ife`, `mc`, and `cfe` but not in `gsynth`. `loo` is available in `fe`, `ife`, `mc`, and `gsynth`. + +- The **cfe** method supports additional parameters: `Z`/`gamma` for time-invariant covariates with time-varying coefficients, `Q`/`kappa` (or `Q.type`) for unit-specific time trends, extra additive fixed effects via `index[3:]`, and interactive fixed effects via `r`. See [Chapter @sec-cfe] for details. - `force` allows various levels of fixed effects (`"none"`, `"unit"`, `"time"`, `"two-way"`). @@ -103,7 +115,6 @@ A check mark (✓) indicates that the method requires or accepts the input. | **Inputs** | **Description** | **Default** | |:--:|:---|:--:| -| `degree` | The degree of the interacted fixed effects(int) | 2 | | `nboots` | Number of bootstrap runs (int) | 200 | | `k` | Sets the number of cross-validation rounds(int) | 10 | | `r` | Sets the number of factors | 0; c(0,5) when CV | @@ -112,4 +123,16 @@ A check mark (✓) indicates that the method requires or accepts the input. - With an integer input for `nlambda`, **fect** can randomly generate appropriate hyper-parameter sequence. +## Utility Functions + +In addition to the main `fect()` function, the package provides several utility functions: + +| **Function** | **Description** | **Key Arguments** | +|:-------------|:----------------|:------------------| +| `esplot()` | Standalone event study plot from a data frame. Does not require a `fect` object. | `data`, `Period`, `Estimate`, `CI.lower`, `CI.upper`, `connected`, `highlight.periods` | +| `fect_mspe()` | Model comparison via cross-validated prediction error. | `out.fect`, `seed`, `cv.method`, `criterion`, `k`, `cv.prop` | +| `get.cohort()` | Generate cohort indicators based on treatment timing. | `data`, `D`, `index`, `entry.time`, `start0` | +| `att.cumu()` | Compute cumulative treatment effects over a specified period range. | `x` (fect object), `period`, `weighted`, `type` | +| `effect()` | Compute cumulative or subgroup-averaged treatment effects. Requires `keep.sims = TRUE` in `fect()`. | `x` (fect object), `cumu`, `id`, `period`, `plot` | + Enjoy using **fect**! diff --git a/vignettes/bb-updates.Rmd b/vignettes/bb-updates.Rmd new file mode 100644 index 00000000..54f273f3 --- /dev/null +++ b/vignettes/bb-updates.Rmd @@ -0,0 +1,88 @@ +# Changelog {#sec-changelog .unnumbered} + +## v2.2.0 + +* Added `time.component.from` parameter: `"notyettreated"` (default) or `"nevertreated"` controls which units provide the time-varying model components (time fixed effects, latent factors, and temporal dynamics). Replaces `method = "gsynth"` with `method = "ife", time.component.from = "nevertreated"`. +* CFE estimator now supports `time.component.from = "nevertreated"`, enabling full CFE model components (additional FEs, $Z/\gamma$, $Q/\kappa$, latent factors) with never-treated estimation. +* Added `fect_mspe()` for out-of-sample model comparison (MSPE, RMSE, MAD) across specifications. +* Unified cross-validation: replaced `cv.treat` + `mask.method` with single `cv.method` parameter; added `cv.sample` k-fold CV for `fect_nevertreated`. +* Improved EM convergence conditioning: R-level centering + C++ component-wise convergence (up to 2000x better component accuracy). +* Removed deprecated methods: `polynomial`, `bspline`, `cfe_old`. Renamed internal `fect_gsynth` to `fect_nevertreated`. +* Plot system overhaul: pre/post color distinction, highlight shapes (triangles for placebo, diamonds for carryover), new `esplot()` parameters (`xangle`, `yangle`, `xbreaks`, `ybreaks`, `legendOff`). +* Fixed parallel bootstrap `.export` lists for CFE + nevertreated; fixed `fect_mspe` + `CV=TRUE` interaction; fixed `start0` coloring; fixed `.as_mask()` dimension bug. +* Renamed datasets for clarity: `simdata1` → `sim_base`, `simdata2` → `sim_trend`, `simgsynth` → `sim_gsynth`. Added `sim_region` and `sim_linear`. `simdata` kept for backward compatibility. +* Fixed ggplot2 deprecation warnings: `size` → `linewidth` in all `geom_hline`, `geom_line`, and bound line aesthetics. +* Fixed `GGally::ggpairs` loadings plot warnings by applying color/fill scales inside sub-panel functions. +* Exit plot legends now read "Under treatment" / "Out of treatment" instead of "Pre-treatment" / "Post-treatment". +* CRAN compliance: resolved codoc mismatches in `esplot.Rd`, `fect_mspe.Rd`, `plot.fect.Rd`; removed duplicate data source (`fect.RData`); added `globalVariables` declarations. +* 590 tests (up from 131), including 98 book-derived behavioral tests and 36 CFE bifurcation tests. + +## v2.1.1 + +* Added `fect_mspe()` for evaluating out-of-sample prediction accuracy across model specifications. +* Optimized CFE estimator performance: memory recycling, sparse data handling, reduced C++ deep copies. +* Fixed `sigma2` normalization crash and division-by-zero guards in bootstrap. +* Fixed bootstrap NA and multicollinearity abort issues. +* Fixed gsynth weight calculation and `r=0` grand mean issue. +* Fixed `Q.type` routing in CFE and handling of time periods with all units under control. +* Switched parallel backend from `future` to `parallelly`. +* Added CFE tutorial chapter to Quarto book. +* Added `codetools` and `tail` imports required by new functions. + +## v2.1.0 + +(2025-10-27) + +* Added CFE (Complex Fixed Effects) estimator as `method = "cfe"`: additional group FEs via `index`, time-invariant covariates with time-varying coefficients (`Z`/`gamma`), unit-specific loadings on known time bases (`Q.type`/`Q`/`kappa`), and latent factors (`r`). +* Added B-spline option to `Q.type` for flexible nonlinear time trend approximation. +* Added `return.data = TRUE` option to `plot()` for extracting plot data. +* Added `doRNG` for reproducible parallel bootstrap results. +* Switched parallel backend from `future` to `parallelly`. +* Aligned gsynth bootstrap values and standard errors with the original gsynth package. +* Reduced memory usage by removing unnecessary C++ deep copies. +* Added `min.T0` restrictions to prevent estimation with too few control observations. +* Fixed various R CMD check issues for CRAN compliance. + +## v2.0.5 + +(2025-08-25) CRAN release. + +* Fixed various bugs due to changes in dependencies +* Added limit to `cores` by default in parallel computing +* Add new plot `type = "hte"` +* Added R-CMD-check for merging + +## v2.0.2 + +(2025-05-07) + +* Fixed various bugs +* Updated counterfactual plot +* Added sensitivity analysis +* Added `did_wrapper()` and `esplot()` +* Added various plotting options +* Improved documentation + +## v2.0.0 + +(2025-01-17) CRAN release. + +* Changed to new syntax +* Fixed various bugs +* Added `get_cohort()` +* Merged in **gsynth** + +## v1.0.0 + +* First CRAN version +* Fixed bugs + +## v0.6.5 + +* Replace fastplm with fixest for fixed effects estimation +* Added plots for heterogeneous treatment effects +* Fixed bugs + +## v0.4.1 + +* Added a `NEWS.md` file to track changes to the package. diff --git a/vignettes/index.qmd b/vignettes/index.qmd index ba556bf7..ca4dc3bb 100644 --- a/vignettes/index.qmd +++ b/vignettes/index.qmd @@ -1,6 +1,6 @@ # Welcome! {.unnumbered} -This Quarto book serves as a user manual for the **fect** package in R, which implements counterfactual (imputation) estimators for causal inference with panel data and performs diagnostic tests. +This Quarto book serves as a user manual for the **fect** package in R, which implements counterfactual (imputation) estimators for causal inference with panel data---without feedback---and performs diagnostic tests. **fect** covers a series of counterfactual estimators, including the five estimators from the last version and integrating the latest version of the **gsynth** package for the generalized synthetic control method (Gsynth). This Quarto book also facilitates the application of various new difference-in-differences (DID) estimators. For details of these methods, see @@ -28,53 +28,89 @@ There are several reasons why you might consider using counterfactual estimators However, these counterfactual estimators come with important limitations: - Most rely on some form of the parallel trends assumption or a low-rank structure.\ -- They generally do not accommodate dynamic treatment assignment based on sequential ignorability.\ +- They generally do not accommodate dynamic treatment assignment given past outcomes or covariates---i.e., "feedback"---based on sequential ignorability.\ - Methods for continuous treatments are still underdeveloped and are not currently covered by **fect**. @CLLX2025 reanalyze 49 published studies in political science and offer justifications for adopting these estimators. ## Why the Merge? -I have decided to merge the two packages, **gsynth** and **fect**, as **gsynth** is fundamentally a counterfactual estimator. The two packages increasingly share similar code modules and features, including core algorithms (the `ife` and `mc` methods in both packages are essentially identical), tuning methods, and visualization tools. This merge will greatly simplify package maintenance moving forward. +I have decided to merge the two packages, **gsynth** and **fect**, as **gsynth** is fundamentally a counterfactual estimator based on `ife`. The two packages increasingly share similar code modules and features, including core algorithms (the `ife` and `mc` methods in both packages are essentially identical), tuning methods, and visualization tools. This merge will greatly simplify package maintenance moving forward. -However, some key differences between the two approaches remain: +Moving forward, I will discontinue maintaining **gsynth** and focus on adding more functionalities to **fect**. -- Gsynth is specifically designed for block and staggered DID settings *without* treatment reversal, while other methods accommodate treatment reversal under the assumption of limited carryover effects. -- Gsynth is particularly suited for cases where the number of treated units is small, including scenarios with only one treated unit. By setting `vartype = "parametric"`, we can use a two-stage parametric bootstrapping procedure to produce uncertainty estimates. In contrast, other methods rely on large samples, particularly a large number of treated units, to obtain reliable standard errors and confidence intervals using `"bootstrap"` or `"jackknife"`. -- Compared with IFEct (`method = "ife"`), Gsynth does not rely on pre-treatment data from the treated units to impute $\hat{Y}(0)$. This approach significantly speeds up computation and improves stability. +## Two Research Settings -Therefore, we recommend setting `method = "gsynth"` in **fect** for scenarios where the treatment does not reverse (or is coded accordingly) and the number of treated units is small . +The differences between **gsynth** and **fect** reflect a broader distinction in panel data methods. We separate two research settings. -Moving forward, I will discontinue maintaining **gsynth** and focus on adding more functionalities to **fect**. +The **Synth** (synthetic control) setting learns time components from never-treated units, optionally via dimension reduction (e.g., factor models), and projects counterfactuals for treated units through vertical regression. This is the setting of **gsynth** [@Xu2017]. Designed for block or staggered adoption without treatment reversal, it is well suited for a small number of treated units (including $N_{tr} = 1$). The estimand is SATT. + +The **DID/TWFE** setting imputes treated counterfactuals using all not-yet-treated observations, including both never-treated units and pre-treatment periods of treated units. This accommodates treatment reversal and uses more data. The estimand is PATT. + +Inference differs across the two settings. The Synth setting conditions on fixed pre-treatment information $X$ and uses predictive uncertainty, implemented via a two-stage parametric bootstrap (`vartype = "parametric"`). The DID/TWFE setting relies on super-population inference, for which nonparametric bootstrap (`vartype = "bootstrap"`) or jackknife (`vartype = "jackknife"`) are appropriate. + +### Estimation and Inference + +Starting from `v2.2.0`, `time.component.from` determines which setting governs estimation. It specifies how time components, such as time fixed effects, latent factors, and temporal dynamics, are learned from the data. The table summarizes compatibility. + +| Method | Description | `time.component.from` | +|:------------------|:-----------------------|:-----------------------------| +| `"fe"` | Two-way fixed effects ($r = 0$) | Both | +| `"ife"` | Interactive fixed effects ($r \geq 0$) | Both | +| `"cfe"` | Complex fixed effects | Both | +| `"mc"` | Matrix completion | `"notyettreated"` only | +| `"gsynth"` | Equivalent to `"ife"` with `time.component.from = "nevertreated"` | `"nevertreated"` | + +The two values correspond to the two settings. `"notyettreated"` (default) uses all not-yet-treated observations to learn components. `"nevertreated"` uses only never-treated controls. + +### How Should I Choose? + +Choose based on estimand and inference. If the target is unit-specific and conditions on $X$, use the Synth setting. If the target is a population parameter, use the DID framework. The table below gives recommendations based on feasibility in common scenarios. + +| Scenario | Recommended Settings | +|:----------------------|:-----------------------------------------------| +| Treatment switches on and off | `time.component.from = "notyettreated"` (default) | +| No reversal, many treated units | Either setting | +| No reversal, few treated units | `time.component.from = "nevertreated"`, `vartype = "parametric"` | +| Reproduce **gsynth** | `method = "gsynth"` or `method = "ife"`, `time.component.from = "nevertreated"` | ## Organization The user guide is structured into the following chapters: - [Chapter @sec-start]\ - This chapter covers installation instructions and introduces the datasets. + Installation instructions and datasets. - [Chapter @sec-fect]\ - This chapter explains how to apply the five estimators and diagnostic tests available in previous versions of **fect**. + Also known as the fixed effects counterfactual estimator (FEct), including estimation, inference, alternative estimands. + +- [Chapter @sec-ife-mc]\ + Interactive fixed effects (IFE) and matrix completion (MC) methods, cross-validation, and diagnostic tests. + +- [Chapter @sec-cfe]\ + The complex fixed effects (CFE) estimator: multi-level fixed effects, time-invariant covariates with time-varying coefficients, unit-specific time trends, in addition to interactive fixed effects. + +- [Chapter @sec-hte]\ + Effect heterogeneity: box plots, calendar-time trends, and covariate-based HTE. Triple difference-in-differences designs (in development). - [Chapter @sec-plots]\ - In this chapter, we explore various plotting options available in **fect**. + Details of the plotting options and customization. - [Chapter @sec-gsynth]\ - This chapter provides a step-by-step guide to implementing all the functionalities of the original **gsynth** R package using **fect**. + The Gsynth program — the synthetic control setting using `time.component.from = "nevertreated"` — originally developed in the **gsynth** package, with CFE extensions. - [Chapter @sec-panel]\ - This chapter facilitates the application of various new DID estimators. + Application of various "modern" DID estimators. - [Chapter @sec-panel-sens]\ - This chapter introduces the sensitivity analysis for the counterfactual estimators. + Sensitivity analysis for the counterfactual estimators. - [Chapter @sec-cheatsheet]\ - The final chapter summarizes the core inputs required for implementing the six methods, along with options for plotting and diagnostics. + Quick reference for methods, parameters, plotting, and diagnostics. ## Contributors -The following individuals have contributed to **gsynth** and **fect**, listed in the order of their involvement in the project: +The following individuals (and AI) have contributed to **gsynth** and **fect**, listed in the order of their involvement in the project: - [Yiqing Xu](https://yiqingxu.org/)\ - [Licheng Liu](https://liulch.github.io/)\ @@ -84,47 +120,7 @@ The following individuals have contributed to **gsynth** and **fect**, listed in - [Tianzhu Qin](https://tianzhuqin.github.io/) (PhD Student at Cambridge University) - Jinwen Wu (Predoc at Stanford PoliSci) - [Rivka Lipkovitz](https://rivka.me/) (Undergraduate at MIT) - -Special thanks to Ziyi, Tianzhu, and Rivka for their tireless efforts in improving this package. Thanks to Jinwen for setting up and maintaining this User Manual. - -## Update Log - -### v2.1.0 - -(2025-10-27) - -- Fix speed issue -- Rewrite complex fixed effect handling -- Fixed various bugs - -### v2.0.5 - -(2025-08-25) CRAN release. - -- Fixed various bugs due to changes in dependencies -- Added limit to `cores` by default in parallel computing -- Add new plot `type = "hte"` -- Added R-CMD-check for merging - -### v2.0.2 - -(2025-05-07) - -- Fixed various bugs -- Updated counterfactual plot -- Added sensitivity analysis -- Added `did_wrapper()` and `esplot()` -- Added various plotting options -- Improved documentation - -### v2.0.0 - -(2025-01-17) CRAN release. - -- Changed to new syntax -- Fixed various bugs -- Added `get_cohort()` -- Merged in **gsynth** +- [StatsClaw](https://github.com/xuyiqing/StatsClaw) (Agentic System for Statistical Software Development) ## Report Bugs @@ -142,7 +138,4 @@ Please report any bugs by submitting an issue on [GitHub](https://github.com/xuy **gsynth** (retiring): [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [CRAN status](https://CRAN.R-project.org/package=gsynth) [downloads: CRAN](https://cran.r-project.org/web/packages/gsynth/index.html) -**panelView**: [![Lifecycle: stable](https://lifecycle.r-lib.org/articles/figures/lifecycle-stable.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [CRAN status](https://CRAN.R-project.org/package=panelView) [downloads: CRAN](https://cran.r-project.org/package=panelView) - - diff --git a/vignettes/references.bib b/vignettes/references.bib index 25613360..16f011fc 100644 --- a/vignettes/references.bib +++ b/vignettes/references.bib @@ -244,109 +244,6 @@ @ARTICLE{Gobillon2016 year = 2016 } -@article{ZQX2025, - title={Revisiting Triple-Difference Designs in Political Science}, - author={Zhang, Jingyu and Qin, Tianzhu and Xu, Yiqing}, - journal={Working Paper}, - year={2025} -} - -@book{Pearl2009, - title={Causality: Models, Reasoning, and Inference}, - author={Pearl, Judea}, - year={2009}, - edition={2nd}, - publisher={Cambridge University Press} -} - -@article{vanderweele2009, - title={On the Distinction between Interaction and Effect Modification}, - author={VanderWeele, Tyler J}, - journal={Epidemiology}, - volume={20}, - number={6}, - pages={863--871}, - year={2009} -} - -@article{bansak2021, - title={Using Conjoint Experiments to Analyze Election Outcomes: The Essential Role of the Average Marginal Component Effect}, - author={Bansak, Kirk and Hainmueller, Jens and Hopkins, Daniel J and Yamamoto, Teppei}, - journal={Political Analysis}, - volume={29}, - number={3}, - pages={366--380}, - year={2021} -} - -@article{xu2024factorial, - title={Causal Inference with Panel Experiments: Factorial Designs and Effect Modification}, - author={Xu, Yiqing}, - journal={Working Paper, Stanford University}, - year={2024} -} - -@article{HMX2019, - title={How Much Should We Trust Estimates from Multiplicative Interaction Models? Simple Tools to Improve Empirical Practice}, - author={Hainmueller, Jens and Mummolo, Jonathan and Xu, Yiqing}, - journal={Political Analysis}, - volume={27}, - number={2}, - pages={163--192}, - year={2019} -} - -@article{imai2019, - title={When Should We Use Unit Fixed Effects Regression Models for Causal Inference with Longitudinal Data?}, - author={Imai, Kosuke and Kim, In Song}, - journal={American Journal of Political Science}, - volume={63}, - number={2}, - pages={467--490}, - year={2019} -} - -@article{liu2024, - title={A Practical Guide to Counterfactual Estimators for Causal Inference with Time-Series Cross-Sectional Data}, - author={Liu, Licheng and Wang, Ye and Xu, Yiqing}, - journal={American Journal of Political Science}, - volume={68}, - number={1}, - pages={160--176}, - year={2024} -} - -@article{BCG2006, - title={The Dangers of Extreme Counterfactuals}, - author={Brambor, Thomas and Clark, William Roberts and Golder, Matt}, - journal={Political Analysis}, - volume={14}, - number={1}, - pages={73--94}, - year={2006} -} - -@article{strezhnev2023, - title={Decomposing Triple-Differences}, - author={Strezhnev, Anton}, - journal={Working Paper}, - year={2023} -} - -@article{caron2025, - title={Triple Differences with Heterogeneous Treatment Effects}, - author={Caron, Justin}, - journal={Working Paper}, - year={2025} -} - -@article{ortiz2025, - title={Difference-in-Difference-in-Differences}, - author={Ortiz, Luis E.}, - journal={Working Paper}, - year={2025} -} - @article{abadie2008failure, title={On the failure of the bootstrap for matching estimators}, author={Abadie, Alberto and Imbens, Guido W}, diff --git a/vignettes/references.qmd b/vignettes/references.qmd index 925f7c49..a0ca0e84 100644 --- a/vignettes/references.qmd +++ b/vignettes/references.qmd @@ -1,3 +1,7 @@ +--- +suppress-bibliography: false +--- + # References {.unnumbered} ::: {#refs} diff --git a/vignettes/rscript/02-fect.R b/vignettes/rscript/02-fect.R new file mode 100644 index 00000000..b49e4d23 --- /dev/null +++ b/vignettes/rscript/02-fect.R @@ -0,0 +1,188 @@ +## ----.common, include = FALSE------------------------------------------------- +source("_common.R") + + +## ----setup-seed, echo = FALSE------------------------------------------------- +set.seed(1234) + + +## ----load-packages, message = FALSE, warning = FALSE-------------------------- +data(sim_base) +data(sim_gsynth) + + +## ----panelview-treatment, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +library(panelView) +panelview(Y ~ D, data = sim_base, index = c("id","time"), + axis.lab = "time", xlab = "Time", ylab = "Unit", + gridOff = TRUE, by.timing = TRUE, + background = "white", main = "Simulated Data: Treatment Status") + + +## ----panelview-outcome, fig.width = 6, fig.height = 4.5, warning = FALSE------ +panelview(Y ~ D, data = sim_base, index = c("id","time"), + axis.lab = "time", xlab = "Time", ylab = "Unit", + theme.bw = TRUE, type = "outcome", + main = "Simulated Data: Outcome") + + +## ----simdata_fect_nose, eval=TRUE, cache = TRUE, message = FALSE, results = 'hide'---- +out.fect <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id","time"), + method = "fe", force = "two-way") + + +## ----fect_plot_nose, fig.width = 6, fig.height = 4.5-------------------------- +plot(out.fect, main = "Estimated ATT (FEct)", ylab = "Effect of D on Y", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) + + +## ----simdata_fect, eval=TRUE, cache = TRUE, message = FALSE, results = 'hide'---- +out.fect <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id","time"), + method = "fe", force = "two-way", se = TRUE, + parallel = TRUE, cores = 16, nboots = 1000) + + +## ----fect_plot_nse, fig.width = 6, fig.height = 4.5--------------------------- +plot(out.fect, main = "Estimated ATT (FEct)", ylab = "Effect of D on Y", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8, stats = "F.p") + + +## ----exit_fect, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +plot(out.fect, type = "exit", main = "Exit Plot (FEct)") + + +## ----print-fect--------------------------------------------------------------- +print(out.fect) + + +## ----extract-estimates, eval = FALSE------------------------------------------ +# out.fect$est.att +# out.fect$est.avg +# out.fect$beta + + +## ----extract-bootstrap, eval = FALSE------------------------------------------ +# out.fect$eff.boot + + +## ----fect_placebo, eval=TRUE, cache=TRUE, message=FALSE, results='hide', fig.width=6, fig.height=4.5---- +out.fect.placebo <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id","time"), + force = "two-way", method = "fe", + se = TRUE, nboots = 1000, parallel = TRUE, cores = 16, + placeboTest = TRUE, placebo.period = c(-2, 0)) +plot(out.fect.placebo, cex.text = 0.8) + + +## ----fect_carryover, eval=TRUE, cache=TRUE, message=FALSE, results='hide'----- +out.fect.carry <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id","time"), + force = "two-way", method = "fe", + se = TRUE, nboots = 1000, parallel = TRUE, cores = 16, + carryoverTest = TRUE, carryover.period = c(1, 3)) + + +## ----fect_carryover_plot, eval=TRUE, cache=TRUE, warning=FALSE, fig.width=6, fig.height=5---- +plot(out.fect.carry, type = "exit", cex.text = 0.8, main = "Carryover Effects (FEct)") + + +## ----fect_loo, eval=TRUE, cache = TRUE, message = FALSE----------------------- +out.fect.loo <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id","time"), + method = "fe", force = "two-way", se = TRUE, loo = TRUE, + parallel = TRUE, cores = 16, nboots = 1000) + + +## ----plot-gap-loo, fig.width = 6, fig.height = 4.5---------------------------- +plot(out.fect.loo,main = "Estimated ATT (FEct) -- LOO", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) + + +## ----cumu_effect, cache = TRUE------------------------------------------------ +out <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), + method = "ife", time.component.from = "nevertreated", + force = "two-way", CV = TRUE, r = c(0, 5), + se = TRUE, nboots = 1000, vartype = 'bootstrap', + parallel = TRUE, cores = 16, keep.sims=TRUE) +cumu.out <- effect(out) + + +## ----cumu_effect_plot, cache = TRUE------------------------------------------- +print(cumu.out) +plot(cumu.out) + + +## ----cumu_effect_byperiod, cache = TRUE--------------------------------------- +effect(out, cumu=FALSE) + + +## ----cumu_effect_subset, cache = TRUE----------------------------------------- +effect(out, cumu=TRUE, id=c(101,102,103), period=c(1,5)) + + +## ----effect-mc, cache = TRUE-------------------------------------------------- +out_mc <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), + method = "mc", force = "two-way", CV = TRUE, r = c(0, 5), + se = TRUE, nboots = 1000, vartype = 'bootstrap', + parallel = TRUE, cores = 16, keep.sims=TRUE) +plot(effect(out_mc)) + + +## ----effect-jackknife, cache = TRUE------------------------------------------- +out_jack <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), + method = "mc", force = "two-way", CV = TRUE, r = c(0, 5), + se = TRUE, nboots = 1000, vartype = 'jackknife', + parallel = TRUE, cores = 16, keep.sims=TRUE) +plot(effect(out_jack)) + + +## ----simdata_bal, eval=TRUE, cache = TRUE------------------------------------- +out.bal <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id","time"), + balance.period = c(-3, 4), force = "two-way", method = "ife", + CV = FALSE, r = 2, se = TRUE, nboots = 1000, parallel = TRUE, cores = 16) + + +## ----plot-balanced-att, fig.width = 6, fig.height = 4.5----------------------- +plot(out.bal, main = "Estimated ATT (Balanced Sample)") + + +## ----plot-balanced-custom, fig.width = 6, fig.height = 4.5-------------------- +plot(out.bal, main = "Estimated ATT (Balanced Sample)", + post.color = "red", count.color = "blue") + + +## ----simdata_panelview_cohort, fig.width = 6, fig.height = 4.5, warning = FALSE---- +panelview(Y ~ D, data = sim_base, index = c("id","time"), by.timing = TRUE, + axis.lab = "time", xlab = "Time", ylab = "Unit", + background = "white", main = "Simulated Data: Treatment Status") + + +## ----get_cohort--------------------------------------------------------------- +sim_base.cohort <- get.cohort(data = sim_base,D = 'D',index = c("id","time")) +print(table(sim_base.cohort[,'Cohort'])) + + +## ----get_cohort2-------------------------------------------------------------- +sim_base.cohort2 <- get.cohort(data = sim_base,D = 'D',index = c("id","time"), + entry.time = list(c(21,27),c(30,33))) +print(table(sim_base.cohort2[,'Cohort'])) + + +## ----simdata_fe_cohort, eval = TRUE, cache = TRUE, message = FALSE, results='hide'---- +out.fe.g <- fect(Y ~ D + X1 + X2, data = sim_base.cohort, index = c("id","time"), + force = "two-way", method = "fe", + se = TRUE, nboots = 1000, parallel = TRUE, cores = 16, group = 'Cohort') + + +## ----cohort_plot1, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +plot(out.fe.g, show.group = "Cohort:22", + xlim = c(-15, 10), ylim = c(-10, 10)) + + +## ----simdata_w, eval=TRUE, cache = TRUE--------------------------------------- +sim_base$Weight <- abs(rnorm(n = dim(sim_base)[1])) +out.w <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id","time"), + force = "two-way", method = "ife", W = 'Weight', + CV = FALSE, r = 2, se = TRUE, nboots = 1000, parallel = TRUE, cores = 16) + + +## ----plot-weighted-att, fig.width = 6, fig.height = 4.5----------------------- +plot(out.w, main = "Estimated Weighted ATT") + diff --git a/vignettes/rscript/03-ife-mc.R b/vignettes/rscript/03-ife-mc.R new file mode 100644 index 00000000..f7e20f5e --- /dev/null +++ b/vignettes/rscript/03-ife-mc.R @@ -0,0 +1,141 @@ +## ----.common, include = FALSE------------------------------------------------- +source("_common.R") + + +## ----setup-ife-mc, echo = FALSE, message = FALSE, warning = FALSE------------- +set.seed(1234) +data(simdata) + + +## ----simdata_ife, eval=TRUE, cache = TRUE------------------------------------- +out.ife <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + force = "two-way", method = "ife", CV = TRUE, r = c(0, 5), + se = TRUE, nboots = 200, parallel = TRUE, cores = 16) +print(out.ife) + + +## ----plot-att-ife, fig.width = 6, fig.height = 4.5--------------------------- +plot(out.ife, main = "Estimated ATT (IFEct)") + + +## ----simdata_mc, eval=TRUE, cache = TRUE-------------------------------------- +out.mc <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + force = "two-way", method = "mc", CV = TRUE, + se = TRUE, nboots = 200, parallel = TRUE, cores = 16) + +print(out.mc) + + +## ----plot-att-mc, fig.width = 6, fig.height = 4.5----------------------------- +plot(out.mc, main = "Estimated ATT (MC)") + + +## ----cv_ife_demo, eval=TRUE, cache=TRUE, message=FALSE, results='hide'-------- +out.cv <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + method = "ife", CV = TRUE, r = c(0, 5), + se = FALSE, parallel = TRUE, cores = 16) + + +## ----print-cv-selected-r------------------------------------------------------ +cat("Selected r:", out.cv$r.cv, "\n") + + +## ----cv_method_compare, eval=TRUE, cache=TRUE, message=FALSE, results='hide'---- +out.all <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + method = "ife", CV = TRUE, r = c(0, 5), + cv.method = "all_units", se = FALSE, parallel = TRUE, cores = 16) + +out.tr <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + method = "ife", CV = TRUE, r = c(0, 5), + cv.method = "treated_units", se = FALSE, parallel = TRUE, cores = 16) + + +## ----print-cv-method-compare-------------------------------------------------- +cat("cv.method = 'all_units': r.cv =", out.all$r.cv, "\n") +cat("cv.method = 'treated_units': r.cv =", out.tr$r.cv, "\n") + + +## ----criterion_compare, eval=TRUE, cache=TRUE, message=FALSE, results='hide'---- +out.mspe <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + method = "ife", CV = TRUE, r = c(0, 5), + criterion = "mspe", se = FALSE, parallel = TRUE, cores = 16) + +out.pc <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + method = "ife", CV = TRUE, r = c(0, 5), + criterion = "gmspe", se = FALSE, parallel = TRUE, cores = 16) + + +## ----print-criterion-compare-------------------------------------------------- +cat("criterion = 'mspe': r.cv =", out.mspe$r.cv, "\n") +cat("criterion = 'gmspe': r.cv =", out.pc$r.cv, "\n") + + +## ----placebo_ife, eval = TRUE, cache = TRUE, message = FALSE, results='hide'---- +out.ife.p <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), + force = "two-way", method = "ife", r = 2, CV = 0, + parallel = TRUE, cores = 16, se = TRUE, + nboots = 200, placeboTest = TRUE, placebo.period = c(-2, 0)) + +out.mc.p <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), + force = "two-way", method = "mc", lambda = out.mc$lambda.cv, + CV = 0, parallel = TRUE, cores = 16, se = TRUE, + nboots = 200, placeboTest = TRUE, placebo.period = c(-2, 0)) + + +## ----placebo_ife_plot, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +plot(out.ife.p, ylab = "Effect of D on Y", main = "Estimated ATT (IFE)", + cex.text = 0.8, stats = c("placebo.p","equiv.p")) + + +## ----placebo_mc_plot, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +plot(out.mc.p, cex.text = 0.8, stats = c("placebo.p","equiv.p"), + main = "Estimated ATT (MC)") + + +## ----simdata_ife_loo, eval=TRUE, cache = TRUE, message = FALSE, results = 'hide'---- +out.ife.loo <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + method = "ife", force = "two-way", se = TRUE, parallel = TRUE, cores = 16, nboots = 200, loo = TRUE) +out.mc.loo <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id","time"), + method = "mc", force = "two-way", se = TRUE, parallel = TRUE, cores = 16, nboots = 200, loo = TRUE) + + +## ----pretrend_ife, eval = TRUE, cache = TRUE, fig.width = 6, fig.height = 4.5, warning = FALSE---- +plot(out.ife.loo, type = "equiv", ylim = c(-4,4), loo = TRUE, + cex.legend = 0.6, main = "Testing Pre-Trend (IFEct)", cex.text = 0.8) + + +## ----pretrend_mc, eval = TRUE, cache = TRUE, fig.width = 6, fig.height = 4.5, warning = FALSE---- +plot(out.mc.loo, type = "equiv", ylim = c(-4,4), loo = TRUE, + cex.legend = 0.6, main = "Testing Pre-Trend (MC)", cex.text = 0.8) + + +## ----carryover_ife, eval = TRUE, cache = TRUE, message = FALSE, results='hide'---- +out.ife.c <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), + force = "two-way", method = "ife", r = 2, CV = 0, + parallel = TRUE, cores = 16, se = TRUE, + nboots = 200, carryoverTest = TRUE, carryover.period = c(1, 3)) + +out.mc.c <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), + force = "two-way", method = "mc", lambda = out.mc$lambda.cv, + CV = 0, parallel = TRUE, cores = 16, se = TRUE, + nboots = 200, carryoverTest = TRUE, carryover.period = c(1, 3)) + + +## ----carryover_ife_plot, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 5---- +plot(out.ife.c, type = "exit", ylim = c(-2.5,4.5), + cex.text = 0.8, main = "Carryover Effects (IFE)") + + +## ----carryover_mc_plot, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 5---- +plot(out.mc.c, type = "exit", ylim = c(-2.5,4.5), + cex.text = 0.8, main = "Carryover Effects (MC)") + + +## ----carryover_rm, eval = TRUE, cache = TRUE, message = FALSE, results='hide', fig.width = 6, fig.height = 4.5---- +out.ife.rm.test <- fect(Y ~ D + X1 + X2, data = simdata, index = c("id", "time"), + force = "two-way", method = "ife", r = 2, CV = 0, + parallel = TRUE, cores = 16, se = TRUE, carryover.rm = 3, + nboots = 200, carryoverTest = TRUE, carryover.period = c(1, 3))# remove three periods + +plot(out.ife.rm.test, cex.text = 0.8, stats.pos = c(5, 2.5)) + diff --git a/vignettes/rscript/04-cfe.R b/vignettes/rscript/04-cfe.R new file mode 100644 index 00000000..94d003c1 --- /dev/null +++ b/vignettes/rscript/04-cfe.R @@ -0,0 +1,222 @@ +## ----.common, include = FALSE------------------------------------------------- +source("_common.R") + + +## ----setup-cfe, echo = FALSE-------------------------------------------------- +set.seed(1234) + + +## ----load-packages-cfe, message = FALSE, warning = FALSE---------------------- +data(simdata) +data(sim_region) +data(sim_linear) +data(sim_trend) + + +## ----cfe-42-load, eval = TRUE------------------------------------------------- +head(sim_region) + + +## ----cfe-42-fe-only, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'---- +out.fe.only <- fect(Y ~ D, data = sim_region, + index = c("id", "time"), + method = "fe", force = "two-way", + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) + + +## ----cfe-42-fe-only-plot, fig.width = 6, fig.height = 4.5--------------------- +plot(out.fe.only, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "FE Only — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) + + +## ----cfe-42-with-region, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'---- +out.cfe.region <- fect(Y ~ D, data = sim_region, + index = c("id", "time", "region_time"), + method = "cfe", force = "two-way", + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) + + +## ----cfe-42-with-region-plot, fig.width = 6, fig.height = 4.5----------------- +plot(out.cfe.region, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "CFE with Region×Time FE — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) + + +## ----cfe-43-fe-baseline, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'---- +out.fe.base <- fect(Y ~ D + X1 + X2, data = simdata, + index = c("id", "time"), + method = "fe", force = "two-way", + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) + + +## ----cfe-43-fe-baseline-plot, fig.width = 6, fig.height = 4.5----------------- +plot(out.fe.base, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "FE Only (simdata) — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) + + +## ----cfe-43-gamma-setup, eval = TRUE------------------------------------------ +simdata$gamma_t <- simdata$time + + +## ----cfe-43-with-z, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'---- +out.cfe.z <- fect(Y ~ D + X1 + X2, data = simdata, + index = c("id", "time"), + method = "cfe", force = "two-way", + Z = "L1", gamma = "gamma_t", + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) + + +## ----cfe-43-with-z-plot, fig.width = 6, fig.height = 4.5---------------------- +plot(out.cfe.z, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "CFE with Z = L1 — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) + + +## ----cfe-44-linear-load, eval = TRUE------------------------------------------ +head(sim_linear) + + +## ----cfe-44-lin-fe-only, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'---- +out.fe.lin <- fect(Y ~ D, data = sim_linear, + index = c("id", "time"), + method = "fe", force = "two-way", + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) + + +## ----cfe-44-lin-fe-only-plot, fig.width = 6, fig.height = 4.5----------------- +plot(out.fe.lin, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "FE Only (Linear Trend DGP) — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) + + +## ----cfe-44-lin-cfe, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'---- +out.cfe.lin <- fect(Y ~ D, data = sim_linear, + index = c("id", "time"), + method = "cfe", force = "two-way", + Q.type = "linear", + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) + + +## ----cfe-44-lin-cfe-plot, fig.width = 6, fig.height = 4.5--------------------- +plot(out.cfe.lin, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "CFE with Linear Trend — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) + + +## ----cfe-44-sin-load, eval = TRUE--------------------------------------------- +head(sim_trend) + + +## ----cfe-44-sin-fe-only, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'---- +out.fe.trend <- fect(Y ~ D, data = sim_trend, + index = c("id", "time"), + method = "fe", force = "two-way", + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) + + +## ----cfe-44-sin-fe-only-plot, fig.width = 6, fig.height = 4.5----------------- +plot(out.fe.trend, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "FE Only (Sin Trend DGP) — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) + + +## ----cfe-44-sin-bspline, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'---- +out.cfe.bs <- fect(Y ~ D, data = sim_trend, + index = c("id", "time"), + method = "cfe", force = "two-way", + Q.type = "bspline", + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) + + +## ----cfe-44-sin-bspline-plot, fig.width = 6, fig.height = 4.5----------------- +plot(out.cfe.bs, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "CFE with B-spline Trend — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) + + +## ----cfe-45-gamma-setup, eval = TRUE------------------------------------------ +simdata$gamma_t <- simdata$time + + +## ----cfe-45-fit-models, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'---- +# Model 1: FE only +out.fe <- fect(Y ~ D + X1 + X2, data = simdata, + index = c("id", "time"), + method = "fe", force = "two-way", se = FALSE) + +# Model 2: CFE with Z = L1 only +out.cfe.z.only <- fect(Y ~ D + X1 + X2, data = simdata, + index = c("id", "time"), + method = "cfe", force = "two-way", + Z = "L1", gamma = "gamma_t", + se = FALSE) + +# Model 3: CFE with Z = L1 + 1 factor +out.cfe.z.f1 <- fect(Y ~ D + X1 + X2, data = simdata, + index = c("id", "time"), + method = "cfe", force = "two-way", + Z = "L1", gamma = "gamma_t", + r = 1, se = FALSE) + +# Model 4: IFE with 2 factors +out.ife.r2 <- fect(Y ~ D + X1 + X2, data = simdata, + index = c("id", "time"), + method = "ife", force = "two-way", + r = 2, se = FALSE) + + +## ----cfe-45-mspe, eval = TRUE, cache = TRUE----------------------------------- +mspe.out <- fect_mspe( + list(FE = out.fe, + CFE_Z = out.cfe.z.only, + CFE_Z_F1 = out.cfe.z.f1, + IFE_r2 = out.ife.r2), + seed = 1234) +print(mspe.out$summary[, c("Model", "MSPE", "RMSE", "MAD")]) + + +## ----cfe-45-best-placebo, eval = TRUE, cache = TRUE, message = FALSE, warning = FALSE, results = 'hide'---- +out.cfe.best <- fect(Y ~ D + X1 + X2, data = simdata, + index = c("id", "time"), + method = "cfe", force = "two-way", + Z = "L1", gamma = "gamma_t", + r = 1, + se = TRUE, parallel = TRUE, cores = 16, nboots = 200, + placeboTest = TRUE, placebo.period = c(-2, 0)) + + +## ----cfe-45-best-placebo-plot, fig.width = 6, fig.height = 4.5---------------- +plot(out.cfe.best, cex.text = 0.8, + stats = c("placebo.p", "equiv.p"), + main = "CFE (Z + 1 Factor) — Placebo Test", + cex.main = 0.8, cex.lab = 0.8, cex.axis = 0.8) + + +## ----cfe-46-zparam-example, eval = FALSE-------------------------------------- +# # Example with Z.param (not run — requires appropriate data) +# # out <- fect(Y ~ D, data = mydata, +# # index = c("unit", "time"), +# # method = "cfe", force = "two-way", +# # Z = c("baseline_gdp", "baseline_pop"), +# # gamma = c("decade", "political_era"), +# # Z.param = list(decade = "baseline_gdp", +# # political_era = "baseline_pop")) + diff --git a/vignettes/rscript/05-hte.R b/vignettes/rscript/05-hte.R new file mode 100644 index 00000000..160d0ab4 --- /dev/null +++ b/vignettes/rscript/05-hte.R @@ -0,0 +1,127 @@ +## ----.common, include = FALSE------------------------------------------------- +source("_common.R") + + +## ----setup-hte, echo = FALSE, message = FALSE, warning = FALSE---------------- +set.seed(1234) +data(sim_base) + + +## ----hte_setup, eval=TRUE, cache=TRUE, message=FALSE, results='hide'---------- +out.fect <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id","time"), + method = "fe", force = "two-way", se = TRUE, + parallel = TRUE, cores = 16, nboots = 1000) + + +## ----hte, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +plot(out.fect, type = "box", xlim = c(-15, 10)) + + +## ----hte_time, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +plot(out.fect, type = "calendar", xlim = c(1, 35)) + + +## ----hte_X1, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +plot(out.fect, type = "hte", covariate = "X1") + + +## ----hte_discrete, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +sim_base$X3 <- sample(1:3, size = nrow(sim_base), replace = TRUE) +out.fect.X3 <- fect(Y ~ D + X1 + X2 + X3, data = sim_base, index = c("id","time"), + method = "fe", se = TRUE, seed = 123, + nboots = 1000, parallel = TRUE, cores = 16) + + +## ----plot-hte-discrete, fig.width = 6, fig.height = 4.5----------------------- +plot(out.fect.X3, type="hte", covariate = "X3", + xlab = "", ylab = "Effect of D on Y", + covariate.labels = c("USA", "China", "UK"), + ylim = c(-2, 6)) + + +## ----cm_fe, eval = TRUE, cache = TRUE, message = FALSE, results = 'hide'------ +out.cm <- fect(Y ~ D + X1 + X2, data = sim_base, index = c("id", "time"), + method = "fe", force = "two-way", se = TRUE, + cm = TRUE, parallel = TRUE, cores = 16, nboots = 1000) + + +## ----hte_em, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +plot(out.cm, type = "hte", covariate = "X1", + xlab = "Moderator (X1)", ylab = "Effect on Y") + + +## ----hte_cm, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +plot(out.cm, type = "hte", covariate = "X1", cm = TRUE, + xlab = "Moderator (X1)", ylab = "Effect on Y", ylim = c(-0.5, 5)) + + +## ----hte_scatter, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +plot(out.cm, type = "hte", covariate = "X1", cm = TRUE, loess.fit = FALSE, + xlab = "Moderator (X1)", ylab = "Effect on Y", ylim = c(-0.5, 5)) + + +## ----hte_placebo, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +plot(out.cm, type = "hte", covariate = "X1", + pretreatment = TRUE, num.pretreatment = 3, + xlab = "X1", ylab = "Placebo Effect") + + +## ----hte_placebo_cm, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +plot(out.cm, type = "hte", covariate = "X1", cm = TRUE, + pretreatment = TRUE, num.pretreatment = 3, + xlab = "X1", ylab = "Placebo Effect", ylim = c(-0.5, 1.5)) + + +## ----iden_test, eval = TRUE, cache = TRUE, message = FALSE-------------------- +iden.test <- fect_iden(out.cm, moderator = "X1") + + +## ----iden_results, eval = TRUE------------------------------------------------ +cat("=== Treated cells (e1) ===\n") +cat(" n =", iden.test$e1$n, "\n") +cat(" R-squared =", round(iden.test$e1$r2, 4), "\n") +cat(" Test stat =", round(iden.test$e1$stat, 3), "\n") +cat(" df =", iden.test$e1$df, "\n") +cat(" p-value =", round(iden.test$e1$p, 4), "\n\n") + +cat("=== Control cells (e0) ===\n") +cat(" n =", iden.test$e0$n, "\n") +cat(" R-squared =", round(iden.test$e0$r2, 4), "\n") +cat(" Test stat =", round(iden.test$e0$stat, 3), "\n") +cat(" df =", iden.test$e0$df, "\n") +cat(" p-value =", round(iden.test$e0$p, 4), "\n") + + +## ----iden_components, eval = TRUE, cache = TRUE, message = FALSE-------------- +# Quadratic terms only (no interactions) +iden.quad <- fect_iden(out.cm, moderator = "X1", interaction = FALSE) +cat("Quadratic-only: p =", + round(iden.quad$e1$p, 4), "(treated),", + round(iden.quad$e0$p, 4), "(control)\n") + +# Interactions only (no quadratics) +iden.inter <- fect_iden(out.cm, moderator = "X1", quadratic = FALSE) +cat("Interaction-only: p =", + round(iden.inter$e1$p, 4), "(treated),", + round(iden.inter$e0$p, 4), "(control)\n") + + +## ----discrete_cm_setup, eval = TRUE, cache = TRUE, message = FALSE, results = 'hide'---- +sim_base$X3 <- sample(1:3, size = nrow(sim_base), replace = TRUE) +out.discrete <- fect(Y ~ D + X1 + X2 + X3, data = sim_base, + index = c("id", "time"), + method = "fe", force = "two-way", se = TRUE, + cm = TRUE, parallel = TRUE, cores = 16, nboots = 1000) + + +## ----discrete_em_plot, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +plot(out.discrete, type = "hte", covariate = "X3", + covariate.labels = c("USA", "China", "UK"), + xlab = "", ylab = "Effect on Y", ylim = c(-0.5, 5)) + + +## ----discrete_cm_plot, eval = TRUE, cache = TRUE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +plot(out.discrete, type = "hte", covariate = "X3", cm = TRUE, + covariate.labels = c("USA", "China", "UK"), + xlab = "", ylab = "Effect on Y", ylim = c(-0.5, 5)) + diff --git a/vignettes/rscript/06-plots.R b/vignettes/rscript/06-plots.R new file mode 100644 index 00000000..b9bfa8f6 --- /dev/null +++ b/vignettes/rscript/06-plots.R @@ -0,0 +1,353 @@ +## ----.common, include = FALSE------------------------------------------------- +source("_common.R") + + +## ----load, message=FALSE------------------------------------------------------ +# load libraries and data +library(ggplot2) +library(panelView) +data(gs2020) +data(hh2019) +ls() + + +## ----est, cache = TRUE-------------------------------------------------------- +out <- fect(Y = "general_sharetotal_A_all", + D = "cand_A_all", + X = c("cand_H_all", "cand_B_all"), + index = c("district_final", "cycle"), + data = gs2020, method = "fe", + force = "two-way", se = TRUE, + parallel = TRUE, cores = 16, nboots = 1000) + +out.hh <- fect(nat_rate_ord ~ indirect, + data = hh2019, + index = c("bfs","year"), + method = 'fe', se = TRUE, + parallel = TRUE, cores = 16, nboots = 1000, + keep.sims = TRUE) + + +## ----plot-gap-default--------------------------------------------------------- +plot(out) # the effect of co-ethnic mobilization +plot(out.hh) # the effect of indirect democracy on naturalization rate + + +## ----begin-post-customization------------------------------------------------- +plot(out, start0 = TRUE, + main = "Custom Starting Period") + + +## ----connected-estimates------------------------------------------------------ +plot(out, + post.color = "green4", + connected = TRUE, + est.lwidth = 1.2, + est.pointsize = 3) + + +## ----ci-outline--------------------------------------------------------------- +plot(out, + connected = TRUE, + ci.outline = TRUE, + main = "The Effect of Coethnic Mobilization") +plot(out.hh, + preset = "vibrant", + ci.outline = TRUE, + main = "The Effect of Indirect Democracy") + + +## ----preset-vibrant----------------------------------------------------------- +plot(out, + preset = "vibrant", + main = "Vibrant Preset Colors: Grumbach and Sahn (2020)") +plot(out.hh, + preset = "vibrant", + main = "Vibrant Preset Colors: Hainmueller and Hangartner (2019)") + + +## ----preset-grayscale--------------------------------------------------------- +plot(out, + preset = "grayscale", + main = "Grayscale Preset Colors") + + +## ----preset-vibrant2---------------------------------------------------------- +plot(out.hh, + preset = "vibrant", + post.color = "green4", + main = "Change Estimates' Color: Hainmueller and Hangartner (2019)") + + +## ----ci-raw-customization----------------------------------------------------- +plot(out, plot.ci = "0.9", + main = "90% confidence intervals") + + +## ----count-histogram-customization-------------------------------------------- +plot(out, + count.color = "lightblue", + count.outline.color = "darkblue", + count.alpha = 0.2, + main = "Count Histogram Customization") + + +## ----axis-legend-customization------------------------------------------------ +plot(out, + xlim = c(-10, 1), + ylim = c(-0.15, 0.30), + xlab = "Custom Time Axis", + ylab = "Estimated ATT", + xangle = 90, + xbreaks = seq(-10, 1, by = 2), + gridOff = TRUE, + main = "Axis and Legend Customization") + + +## ----text-customization------------------------------------------------------- +plot(out, + ylim = c(-0.15, 0.3), + theme.bw = FALSE, + cex.main = 1.25, + cex.axis = 1.2, + cex.lab = 1.2, + cex.legend = 1, + cex.text = 1.2, + main = "Text and Theme Customization") + + +## ----line-bound-customization------------------------------------------------- +plot(out, + est.lwidth = 1.5, + est.pointsize = 3, + lcolor = c("red","skyblue"), + lwidth = 2, + main = "Line Customization") + + +## ----counterfactual----------------------------------------------------------- +plot(out, type = "counterfactual", + main = "Grumbach & Sahn (2020): Treated vs. Counterfactuals", + ylab = "Proportion of Asian Donation", + legend.pos = "bottom") + + +## ----counterfactual-hh-------------------------------------------------------- +plot(out.hh, type = "counterfactual", + main = "Hainmueller & Hangartner (2019): Treated vs. Counterfactuals", + ylab = "Naturalization Rate", + legend.pos = "top") + + +## ----counterfactual-colors---------------------------------------------------- +plot(out.hh, type = "counterfactual", + main = "Hainmueller & Hangartner (2019): Treated vs. Counterfactuals", + ylab = "Naturalization Rate", + legend.pos = "bottom", + ci.outline = TRUE, + color = "red3", + counterfactual.color = "green4") + + +## ----counterfactual-rawall---------------------------------------------------- +plot(out, type = "counterfactual", raw = "all") + + +## ----counterfactual-rawband--------------------------------------------------- +plot(out, type = "counterfactual", raw = "band") + + +## ----counterfactual-colors2--------------------------------------------------- +plot(out, type = "counterfactual", + count.color = "black", + count.alpha = 1, + color = "red", + counterfactual.color = "purple", + counterfactual.raw.treated.color = "orange", + counterfactual.linetype = "dotted", + raw = "all", + main = "Counterfactual Plot with Custom Colors") + + +## ----placebo, cache = TRUE---------------------------------------------------- +out_fe_placebo <- fect(Y = "general_sharetotal_A_all", D = "cand_A_all", X = c("cand_H_all", "cand_B_all"), data = gs2020, + index = c("district_final", "cycle"), force = "two-way", + method = "fe", CV = FALSE, parallel = TRUE, cores = 16, + se = TRUE, nboots = 1000, placeboTest = TRUE, + placebo.period = c(-2, 0)) + +plot(out_fe_placebo) + + +## ----plot-placebo-connected--------------------------------------------------- +plot(out_fe_placebo, connected = TRUE, preset = "grayscale", + main = "Placebo Test with Connected Estimates") + + +## ----plot-placebo-color------------------------------------------------------- +plot(out_fe_placebo, placebo.color = "green4") + + +## ----plot-equiv-bound--------------------------------------------------------- +plot(out, type = "equiv", bound = "equiv", tost.threshold = 0.1, + ylim = c(-0.15, 0.15)) + + +## ----plot-equiv-min----------------------------------------------------------- +plot(out, type = "equiv", bound = "min", ylim = c(-0.15, 0.15)) + + +## ----plot-equiv-both---------------------------------------------------------- +plot(out, type = "equiv", tost.threshold = 0.1, ylim = c(-0.15, 0.15)) + + +## ----stats-customization------------------------------------------------------ +plot(out, type = "equiv", + ylim = c(-0.25, 0.25), + stats = c("F.p", "equiv.p"), + stats.labs = c("F Test P-value", "Equivalence P-value"), + stats.pos = c(-8, 0.2), + show.stats = TRUE, + main = "Statistical Test Annotations") + + +## ----plot-exit-default-------------------------------------------------------- +plot(out_fe_placebo, type = "exit") + + +## ----carryover, cache = TRUE-------------------------------------------------- +out_fe_carryover <- fect(Y = "general_sharetotal_A_all", D = "cand_A_all", X = c("cand_H_all", "cand_B_all"), data = gs2020, + index = c("district_final", "cycle"), force = "two-way", + parallel = TRUE, cores = 16, se = TRUE, CV = FALSE, + nboots = 1000, carryoverTest = TRUE, + carryover.period = c(1, 3)) +plot(out_fe_carryover) + + +## ----plot-cumulative-hh------------------------------------------------------- +plot(effect(out.hh), main = "Cumulative Effect of Indirect Democracy", + ylab = "Cumulative Effect on Naturalization Rate") + + +## ----subset-no-reversals------------------------------------------------------ +# flag units that ever have a 1 to 0 change in d +rev_flag <- tapply(gs2020[["cand_A_all"]], + gs2020[["district_final"]], + function(x) any(diff(x) < 0)) + +# units with no reversals +good_units <- names(rev_flag)[!rev_flag] + +# subset the desired rows +gs2020_no_reversals <- gs2020[gs2020[["district_final"]] %in% good_units, ] + + + +## ----no-reversals-est, cache = TRUE------------------------------------------- +out_no_reversals <- fect(Y = "general_sharetotal_A_all", + D = "cand_A_all" , + X = c("cand_H_all", "cand_B_all") , + index = c("district_final", "cycle"), + data = gs2020_no_reversals, + method = "fe", + force = "two-way", + se = TRUE, parallel = TRUE, cores = 16, + nboots = 1000, + keep.sims = TRUE) + + +## ----cumulative-effects------------------------------------------------------- +plot(effect(out_no_reversals), xlim = c(1, 2)) + + +## ----plot-box-hte------------------------------------------------------------- +plot(out, type = "box", xlim = c(-12, 3)) + + +## ----plot-calendar-hte-------------------------------------------------------- +plot(out, type = "calendar", main = "The Effect of Coethnic Mobilization") +plot(out.hh, type = "calendar", xlim = c(1995, 2009), + main = "The Effect of Indirect Democracy") + + +## ----plot-hte-covariate------------------------------------------------------- +plot(out, type = "hte", covariate = "cand_B_all", + main = "HTE by Black Candidate Presence", + xlab = "Black Candidate Indicator", + ylab = "Effect on Asian Donation Share") + + +## ----plot-hte-discrete-ch6---------------------------------------------------- +plot(out, type = "hte", covariate = "cand_H_all", + covariate.labels = c("No Hispanic Candidate", "Hispanic Candidate"), + main = "HTE by Hispanic Candidate Presence", + ylab = "Effect on Asian Donation Share") + + +## ----status------------------------------------------------------------------- +plot(out_fe_carryover, type = "status", + status.treat.color = "#D55E00", + status.control.color = "#0072B2", + status.carryover.color = "#CC79A7", + status.missing.color = "#009E73", + status.background.color = "#F3EAD2", + main = "Status Plot") + + +## ----est-ife, cache = TRUE---------------------------------------------------- +out_ife <- fect(nat_rate_ord ~ indirect, + data = hh2019, + index = c("bfs", "year"), + method = "ife", r = 2, + se = TRUE, parallel = TRUE, cores = 16, nboots = 1000) + + +## ----plot-factors------------------------------------------------------------- +plot(out_ife, type = "factors", main = "Estimated Latent Factors") + + +## ----plot-factors-nofe-------------------------------------------------------- +plot(out_ife, type = "factors", include.FE = FALSE, + main = "Factors without Fixed Effects") + + +## ----plot-loadings------------------------------------------------------------ +plot(out_ife, type = "loadings", main = "Factor Loadings") + + +## ----esplot-basic, fig.width = 6, fig.height = 4.5---------------------------- +# Create example data from a fect result +es_data <- data.frame( + Time = as.numeric(rownames(out$est.att)), + ATT = out$est.att[, "ATT"], + CI.lower = out$est.att[, "CI.lower"], + CI.upper = out$est.att[, "CI.upper"] +) + +esplot(es_data, Period = "Time", + main = "Event Study Plot with esplot()", + ylab = "Estimated ATT", + xlab = "Periods Since Treatment", + xlim = c(-15, 5), ylim = c(-0.3, 0.7)) + + +## ----esplot-fect-object, fig.width = 6, fig.height = 4.5---------------------- +esplot(out, main = "Direct from fect object") + + +## ----esplot-connected, fig.width = 6, fig.height = 4.5------------------------ +esplot(es_data, Period = "Time", + connected = TRUE, + main = "Connected Event Study Plot", + ylab = "Estimated ATT", + xlim = c(-15, 5), ylim = c(-0.3, 0.7)) + + +## ----esplot-highlight, fig.width = 6, fig.height = 4.5------------------------ +esplot(es_data, Period = "Time", + highlight.periods = c(-2, -1, 0), + highlight.colors = c("orange", "orange", "red"), + main = "Highlighting Key Periods", + ylab = "Estimated ATT", + xlim = c(-15, 5), ylim = c(-0.3, 0.7)) + diff --git a/vignettes/rscript/07-gsynth.R b/vignettes/rscript/07-gsynth.R new file mode 100644 index 00000000..b861292e --- /dev/null +++ b/vignettes/rscript/07-gsynth.R @@ -0,0 +1,283 @@ +## ----.common, include = FALSE------------------------------------------------- +source("_common.R") + + +## ----setup-seed, echo = FALSE------------------------------------------------- +set.seed(1234) + + +## ----load-packages, warning=FALSE, message=FALSE------------------------------ +data(sim_gsynth) +data(turnout) +ls() + + +## ----head-sim_gsynth---------------------------------------------------------- +head(sim_gsynth) + + +## ----sim-panelview-status, cache = FALSE, fig.height=7, fig.width=7, warning=FALSE---- +library(panelView) +panelview(Y ~ D, data = sim_gsynth, index = c("id","time"), pre.post = TRUE) + + +## ----sim-panelview-outcome, cache = FALSE,fig.height=5, fig.width=7----------- +panelview(Y ~ D, data = sim_gsynth, index = c("id","time"), type = "outcome") + + +## ----sim2_onecore, cache = TRUE----------------------------------------------- +system.time( +out <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), + method = "gsynth", force = "two-way", CV = TRUE, r = c(0, 5), + se = TRUE, nboots = 1000, vartype = 'parametric', + parallel = TRUE, cores = 16)) + + +## ----print-results, eval = FALSE---------------------------------------------- +# print(out) +# out$est.att +# out$est.avg +# out$beta + + +## ----sim2, cache = TRUE, warning = FALSE-------------------------------------- +system.time( +out <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), method = "gsynth", force = "two-way", CV = TRUE, r = c(0, 5), se = TRUE, nboots = 1000, vartype = 'parametric', parallel = TRUE, cores = 16) +) + + +## ----simJack, cache = TRUE, message = FALSE----------------------------------- +out2 <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), + method = "gsynth", force = "two-way", + CV = TRUE, r = c(0, 5), se = TRUE, + vartype = "jackknife", + parallel = TRUE, cores = 16) + + + +## ----sim_gap1, fig.height=5, fig.width=7-------------------------------------- +a <- plot(out) # by default, type = "gap" +print(a) + + +## ----sim_gap1a, fig.height=5, fig.width=7------------------------------------- +plot(out, theme.bw = FALSE) + + +## ----sim-gap-connected, fig.height=5, fig.width=7----------------------------- +plot(out, connected = TRUE) + + +## ----sim-gap-line, fig.height=5, fig.width=7---------------------------------- +plot(out, connected = TRUE, show.points = FALSE) + + +## ----sim_gap2, fig.height=5, fig.width=7-------------------------------------- +plot(out, type = "gap", ylim = c(-6,12), xlab = "Period", + main = "Estimated ATT (Gsynth)") + + +## ----sim-counterfactual, cache = FALSE, fig.height=5, fig.width=7------------- +plot(out, type = "counterfactual") + + +## ----sim-counterfactual-all, cache = FALSE,fig.height=5, fig.width=7---------- +plot(out, type = "counterfactual", raw = "all") + + +## ----sim-counterfactual-band, cache = FALSE,fig.height=5, fig.width=7--------- +plot(out, type = "counterfactual", raw = "band") + + +## ----sim_status, cache = FALSE,fig.height=7, fig.width=7---------------------- +plot(out, type = "status", yticklabels="0", + xticklabels=c("5", "10", "15","20", "25", "30") ) + + +## ----sim_L, cache = TRUE, message = FALSE, results='hide', fig.height=7, fig.width=7---- +plot(out, type = "loadings") + + +## ----sim_F, message = FALSE, results='hide', fig.height=5, fig.width=7-------- +plot(out, type = "factors", xlab = "Time") + + +## ----sim_box, cache = FALSE,fig.height=5, fig.width=8------------------------- +plot(out, type = "box", xlab = "time", + xticklabels=c("-19", "-15", "-10", "-5","0","5","10") ) + + +## ----sim_box2, eval = FALSE, fig.height=7, fig.width=7------------------------ +# plot(out, type = "box", xlim = c(-15, 10), +# xticklabels=c( "-15", "-10", "-5","0","5","10")) + + +## ----calendar, cache = FALSE,fig.height=5, fig.width=7------------------------ +plot(out,type = "calendar") + + +## ----sim-equiv, cache = FALSE, fig.height=5, fig.width=7---------------------- +plot(out, type = "equiv", ylim = c(-5, 5)) + + +## ----sim-equiv-no-stats, cache = FALSE, fig.height=5, fig.width=7------------- +plot(out, type = "equiv", show.stats = FALSE) + + +## ----sim-equiv-reposition, cache = FALSE, fig.height=5, fig.width=7----------- +plot(out, type = "equiv", stats.pos = c(-19, 4.5), ylim = c(-5, 5)) + + +## ----turnout-panelview-status, cache = FALSE, warning=FALSE, fig.height=10, fig.width=7---- +panelview(turnout ~ policy_edr, data = turnout, + index = c("abb","year"), pre.post = TRUE, + by.timing = TRUE) + + +## ----turnout-panelview-outcome, cache = FALSE, warning =FALSE, fig.height=5, fig.width=7---- +panelview(turnout ~ policy_edr, data = turnout, + index = c("abb","year"), type = "outcome", + main = "EDR Reform and Turnout", + by.group = TRUE) + + +## ----turnout_did, cache = TRUE------------------------------------------------ +out0 <- fect(turnout ~ policy_edr + policy_mail_in + policy_motor, + data = turnout, index = c("abb","year"), + se = TRUE, method = "gsynth", + r = 0, CV = FALSE, force = "two-way", + nboots = 1000, seed = 02139) + + +## ----turnout_did_gap, fig.height=5, fig.width=7------------------------------- +plot(out0, type = "gap", xlim = c(-15, 5), ylim=c(-15, 10)) + + +## ----turnout_est, cache = TRUE------------------------------------------------ +out_turnout <- fect(turnout ~ policy_edr + policy_mail_in + policy_motor, + data = turnout, index = c("abb","year"), + se = TRUE, method = "gsynth", vartype = "parametric", + r = c(0, 5), CV = TRUE, force = "two-way", + nboots = 1000, seed = 02139, keep.sims = TRUE) + + +## ----turnout-implied-weights-------------------------------------------------- +dim(out_turnout$wgt.implied) +sort(out_turnout$wgt.implied[,8]) + + +## ----turnout_gap, fig.height=5, fig.width=7----------------------------------- +plot(out_turnout, xlim = c(-10, 5), ylim=c(-15, 10)) + + +## ----turnout-status-plot, fig.height=12, fig.width=7-------------------------- +plot(out_turnout, type = "status",xlab = "Year", ylab = "State", main = "Treatment Status", + xticklabels=c(1920, 1928, 1936, 1944, 1952, 1960, + 1968, 1976, 1984, 1992, 2000, 2008), xangle=10) + + +## ----turnout_counterfactual, fig.height=5, fig.width=7------------------------ +plot(out_turnout, type = "counterfactual") + + +## ----turnout_gap2, fig.height=5, fig.width=7---------------------------------- +plot(out_turnout, type = "counterfactual", id = "WI", main = "Wisconsin") + + +## ----turnout_box, fig.height=4, fig.width=8----------------------------------- +plot(out_turnout, type = "box", + xticklabels=c("-20", "-15", "-10", "-5","0","5","10")) + + +## ----turnout_calendar, fig.height=5, fig.width=7------------------------------ +plot(out_turnout, type = "calendar", ylim = c(-15,15)) + + +## ----turnout_F, message = FALSE, results = 'hide', fig.height=5, fig.width=7, warning=FALSE---- +plot(out_turnout, type = "factors", xlab = "Year") + + +## ----turnout_L, message = FALSE, results = 'hide', fig.height=7, fig.width=7, warning=FALSE---- +plot(out_turnout, type = "loadings") + + +## ----create-unbalanced-data--------------------------------------------------- +set.seed(123456) +turnout.ub <- turnout[-c(which(turnout$abb=="WY")[1:15], + sample(1:nrow(turnout),50,replace=FALSE)),] + + +## ----turnout_ub_panelview_miss, cache = FALSE,fig.height=7, fig.width=7------- +panelview(turnout ~ policy_edr + policy_mail_in + policy_motor, + data = turnout.ub, index = c("abb","year"), + pre.post = TRUE) + + +## ----turnout_ub_est, cache = TRUE, message = FALSE---------------------------- +out_ub <- fect(turnout ~ policy_edr + policy_mail_in + policy_motor, + data = turnout.ub, index = c("abb","year"), + se = TRUE, method = "gsynth", + r = c(0, 5), CV = TRUE, force = "two-way", + parallel = TRUE, cores = 16, min.T0 = 8, + nboots = 1000, seed = 02139) + + +## ----turnout_ub_panelview_miss2, fig.height=12, fig.width=7------------------- +plot(out_ub, type = "status", + xticklabels=c(1920, 1928, 1936, 1944, 1952, 1960, + 1968, 1976, 1984, 1992, 2000, 2008), + xangle=10) + + +## ----turnout_ub_obs_2, fig.height=7, fig.width=7------------------------------ +plot(out_ub, type = "status", xlab = "Year", ylab = "State", + main = "Treatment Status", id = out_ub$id[out_ub$tr], + xlim = c(1920,2012), + xticklabels=c(1920, 1928, 1936, 1944, 1952, 1960, + 1968, 1976, 1984, 1992, 2000, 2008)) + + + +## ----turnout_ub_gap, fig.height=5, fig.width=7-------------------------------- +plot(out_ub, type = "gap", ylim = c(-10, 20)) + + +## ----cfe_nt_demo, eval=TRUE, cache=TRUE, message=FALSE, results='hide'-------- +out.cfe.nt <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), + method = "cfe", force = "two-way", + time.component.from = "nevertreated", + Q.type = "linear", + se = FALSE, CV = TRUE, r = c(0, 5), + parallel = TRUE, cores = 16) + + +## ----cfe-nt-summary----------------------------------------------------------- +cat("CFE + nevertreated: r.cv =", out.cfe.nt$r.cv, + ", ATT =", round(out.cfe.nt$att.avg, 3), "\n") + + +## ----cfe_nt_vs_gsynth, eval=TRUE, cache=TRUE, message=FALSE, results='hide'---- +# Model 1: gsynth (pure IFE, r = 2) +out.gsynth.comp <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), + method = "gsynth", force = "two-way", + r = 2, se = FALSE, CV = FALSE) + +# Model 2: CFE + nevertreated with r = 2 only (equivalent to gsynth) +out.cfe.nt.comp <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), + method = "cfe", force = "two-way", + time.component.from = "nevertreated", + r = 2, se = FALSE, CV = FALSE) + +# Model 3: CFE + nevertreated with r = 2 and linear trend (overspecified) +out.cfe.nt.lin <- fect(Y ~ D + X1 + X2, data = sim_gsynth, index = c("id","time"), + method = "cfe", force = "two-way", + time.component.from = "nevertreated", + Q.type = "linear", r = 2, se = FALSE, CV = FALSE) + + +## ----cfe_nt_mspe, eval=TRUE, cache=TRUE--------------------------------------- +mspe.comp <- fect_mspe(list(gsynth_r2 = out.gsynth.comp, + CFE_r2 = out.cfe.nt.comp, + CFE_linear_r2 = out.cfe.nt.lin), seed = 1234) +print(mspe.comp$summary[, c("Model", "MSPE", "RMSE", "MAD")]) + diff --git a/vignettes/rscript/08-panel.R b/vignettes/rscript/08-panel.R new file mode 100644 index 00000000..b73cbd19 --- /dev/null +++ b/vignettes/rscript/08-panel.R @@ -0,0 +1,627 @@ +## ----.common, include = FALSE------------------------------------------------- +source("_common.R") + + +## ----install-packages, message = FALSE, warning = FALSE----------------------- +# install packages from CRAN +packages <- c("dplyr", "fixest", "did", "didimputation", + "panelView", "ggplot2", "bacondecomp", "HonestDiD", + "DIDmultiplegtDYN", "PanelMatch", "readstata13") +install.packages(setdiff(packages, rownames(installed.packages()))) + +# install most up-to-date "fect" from Github +if ("fect" %in% rownames(installed.packages()) == FALSE) { + devtools:: install_github("xuyiqing/fect") +} + +# install forked "HonestDiD" package compatible with "fect" +if ("HonestDiDFEct" %in% rownames(installed.packages()) == FALSE) { + devtools:: install_github("lzy318/HonestDiDFEct") +} + + +## ----load-libraries, message = FALSE, warning = FALSE------------------------- +library(dplyr) +library(readstata13) +library(fixest) +library(did) +library(panelView) +library(PanelMatch) +library(ggplot2) +library(bacondecomp) +library(didimputation) +library(doParallel) +library(HonestDiD) +library(HonestDiDFEct) +has_polars <- requireNamespace("polars", quietly = TRUE) +if (has_polars) { + library(polars) + library(DIDmultiplegtDYN) # requires polars; may require XQuartz for rgl +} + + +## ----load-hh2019, message = FALSE, warning = FALSE---------------------------- +data(hh2019) +data <- hh2019 +head(data) + + +## ----hh_panelview_treat, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +panelview(nat_rate_ord ~ indirect, data = data, index = c("bfs","year"), + xlab = "Year", ylab = "Unit", display.all = T, + gridOff = TRUE, by.timing = TRUE) + + +## ----hh_panelview_cohort, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5,cache=TRUE---- +panelview(data = data,Y='nat_rate_ord', + D='indirect',index=c("bfs","year"), + by.timing = TRUE, display.all = TRUE, + type = "outcome", by.cohort = TRUE) + + +## ----hh_twfe1, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5---- +# remember to cluster standard errors +model.twfe.0 <- feols(nat_rate_ord~indirect|bfs+year, + data=data, cluster = "bfs") +print(model.twfe.0) + + +## ----hh_bacon, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +data.complete <- data[which(!is.na(data$nat_rate_ord)),] # bacon requires no missingness in the data +df_bacon <- bacon(nat_rate_ord~indirect, + data = data.complete, + id_var = "bfs", + time_var = "year") +ggplot(df_bacon) + + aes(x = weight, y = estimate, shape = factor(type), color = factor(type)) + + labs(x = "Weight", y = "Estimate", shape = "Type", color = 'Type') + + geom_point() + +print(aggregate(df_bacon$estimate * df_bacon$weight, + list(df_bacon$type), FUN=sum)) + + +## ----hh_twfe2, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +# drop always treated units +df <- as.data.frame(data %>% + group_by(bfs) %>% + mutate(treatment_mean = mean(indirect,na.rm = TRUE))) +df.use <- df[which(df$treatment_mean<1),] + +# Re-estimate TWFE on this Sub-sample +model.twfe.1 <- feols(nat_rate_ord~indirect|bfs+year, + data=df.use, cluster = "bfs") +print(model.twfe.1) + + +## ----hh_cohort, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +df.use <- get.cohort(df.use, D = "indirect", index=c("bfs","year"), + start0 = TRUE) +head(df.use[,-5],19) + + +## ----hh_twfeplot, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +# Dynamic TWFE +df.twfe <- df.use +# drop always treated units +df.twfe$treat <- as.numeric(df.twfe$treatment_mean>0) +df.twfe[which(is.na(df.twfe$Time_to_Treatment)),'Time_to_Treatment'] <- 0 # can be an arbitrary value +twfe.est <- feols(nat_rate_ord ~ i(Time_to_Treatment, treat, ref = -1)| bfs + year, + data = df.twfe, cluster = "bfs") +twfe.output <- as.matrix(twfe.est$coeftable) +print(round(twfe.output, 3)) + + +## ----hh_twfeplot2, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +twfe.output <- as.data.frame(twfe.output) +twfe.output$Time <- c(c(-18:-2),c(0:17))+1 +p.twfe <- esplot(twfe.output,Period = 'Time',Estimate = 'Estimate', + SE = 'Std. Error', xlim = c(-12,10)) +p.twfe + + +## ----hh_twfeplot3, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +twfe.output <- as.data.frame(twfe.est$coeftable) +twfe.output$Time <- c(c(-18:-2),c(0:17)) +p.twfe <- esplot(twfe.output, Period = 'Time', + Estimate = 'Estimate', SE = 'Std. Error', + xlim = c(-12,10),start0 = TRUE) +p.twfe + + +## ----hh_st, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +df.st <- NULL +target.cohorts <- setdiff(unique(df.use$Cohort),"Control") +k <- 1 +for(cohort in target.cohorts){ + df.sub <- df.use[which(df.use$Cohort%in%c(cohort,"Control")),] + df.sub$stack <- k + df.st <- rbind(df.st,df.sub) + k <- k + 1 +} +df.st$st_unit <- as.numeric(factor(paste0(df.st$stack,'-',df.st$bfs))) +df.st$st_year <- as.numeric(factor(paste0(df.st$stack,'-',df.st$year))) +model.st <- feols(nat_rate_ord~indirect|st_unit+st_year, + data=df.st, cluster = "st_unit") + +print(model.st) + + +## ----hh_stplot, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +df.st$treat <- as.numeric(df.st$treatment_mean>0) +df.st[which(is.na(df.st$Time_to_Treatment)),'Time_to_Treatment'] <- 1000 +# note, this "1000" can be arbitrary value + +st.est <- feols(nat_rate_ord ~ + i(Time_to_Treatment, treat, ref = -1)| st_unit + + st_year,data = df.st,cluster = "st_unit") + +# make plot +st.output <- as.data.frame(st.est$coeftable) +st.output$Time <- c(c(-18:-2),c(0:17))+1 +p.st <- esplot(st.output,Period = 'Time',Estimate = 'Estimate', + SE = 'Std. Error', xlim = c(-12,10)) +p.st + + +## ----fig-iw-illustration, echo=FALSE, out.width="50%", fig.align="center"----- +knitr::include_graphics("fig/fig_iw.png") + + +## ----hh_sa, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +df.sa <- df.use +df.sa[which(is.na(df.sa$FirstTreat)),"FirstTreat"] <- 1000 +# above, replace NA with an arbitrary number + +model.sa.1 <- feols(nat_rate_ord~sunab(FirstTreat,year)|bfs+year, + data = df.sa, cluster = "bfs") +summary(model.sa.1,agg = "ATT") + + +## ----hh_saplot, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +sa.output <- as.data.frame(as.matrix(model.sa.1$coeftable)) +sa.output$Time <- c(c(-18:-2),c(0:17)) + 1 +p.sa <- esplot(sa.output,Period = 'Time',Estimate = 'Estimate', + SE = 'Std. Error', xlim = c(-12,10)) +p.sa + + +## ----hh_cs1, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +df.cs <- df.use +df.cs[which(is.na(df.cs$FirstTreat)),"FirstTreat"] <- 0 # replace NA with 0 +cs.est.1 <- att_gt(yname = "nat_rate_ord", + gname = "FirstTreat", + idname = "bfs", + tname = "year", + xformla = ~1, + control_group = "nevertreated", + allow_unbalanced_panel = TRUE, + data = df.cs, + est_method = "reg") +cs.est.att.1 <- aggte(cs.est.1, type = "simple", na.rm=T, bstrap = F) +print(cs.est.att.1) + + +## ----hh_csplot1, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +cs.att.1 <- aggte(cs.est.1, type = "dynamic", + bstrap=FALSE, cband=FALSE, na.rm=T) +print(cs.att.1) + + +## ----hh_csplot1a, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +cs.output <- cbind.data.frame(Estimate = cs.att.1$att.egt, + SE = cs.att.1$se.egt, + time = cs.att.1$egt + 1) +p.cs.1 <- esplot(cs.output,Period = 'time',Estimate = 'Estimate', + SE = 'SE', xlim = c(-12,10)) +p.cs.1 + + +## ----hh_csplot1b, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +cs.est.1.u <- att_gt(yname = "nat_rate_ord", + gname = "FirstTreat", + idname = "bfs", + tname = "year", + xformla = ~1, + control_group = "nevertreated", + allow_unbalanced_panel = TRUE, + data = df.cs, + est_method = "reg", + base_period = "universal") +cs.att.1.u <- aggte(cs.est.1.u, type = "dynamic", + bstrap=FALSE, cband=FALSE, na.rm=T) +cs.output.u <- cbind.data.frame(Estimate = cs.att.1.u$att.egt, + SE = cs.att.1.u$se.egt, + time = cs.att.1.u$egt + 1) +p.cs.1.u <- esplot(cs.output.u,Period = 'time',Estimate = 'Estimate', + SE = 'SE', xlim = c(-12,10)) +p.cs.1.u + + +## ----fig-cs-illustration, echo=FALSE, out.width="50%", fig.align="center"----- +knitr::include_graphics("fig/fig_cs.png") + + +## ----hh_cs2, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +cs.est.2 <- att_gt(yname = "nat_rate_ord", + gname = "FirstTreat", + idname = "bfs", + tname = "year", + xformla = ~1, + control_group = "notyettreated", + allow_unbalanced_panel = TRUE, + data = df.cs, + est_method = "reg") +cs.est.att.2 <- aggte(cs.est.2, type = "simple",na.rm=T, bstrap = F) +print(cs.est.att.2) + + +## ----hh_csplot2b, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +cs.est.2.u <- att_gt(yname = "nat_rate_ord", gname = "FirstTreat", + idname = "bfs", tname = "year", xformla = ~1, + control_group = "notyettreated", + allow_unbalanced_panel = TRUE, + data = df.cs, est_method = "reg", + base_period = "universal") + +cs.att.2.u <- aggte(cs.est.2.u, type = "dynamic", + bstrap=FALSE, cband=FALSE, na.rm=T) + +# plot +cs.output.u <- cbind.data.frame(Estimate = cs.att.2.u$att.egt, + SE = cs.att.2.u$se.egt, + time = cs.att.2.u$egt + 1) +p.cs.2.u <- esplot(cs.output.u,Period = 'time',Estimate = 'Estimate', + SE = 'SE', xlim = c(-12,10)) +p.cs.2.u + + +## ----fig-pm-illustration, echo=FALSE, out.width="50%", fig.align="center"----- +knitr::include_graphics("fig/fig_pm.png") + + + + + + +## ----hh_pm, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +df.pm <- df.use +# we need to convert the unit and time indicator to integer +df.pm[,"bfs"] <- as.integer(as.factor(df.pm[,"bfs"])) +df.pm[,"year"] <- as.integer(as.factor(df.pm[,"year"])) +df.pm <- df.pm[,c("bfs","year","nat_rate_ord","indirect")] + +# Pre-processes and balances panel data +df.pm <- PanelData(panel.data = df.pm, + unit.id = "bfs", + time.id = "year", + treatment = "indirect", + outcome = "nat_rate_ord") + +PM.results <- PanelMatch(lag=3, + refinement.method = "none", + panel.data = df.pm, + qoi = "att", + lead = c(0:3), + match.missing = TRUE) + +## For pre-treatment dynamic effects +PM.results.placebo <- PanelMatch(lag=3, + refinement.method = "none", + panel.data = df.pm, + qoi = "att", + lead = c(0:3), + match.missing = TRUE, + placebo.test = TRUE) + + + +## ----hh_pm1, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +# ATT +PE.results.pool <- PanelEstimate(PM.results, panel.data = df.pm, pooled = TRUE) +summary(PE.results.pool) + + +## ----hh_pm2, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +# Dynamic Treatment Effects +PE.results <- PanelEstimate(PM.results, panel.data = df.pm) +PE.results.placebo <- placebo_test(PM.results.placebo, panel.data = df.pm, plot = F) + +# obtain lead and lag (placebo) estimates +est_lead <- as.vector(PE.results$estimate) +est_lag <- as.vector(PE.results.placebo$estimates) +sd_lead <- apply(PE.results$bootstrapped.estimates,2,sd) +sd_lag <- apply(PE.results.placebo$bootstrapped.estimates,2,sd) +coef <- c(est_lag, 0, est_lead) +sd <- c(sd_lag, 0, sd_lead) +pm.output <- cbind.data.frame(ATT=coef, se=sd, t=c(-2:4)) + +# plot +p.pm <- esplot(data = pm.output,Period = 't', + Estimate = 'ATT',SE = 'se') +p.pm + + +## ----fig-fect-illustration, echo=FALSE, out.width="50%", fig.align="center"---- +knitr::include_graphics("fig/fig_fect.png") + + +## ----hh_fect, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +out.fect <- fect(nat_rate_ord~indirect, data = df, + index = c("bfs","year"), + method = 'fe', se = TRUE) +print(out.fect$est.avg) + + +## ----hh_fectplot, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +fect.output <- as.matrix(out.fect$est.att) +head(fect.output) + + +## ----hh_impute, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +df.impute <- df.use +df.impute[which(is.na(df.impute$FirstTreat)),"FirstTreat"] <- 0 +# above, replace NA with 0 + +out.impute <- did_imputation(data = df.impute, + yname = "nat_rate_ord", + gname = "FirstTreat", + tname = "year", + idname = "bfs", + cluster_var = "bfs") +out.impute + + +## ----hh_fectplot2, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +fect.output <- as.data.frame(fect.output) +fect.output$Time <- c(-17:18) +p.fect <- esplot(fect.output,Period = 'Time',Estimate = 'ATT', + SE = 'S.E.',CI.lower = "CI.lower", + CI.upper = 'CI.upper',xlim = c(-12,10)) +p.fect + + +## ----hh_impute2, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +model.impute <- did_imputation(data = df.impute, + yname = "nat_rate_ord", + gname = "FirstTreat", + tname = "year", + idname = "bfs", + cluster_var = "bfs", + pretrends = c(-13:-1), + horizon = TRUE) +model.impute$term <- as.numeric(model.impute$term)+1 +# above, set 1 as the first post-treatment period + +# plot +to_plot <- as.data.frame(model.impute) +esplot(data=to_plot,Period = "term", + Estimate = 'estimate', SE = 'std.error', + xlim = c(-12,10)) +out.impute + + +## ----hh_balance, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +out.fect.balance <- fect(nat_rate_ord~indirect, data = df, + index = c("bfs","year"), + method = 'fe', se = TRUE, + balance.period = c(-2,4)) +# att +print(out.fect.balance$est.balance.avg) + +# event study plot +fect.balance.output <- as.data.frame(out.fect.balance$est.balance.att) +fect.balance.output$Time <- c(-2:4) +p.fect.balance <- esplot(fect.balance.output,Period = 'Time', + Estimate = 'ATT', SE = 'S.E.', + CI.lower = "CI.lower", + CI.upper = 'CI.upper') +p.fect.balance + + +## ----load-gs2020, message = FALSE, warning = FALSE---------------------------- +data(gs2020) +data <- gs2020 +data$cycle <- as.integer(as.numeric(data$cycle/2)) +head(data) + + +## ----gb_panelview_treat, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +y <- "general_sharetotal_A_all" +d <- "cand_A_all" +unit <- "district_final" +time <- "cycle" +controls <- c("cand_H_all", "cand_B_all") +index <- c("district_final", "cycle") + +panelview(Y=y, D=d, X=controls, index = index, data = data, + xlab = "Time Period", ylab = "Unit", gridOff = TRUE, + by.timing = TRUE, cex.legend=5, cex.axis= 5, + cex.main = 10, cex.lab = 5) + + + +## ----gb_twfe1, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +model.twfe <- feols(general_sharetotal_A_all ~ cand_A_all + + cand_H_all + cand_B_all | district_final + cycle, + data=data, cluster = "district_final") +summary(model.twfe) + + +## ----gb_twfe2, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +data_cohort <- get.cohort(data, index = index, D=d,start0 = TRUE) +# Generate a dummy variable treat +data_cohort$treat <- 0 +data_cohort[which(data_cohort$Cohort!='Control'),'treat'] <- 1 +data_cohort[which(is.na(data_cohort$Time_to_Treatment)), "treat"] <- 0 + +# remove observations that starts with treated status +remove <- intersect(which(is.na(data_cohort$Time_to_Treatment)), + which(data_cohort[,d]==1)) +if(length(remove)>0){data_cohort <- data_cohort[-remove,]} + +# replace missingness in Time_to_Treatment with an arbitrary number +data_cohort[which(is.na(data_cohort$Time_to_Treatment)), "Time_to_Treatment"] <- 999 + +twfe.est <- feols(general_sharetotal_A_all ~ + i(Time_to_Treatment, treat, ref = -1) + + cand_H_all +cand_B_all | district_final + cycle, + data = data_cohort, cluster = "district_final") + + +## ----gb_twfeplot, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +twfe.output <- as.data.frame(twfe.est$coeftable[c(1:25),]) +twfe.output$Time <- c(c(-16:-2),c(0:9)) + 1 + +# plot +p.twfe <- esplot(twfe.output,Period = 'Time',Estimate = 'Estimate', + SE = 'Std. Error', xlim = c(-15,1)) +p.twfe + + +## ----gb_pm, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +df.pm <- data_cohort +# we need to convert the unit and time indicator to integer +df.pm[,"district_final"] <- as.integer(as.factor(df.pm[,"district_final"])) +df.pm[,"cycle"] <- as.integer(as.factor(df.pm[,"cycle"])) +df.pm <- df.pm[,c("district_final","cycle","cand_A_all", + "general_sharetotal_A_all")] + +# Pre-processes and balances panel data +df.pm <- PanelData(panel.data = df.pm, + unit.id = "district_final", + time.id = "cycle", + treatment = "cand_A_all", + outcome = "general_sharetotal_A_all") + +PM.results <- PanelMatch(lag=4, + refinement.method = "none", + panel.data = df.pm, + qoi = "att", + lead = 0, + match.missing = TRUE) + +## For pre-treatment dynamic effects +PM.results.placebo <- PanelMatch(lag=4, + refinement.method = "none", + panel.data = df.pm, + qoi = "att", + lead = 0, + match.missing = TRUE, + placebo.test = TRUE) + + + +## ----gb_pm1, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +PE.results.pool <- PanelEstimate(PM.results, panel.data = df.pm, pooled = TRUE) +summary(PE.results.pool) + + +## ----gb_pm2, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE, eval = TRUE---- +# Dynamic Treatment Effects +PE.results <- PanelEstimate(PM.results, panel.data = df.pm) +PE.results.placebo <- placebo_test(PM.results.placebo, panel.data = df.pm, + plot = FALSE) + +est_lead <- as.vector(PE.results$estimate) +est_lag <- as.vector(PE.results.placebo$estimates) +sd_lead <- apply(PE.results$bootstrapped.estimates,2,sd) +sd_lag <- apply(PE.results.placebo$bootstrapped.estimates,2,sd) +coef <- c(est_lag, 0, est_lead) +sd <- c(sd_lag, 0, sd_lead) +pm.output <- cbind.data.frame(ATT=coef, se=sd, t=c(-3:1)) + +# plot +p.pm <- esplot(data = pm.output,Period = 't', + Estimate = 'ATT',SE = 'se') +p.pm + + +## ----gb_fect, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +model.fect <- fect(Y = "general_sharetotal_A_all", D = "cand_A_all", + X= c("cand_H_all", "cand_B_all"), data = data, + method = "fe", index = index, se = TRUE, + parallel = TRUE, cores = 16, seed = 1234, force = "two-way") + +print(model.fect$est.avg) + + +## ----gb_fectplot, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +fect.output <- as.data.frame(model.fect$est.att) +fect.output$Time <- c(-15:10) +p.fect <- esplot(fect.output,Period = 'Time',Estimate = 'ATT', + SE = 'S.E.',CI.lower = "CI.lower", + CI.upper = 'CI.upper', xlim = c(-15,1)) +p.fect + + +## ----gb_fectplot3, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +plot(model.fect) + + +## ----gb_fectplot4, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +plot(model.fect, type = 'exit') + + +## ----hh_fectplacebo, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +out.fect.p <- fect(Y = y, X = controls, D = d, data = data, index = index, + method = 'fe', se = TRUE, placeboTest = TRUE, + placebo.period = c(-2,0)) + +plot(out.fect.p, proportion = 0.1, stats = "placebo.p") + + +## ----gb_fectcarryover, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +out.fect.c <- fect(Y = y, X = controls, D = d, data = data, index = index, + method = 'fe', se = TRUE, carryoverTest = TRUE, carryover.period = c(1,2)) + +# plot +plot(out.fect.c, stats = "carryover.p", ylim = c(-0.15, 0.20)) + + +## ----gb_balance, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +out.fect.balance <- fect(Y = y, X = controls, D = d, data = data, + index = index, method = 'fe', se = TRUE, + balance.period = c(-3,1)) + +# att +print(out.fect.balance$est.balance.avg) + +# event study plot +fect.balance.output <- as.data.frame(out.fect.balance$est.balance.att) +fect.balance.output$Time <- c(-3:1) +p.fect.balance <- esplot(fect.balance.output,Period = 'Time',Estimate = 'ATT', + SE = 'S.E.',CI.lower = "CI.lower", + CI.upper = 'CI.upper') +p.fect.balance + + +## ----wrapper, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +res_st <- did_wrapper( + data = hh2019, + Y = "nat_rate_ord", + D = "indirect", + index = c("bfs", "year"), + method = "st", + se = "default" +) +print(res_st) + + +## ----wrapper_boot, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 4.5, cache=TRUE---- +res_st <- did_wrapper( + data = hh2019, + Y = "nat_rate_ord", + D = "indirect", + index = c("bfs", "year"), + method = "st", + se = "boot", + nboots = 200, + parallel = TRUE +) +print(res_st) + + +## ----wrapper_plot, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5, cache=TRUE---- +esplot(data = res_st, main = "Stacked DID", xlim = c(-12,10)) + diff --git a/vignettes/rscript/09-sens.R b/vignettes/rscript/09-sens.R new file mode 100644 index 00000000..b65c5d33 --- /dev/null +++ b/vignettes/rscript/09-sens.R @@ -0,0 +1,118 @@ +## ----.common, include = FALSE------------------------------------------------- +source("_common.R") + + +## ----install-packages, message = FALSE, warning = FALSE----------------------- +# install packages from CRAN +packages <- c("dplyr", "panelView", "ggplot2") # Removed HonestDiD, doParallel +install.packages(setdiff(packages, rownames(installed.packages()))) + +# install most up-to-date "fect" from Github +if ("fect" %in% rownames(installed.packages()) == FALSE) { + devtools:: install_github("xuyiqing/fect") +} + +# install forked "HonestDiD" package compatible with "fect" +if ("HonestDiDFEct" %in% rownames(installed.packages()) == FALSE) { + devtools:: install_github("lzy318/HonestDiDFEct") # This is used by fect_sens +} + + +## ----load-libraries, message = FALSE, warning = FALSE------------------------- +library(dplyr) +library(panelView) +library(ggplot2) +library(HonestDiDFEct) # Required for fect_sens to work + + +## ----load-hh2019, message = FALSE, warning = FALSE---------------------------- +data(hh2019) +data <- hh2019 +head(data) + + +## ----hh_honest_placebo, warning=FALSE, message=FALSE, cache=FALSE------------- +out.fect.placebo <- fect(nat_rate_ord~indirect, data = hh2019, + index = c("bfs","year"), + method = 'fe', se = TRUE, + placeboTest = TRUE, placebo.period = c(-2,0), + parallel = TRUE, cores = 16) + +# Ensure att.vcov is a valid matrix (compute from bootstrap samples if needed) +if (!is.matrix(out.fect.placebo$att.vcov) && is.matrix(out.fect.placebo$att.boot)) { + out.fect.placebo$att.vcov <- cov(t(out.fect.placebo$att.boot), + use = "pairwise.complete.obs") +} + +# Define post-treatment periods and sensitivity parameters for fect_sens +T.post <- 10 # Number of post-treatment periods based on original analysis +post_periods_vec <- 1:T.post + +# Parameters for Relative Magnitude (RM) restriction +Mbar_vec_avg_rm <- seq(0, 1, by = 0.1) # For average ATT plot +Mbar_vec_period_rm <- c(0, 0.5) # For period-by-period ATT plot + +# Parameters for Smoothness restriction +M_vec_avg_smooth <- seq(0, 0.25, by = 0.05) # For average ATT plot +M_vec_period_smooth <- c(0, 0.1) # For period-by-period ATT plot + +# Run sensitivity analysis using fect_sens +# This function augments out.fect.placebo with sensitivity results +out.fect.placebo <- fect_sens( + fect.out = out.fect.placebo, + post.periods = post_periods_vec, + Mbarvec = Mbar_vec_avg_rm, + periodMbarvec = Mbar_vec_period_rm, + Mvec = M_vec_avg_smooth, + periodMvec = M_vec_period_smooth, + parallel = FALSE # Set to TRUE for parallel processing if desired +) + + +## ----hh_honest.placebo.honest, fig.width = 6, fig.height = 4.5, cache=TRUE---- +plot(out.fect.placebo, + type = "sens", + restrict = "rm", + main = "Relative Magnitude Restriction") + + +## ----hh_honest.placebo.honest.gap.plot, fig.width=7, fig.height=5, cache=TRUE---- +plot(out.fect.placebo, + type = "sens_es", + restrict = "rm", + main = "ATTs with Robust Confidence Sets (RM)", + ylab = "Coefficients and 95% CI", + xlim = c(-12,10), + ylim = c(-6,8), + show.count = TRUE) + + +## ----hh_honest.placebo.honest.gap.plot.colors, fig.width=7, fig.height=5, cache=TRUE---- +plot(out.fect.placebo, + type = "sens_es", + restrict = "rm", + main = "ATTs with Robust Confidence Sets (RM)", + ylab = "Coefficients and 95% CI", + xlim = c(-12,10), + ylim = c(-6,8), + show.count = TRUE, + sens.colors = c("blue", "red")) + + +## ----hh_honest.placebo.honest.sd, fig.width = 7, fig.height = 5, cache=TRUE---- +plot(out.fect.placebo, + type = "sens", + restrict = "sm", + main = "Smoothness Restriction") + + +## ----hh_honest.placebo.honest.gap.sd.plot, fig.height=5, fig.width=7, cache=TRUE, warning=FALSE---- +plot(out.fect.placebo, + type = "sens_es", + restrict = "sm", + main = "ATTs with Robust Confidence Sets (Smoothness)", + ylab = "Coefficients and 95% CI", + xlim = c(-12,10), # Adjusted to match original detailed plot + ylim = c(-12,15), + show.count = TRUE) + diff --git a/vignettes/fectbook.Rproj b/vignettes/vignettes.Rproj similarity index 100% rename from vignettes/fectbook.Rproj rename to vignettes/vignettes.Rproj