--- title: "Choosing controls for a treatment effect" output: rmarkdown::html_vignette: toc: true toc_depth: 2 vignette: > %\VignetteIndexEntry{Choosing controls for a treatment effect} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} run <- all(vapply(c("dagitty", "glmnet", "ncvreg"), requireNamespace, logical(1L), quietly = TRUE)) knitr::opts_chunk$set(collapse = TRUE, comment = "#>", eval = run) library(flexySelect) # Numbers quoted in the text, read from the runs below. fmt <- function(x, k = 2L) { if (run) formatC(x, format = "f", digits = k) else "NA" } perf <- function(res, arm, col) { res$performance[res$performance$arm == arm, col] } kept <- function(res, arm, role) { r <- res$roles 100 * r$mean[r$arm == arm & r$role %in% role & r$metric == "kept" & r$label_set == "disjunctive"] } ``` To estimate the effect of a treatment on an outcome, you adjust for some of the other variables in the data. Some must be adjusted for, some make the estimate more precise, some make it less precise, and some bias it. Which is which depends on the causal graph. A selector that keeps the predictors of the outcome ignores the graph, and keeps any harmful variable that predicts the outcome well. This vignette simulates data from a graph with a confounder, an instrument, a cause of the outcome, a mediator and a collider, and shows which of them two selectors keep and what that does to the estimated effect. # A graph with five kinds of control The treatment is `X` and the outcome `Y`. The other five variables are: * `Z`, a confounder, which causes both `X` and `Y` * `I`, an instrument, which causes `X` only * `W`, which causes `Y` only * `M`, a mediator, on the path from `X` to `Y` * `C`, a collider, caused by both `X` and `Y` `fs_regime()` builds a linear Gaussian model from the graph and a standardised coefficient on each edge. flexySelect calls this a regime. Every variable has variance 1. `roles` records the kind of each variable, so that the variables a selector keeps can be checked against what should be kept. ```{r regime} dag <- "dag { Z -> X ; Z -> Y ; I -> X ; W -> Y X -> M ; M -> Y ; X -> Y ; X -> C ; Y -> C }" coef <- c( "Z->X" = 0.5, "Z->Y" = 0.4, "I->X" = 0.5, "W->Y" = 0.4, "X->M" = 0.5, "M->Y" = 0.3, "X->Y" = 0.2, "X->C" = 0.4, "Y->C" = 0.4 ) roles <- c(Z = "confounder", I = "instrument", W = "outcome_cause", M = "mediator", C = "collider") regime <- fs_regime(dag, coef = coef, roles = roles) regime ``` The total effect of `X` on `Y` is the direct path, 0.2, plus the path through the mediator, 0.5 times 0.3, which gives 0.35. Adjusting for `Z` alone recovers it. # Bias and standard error of each adjustment set In a linear Gaussian model, the coefficient of `X` given any adjustment set can be computed exactly from the graph and its coefficients, and so can the standard error of its least-squares estimate at a given sample size (the finite-sample value, not an approximation). `fs_adjustment()` computes them for all 32 sets. The table shows seven of them at n = 200. `slope` is the coefficient of `X` given the set, `bias` is `slope` minus 0.35, `valid` marks a set that identifies the total effect, `se` is the standard error, and `rmse` is the square root of `bias` squared plus `se` squared. ```{r adjustment} costs <- fs_adjustment(regime, n = 200L) show <- c("{}", "{Z}", "{W, Z}", "{I, W, Z}", "{W, Z, M}", "{W, Z, C}", "{I, W, Z, M, C}") costs <- costs[match(show, costs$set), c("set", "valid", "optimal", "slope", "bias", "se", "rmse")] costs[, 4:7] <- round(costs[, 4:7], 3) rownames(costs) <- NULL costs ``` * With no adjustment, the confounder raises the slope from 0.35 to 0.55. * `{Z}` removes that bias. Adding `W` keeps the slope at 0.35 and lowers the standard error, because `W` explains part of `Y`. `{W, Z}` is the optimal set, the valid set with the smallest asymptotic variance (Henckel, Perković and Maathuis, 2022). * Adding the instrument keeps the slope at 0.35 but raises the standard error, because the instrument explains part of `X` and leaves less variation in `X` to estimate the slope from. * Adding the mediator blocks the indirect path, so the slope is the direct effect, 0.2, not the total effect. Adding the collider opens a path that is not causal, and biases the slope as well. * Adjusting for every variable gives 0.08, less than a quarter of the total effect. # What two selectors keep `lasso` and `mcp` (the minimax concave penalty) are penalised regressions of `Y` on the candidates, with `X` kept in every fit and left unpenalised (`protect = TRUE`). Each of 50 simulated data sets of 200 rows is analysed with both selectors, adjusting for the variables each one keeps, and with four fixed sets: none, all, `{Z}` (labelled `valid`) and the optimal set. The fixed sets should agree with the exact values above, within Monte Carlo error. ```{r study} grid <- fs_grid(regime, n = 200L, protect = TRUE) study <- fs_study( aims = "How does each selector change the estimate of the effect of X on Y?", data = grid, methods = c("lasso", "mcp"), target = "effect", n_sim = 50L, seed = 20261205L ) study res <- fs_run(study) ``` The table gives the bias, its Monte Carlo standard error (`bias_mcse`), the empirical standard error, the root mean squared error and the coverage of the nominal 95% interval, as defined by Morris, White and Crowther (2019). The interval is the usual least-squares one, which takes no account of the selection step. ```{r performance} perf_table <- function(res) { p <- res$performance[, c("arm", "bias", "bias_mcse", "emp_se", "rmse", "coverage")] p[, 2:6] <- round(p[, 2:6], 3) p } perf_table(res) ``` Lasso and MCP underestimate the effect, with a bias of `r fmt(perf(res, "lasso", "bias"))` and `r fmt(perf(res, "mcp", "bias"))`, and their intervals contain 0.35 in `r fmt(100 * perf(res, "lasso", "coverage"), 0L)`% and `r fmt(100 * perf(res, "mcp", "coverage"), 0L)`% of the data sets. Adjusting for every variable has a bias of `r fmt(perf(res, "all", "bias"))`. The share of data sets in which each selector kept each variable shows why. The `verdict` column says whether the variable should be adjusted for, under the modified disjunctive cause criterion, which leaves instruments out (VanderWeele, 2019). ```{r roles} kept_table <- function(res) { r <- res$roles r <- r[r$metric == "kept" & r$label_set == "disjunctive", c("arm", "role", "verdict", "mean")] names(r)[4L] <- "kept" rownames(r) <- NULL r } kept_table(res) ``` Both selectors keep the confounder, which is needed, and `W`, which helps precision. Both keep the mediator in every data set, and they keep the collider in `r fmt(kept(res, "mcp", "collider"), 0L)`% (MCP) and `r fmt(kept(res, "lasso", "collider"), 0L)`% (lasso) of them. Lasso also keeps the instrument in `r fmt(kept(res, "lasso", "instrument"), 0L)`%. Choosing variables by how well they predict `Y` cannot tell a confounder from a mediator or a collider, since all three predict it. Only the graph can. # Using only pre-treatment candidates The mediator and the collider are measured after the treatment. A study that fixes its list of candidate controls before the treatment is given leaves them out, which is a design decision rather than one made from the data. In the code below, `M` and `C` are marked latent: they stay in the graph but are not in the data. ```{r pretreatment} dag_pre <- sub("dag {", "dag {\n M [latent] ; C [latent]", dag, fixed = TRUE) regime_pre <- fs_regime(dag_pre, coef = coef, roles = roles[c("Z", "I", "W")]) study_pre <- fs_study( aims = "The same question, with pre-treatment candidates only.", data = fs_grid(regime_pre, n = 200L, protect = TRUE), methods = c("lasso", "mcp"), target = "effect", n_sim = 50L, seed = 20261205L ) res_pre <- fs_run(study_pre) perf_table(res_pre) kept_table(res_pre) ``` Now the bias of each selector is within its Monte Carlo error of zero. Their root mean squared errors, `r fmt(perf(res_pre, "lasso", "rmse"), 3L)` (lasso) and `r fmt(perf(res_pre, "mcp", "rmse"), 3L)` (MCP), are a little above those of `{Z}`, `r fmt(perf(res_pre, "valid", "rmse"), 3L)`, and the optimal set, `r fmt(perf(res_pre, "optimal", "rmse"), 3L)`. This holds on this graph, where the confounder predicts `Y` well. A confounder that predicts `Y` only weakly can be dropped. ```{r figure, fig.width = 7, fig.height = 4, fig.alt = "Estimated effects of X on Y for each selector and fixed set, with all candidates and with pre-treatment candidates only. With all candidates the selectors are biased downward, close to adjusting for every variable. With pre-treatment candidates they are centred on the total effect of 0.35."} est <- rbind( cbind(res$estimates[, c("arm", "estimate")], candidates = "all"), cbind(res_pre$estimates[, c("arm", "estimate")], candidates = "pre-treatment") ) arms <- c("none", "valid", "optimal", "all", "lasso", "mcp") est$arm <- factor(est$arm, levels = arms) op <- graphics::par(mfrow = c(1L, 2L), mar = c(4, 4, 2, 1)) for (cand in c("all", "pre-treatment")) { e <- est[est$candidates == cand, ] graphics::boxplot(estimate ~ arm, data = e, ylim = range(est$estimate), col = c(rep("grey85", 4L), rep("#56B4E9", 2L)), las = 2, xlab = "", ylab = "estimated effect", main = paste(cand, "candidates")) graphics::abline(h = regime$effect, lty = 2, lwd = 2, col = "#D55E00") } graphics::par(op) ``` The dashed line is the total effect. Grey boxes are the fixed sets and blue boxes the selectors. `all` is a different set in each panel, since it contains every candidate offered. # Reading the result Lasso and MCP choose variables by how well they predict `Y`. That gives a sound estimate of a treatment effect only if no candidate is a mediator or a collider, and only the graph can say which candidates those are. Leaving out variables measured after the treatment is not enough in general. A collider measured before the treatment (M-bias) still biases the estimate. The preset `"cinelli_7"` is this case, and it can be passed to `fs_grid()` by name like any other preset. Simulating a graph and coefficients like your own shows how much a wrong list of candidates could bias the estimate. `fs_regimes()` lists the built-in graphs from the literature on good and bad controls, including the models of Cinelli, Forney and Pearl (2024), with the control in question and whether to adjust for it. ```{r presets} head(fs_regimes()[, c("name", "control", "verdict")], 10L) ``` # References Cinelli, C., Forney, A., & Pearl, J. (2024). A crash course in good and bad controls. *Sociological Methods & Research, 53*(3), 1071--1104. https://doi.org/10.1177/00491241221099552 Henckel, L., Perković, E., & Maathuis, M. H. (2022). Graphical criteria for efficient total effect estimation via adjustment in causal linear models. *Journal of the Royal Statistical Society Series B: Statistical Methodology, 84*(2), 579--599. https://doi.org/10.1111/rssb.12451 Morris, T. P., White, I. R., & Crowther, M. J. (2019). Using simulation studies to evaluate statistical methods. *Statistics in Medicine, 38*(11), 2074--2102. https://doi.org/10.1002/sim.8086 VanderWeele, T. J. (2019). Principles of confounder selection. *European Journal of Epidemiology, 34*(3), 211--219. https://doi.org/10.1007/s10654-019-00494-6