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.
The treatment is X and the outcome Y. The
other five variables are:
Z, a confounder, which causes both X and
YI, an instrument, which causes X onlyW, which causes Y onlyM, a mediator, on the path from X to
YC, a collider, caused by both X and
Yfs_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.
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
#> <fs_regime>
#> 7 nodes (0 latent), 9 edges; total effect of X on Y = 0.35
#> identified by adjusting for {Z}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.
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.
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
#> set valid optimal slope bias se rmse
#> 1 {} FALSE FALSE 0.550 0.200 0.060 0.209
#> 2 {Z} TRUE FALSE 0.350 0.000 0.063 0.063
#> 3 {W, Z} TRUE TRUE 0.350 0.000 0.053 0.053
#> 4 {I, W, Z} TRUE FALSE 0.350 0.000 0.066 0.066
#> 5 {W, Z, M} FALSE FALSE 0.200 -0.150 0.055 0.160
#> 6 {W, Z, C} FALSE FALSE 0.192 -0.158 0.059 0.169
#> 7 {I, W, Z, M, C} FALSE FALSE 0.080 -0.270 0.067 0.278{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).X
and leaves less variation in X to estimate the slope
from.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.
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
#> <fs_study> How does each selector change the estimate of the effect of X on Y?
#> Data 1 mechanism from regime
#> Estimand total effect of X on Y, 0.35 in every mechanism
#> Known now exact bias and SE of none, valid, all, optimal in each regime (`$known`)
#> Methods 2 selectors (lasso, mcp); references none, valid, all, optimal
#> Measures bias, empirical SE, RMSE, coverage; kept by role; cost of each choice
#> Replicates 50 per mechanism: bias MCSE at most 0.011 for every fixed set
#> Cells 1 x 6 x 50 = 300; seed 20261205; plan hash 386d56e9
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.
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)
#> arm bias bias_mcse emp_se rmse coverage
#> 1 lasso -0.268 0.010 0.067 0.277 0.02
#> 2 mcp -0.248 0.012 0.085 0.262 0.08
#> 3 none 0.194 0.009 0.061 0.203 0.08
#> 4 valid -0.007 0.009 0.062 0.062 0.96
#> 5 all -0.275 0.010 0.069 0.284 0.04
#> 6 optimal -0.004 0.008 0.058 0.057 0.92Lasso and MCP underestimate the effect, with a bias of -0.27 and
-0.25, and their intervals contain 0.35 in 2% and 8% of the data sets.
Adjusting for every variable has a bias of -0.28. 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).
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)
#> arm role verdict kept
#> 1 lasso collider drop 1.00
#> 2 lasso confounder keep 1.00
#> 3 lasso instrument drop 0.24
#> 4 lasso mediator drop 1.00
#> 5 lasso outcome_cause keep 1.00
#> 6 mcp collider drop 0.78
#> 7 mcp confounder keep 1.00
#> 8 mcp instrument drop 0.00
#> 9 mcp mediator drop 1.00
#> 10 mcp outcome_cause keep 1.00Both 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 78% (MCP) and 100% (lasso) of
them. Lasso also keeps the instrument in 24%. 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.
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.
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)
#> arm bias bias_mcse emp_se rmse coverage
#> 1 lasso 0.005 0.010 0.070 0.070 0.92
#> 2 mcp -0.002 0.009 0.067 0.066 0.92
#> 3 none 0.194 0.009 0.061 0.203 0.08
#> 4 valid -0.007 0.009 0.062 0.062 0.96
#> 5 all -0.008 0.009 0.063 0.062 0.96
#> 6 optimal -0.004 0.008 0.058 0.057 0.92
kept_table(res_pre)
#> arm role verdict kept
#> 1 lasso confounder keep 0.98
#> 2 lasso instrument drop 0.14
#> 3 lasso outcome_cause keep 1.00
#> 4 mcp confounder keep 0.98
#> 5 mcp instrument drop 0.00
#> 6 mcp outcome_cause keep 1.00Now the bias of each selector is within its Monte Carlo error of
zero. Their root mean squared errors, 0.070 (lasso) and 0.066 (MCP), are
a little above those of {Z}, 0.062, and the optimal set,
0.057. This holds on this graph, where the confounder predicts
Y well. A confounder that predicts Y only
weakly can be dropped.
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")
}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.
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.
head(fs_regimes()[, c("name", "control", "verdict")], 10L)
#> name control verdict
#> 1 cinelli_1 Z good
#> 2 cinelli_2 Z good
#> 3 cinelli_3 Z good
#> 4 cinelli_4 Z good
#> 5 cinelli_5 Z good
#> 6 cinelli_6 Z good
#> 7 cinelli_7 Z bad
#> 8 cinelli_7v Z undecidable
#> 9 cinelli_8 Z neutral
#> 10 cinelli_9 Z neutralCinelli, 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