15  Confounding and Selection Bias on DAGs

A recent study in Buffalo, for example, indicates that smokers differ significantly from nonsmokers in a number of characteristics: they are more neurotic, are more frequently hospitalized, and they change jobs and spouses more often than nonsmokers. Had the two groups been compared for other characteristics, it is quite possible that they would have been found to differ in these also. It is conceivable that some of these other characteristics, rather than smoking, are responsible in whole or in part for the increase in lung cancer among smokers. (Yerushalmy and Palmer 1959)

15.1 Simpson’s paradox

The modern discussion of confounding began with Simpson (1951).1 Table 15.1 shows the example from this paper. Among males and females, treatment is protective. The odds ratio for death comparing the treated and untreated is \[ \begin{aligned} \frac{5 / 8}{3 / 4} &= \frac{20}{24} = \frac{5}{6} \approx 0.83 \text{ among men,} \\ \frac{15 / 12}{3 / 2} &= \frac{30}{36} = \frac{5}{6} \approx 0.83 \text{ among women.} \end{aligned} \] If we ignore biological sex and calculate the crude odds ratio, we get \[ \frac{20 / 20}{6 / 6} = 1. \] This apparent contradiction has come to be known as Simpson’s paradox. Simpson concluded: “The treatment can hardly be rejected as valueless to the race when it is beneficial to males and females.”

Table 15.1: Epidemiologic data stratified by sex (left) and unstratified (right) from Simpson (1951)
Male Female Crude
Dead Alive Dead Alive Dead Alive
Treated 5 8 15 12 20 20
Untreated 3 4 3 2 6 6

This paradox can be resolved using causal directed acyclic graphs (causal DAGs), where nodes represent variables and directed paths from one variable to another represent causal effects. A causal DAG can be used to represent our knowledge or hypotheses about causal relationships among variables (Pearl 1995; Greenland, Pearl, and Robins 1999). In epidemiology, causal DAGs give us useful new insights about how confounding and selection bias occur and how to control them. They also allow us to clearly express the causal assumptions underlying the design and analysis of an epidemiologic study.

15.2 Directed acyclic graphs

In a causal DAG, a series of arrows pointing from a variable \(X\) to a variable \(Y\) is used to show that changing a variable \(X\) (e.g., flipping a switch) will cause a change in \(Y\) (e.g., whether the light is on). A simple example is shown in Figure 15.1. The direction of the arrows captures the asymmetry of causal relationships across time: The past affects the future but the future does not affect the past. In this example, the flipping the light switch can turn on the light but unscrewing the light bulb will not flip the switch. It is good practice to write a DAG so that all arrows point in a consistent direction (e.g., left to right) that represents the passage of time.

Code
dag-lightswitch.R
## Directed acyclic graph for a light switch

# daggity can be installed from github.com/jtextor/dagitty
# ggdag and ggplot2 can be installed using install.packages()
library(dagitty)
library(ggdag, warn.conflicts = FALSE)
library(ggplot2)

# define the DAG
light <- dagitty('dag{
  "Light switch" -> "Light on"
}')
coordinates(light) <- list(x = c("Light switch" = 0, "Light on" = 1),
                           y = c("Light switch" = 0, "Light on" = 0))

# plot the DAG
(ggdag_classic(light)
  + theme_dag_gray_grid()
  + xlim(-0.5, 1.5)
  + ylim(-0.5, 0.5)
)
Figure 15.1: Directed acyclic graph (DAG) showing the causal effect of a light switch on a light.

15.2.1 Adding variables and arrows

When building a causal DAG, we cannot arbitrarily choose which variables to include. Let \(X\) be an exposure or treatment, \(D\) be a disease outcome. To make a causal DAG that represents the causal effect of \(X\) on \(D\), we start with \(X\) and \(D\). Given a set of variables on the DAG, we must add any variable that has a causal effect on two or more of these variables. We continue doing so until there is no common cause of any two variables on the DAG (or, in reality, until any remaining causal relationships are negligible).

The presence or absence of arrows between variables on a DAG are defined in terms of the effects of interventions. Let \(A\) and \(B\) be variables on a DAG and let \(V = (V_1, \ldots, V_k)\) denote all of the other variables on the DAG. Let \[ B^{(a, v)}(\omega) \] be the potential outcome for \(B\) in person \(\omega\) in our population \(\Omega\) when we intervene to set \(A(\omega) = a\) and \(V(\omega) = v = (v_1, \ldots, v_k)\). There is an arrow from \(A\) to \(B\) if and only if there are two values \(a_0\) and \(a_1\) and a vector of values \(v\) such that \[ B^{(a_1, v)}(\omega) \neq B^{(a_0, v)}(\omega) \] for at least one \(\omega \in \Omega\). Intuitively, this means that changing \(A\) alters the value of \(B\) for at least one \(\omega \in \Omega\) even when we intervene to hold all other variables fixed.

The arrow from \(A\) to \(B\) tells us that, for at least one individual, \(A\) has a causal effect on \(B\) that is not mediated through any other variable in the DAG. However, the arrow is either there or not there. It does not tell us anything about how large this effect is, how much of the population experiences it, or anything else quantitative. Some arrows might represent large causal effects for a large proportion of the population while others represent a small causal effect for a small proportion of the population. Still others could represent a large causal effect for a small proportion of the population or a small effect for a large proportion. The DAG abstracts away all of these differences, leaving only what we need to think clearly about the structure of the causal relationships.

15.2.2 Paths and causal paths

The arrows between variables on a DAG are often called edges, which can help us distinguish between situations where we must pay attention to the direction of the arrow and situations where we can safely ignore it. The endpoints of an edge are the head and tail of the arrow. Two edges are adjacent when they have endpoints at the same node.

On a DAG, a path from \(A\) to \(B\) is any sequence of distinct adjacent edges that lead from \(A\) to \(B\) where we can ignore the direction of the arrows. The length of the path is the number of edges it contains. A path might consist of only one arrow. A path from \(A\) to \(B\) where we follow the direction of the arrows is called a directed path or a causal path. The DAG in Figure 15.2 includes the following paths:

  • The path \(B \rightarrow D\) is causal but the path \(D \leftarrow B\) is noncausal.

  • There are two paths from \(A\) to \(C\): \(A \rightarrow C\) and \(A \rightarrow B \rightarrow C\). Both paths are causal.

  • There are two paths from \(B\) to \(C\): \(B \rightarrow C\) and \(B \leftarrow A \rightarrow C\). Only the first path is causal.

  • There are two paths from \(C\) to \(D\): \(C \leftarrow A \rightarrow B \rightarrow D\) and \(C \leftarrow B \rightarrow D\). Neither path is causal.

Code
dag-family.R
## Paths, ancestors, and descendants on a DAG

# load packages
# I set "warn.conflicts = FALSE" to suppress an unnecessary error message.
library(dagitty)
library(ggdag, warn.conflicts = FALSE)
library(ggplot2)

# define the DAG
fam <- dagitty('dag{
  A -> B -> C
  A -> C
  B -> D
}')

# plot the DAG
# Uncomment coord_equal() to get a better-looking DAG in an R plot window.
coordinates(fam) <- list(x = c(A = 0, B = 1, C = 2, D = 2),
                         y = c(A = 0, B = 0, C = 0.5, D = -0.5))
(ggdag_classic(fam)
  + theme_dag_gray_grid()
  + xlim(-0.2, 2.2)
  # + coord_equal()
  # node A
  + annotate("text", x = 0, y = -0.1, label = "Parent of B and C")
  + annotate("text", x = 0, y = -0.2, label = "Ancestor of B, C, and D")
  # node B
  + annotate("text", x = 1, y = -0.1, label = "Child of A")
  + annotate("text", x = 1, y = -0.2, label = "Parent of C and D")
  + annotate("text", x = 1, y = -0.3, label = "Ancestor of C and D")
  # node C
  + annotate("text", x = 2, y = 0.35, label = "Child of A and B")
  + annotate("text", x = 2, y = 0.25, label = "Descendant of A and B")
  # node D
  + annotate("text", x = 2, y = -0.25, label = "Descendant of A and B")
  + annotate("text", x = 2, y = -0.35, label = "Child of B")
)
Figure 15.2: Directed acyclic graph (DAG) showing ancestors, parents, children, and descendants.
dag-family-paths.R
## Paths and causal (directed) paths on a DAG

# load packages
library(dagitty)

# define the DAG
fam <- dagitty('dag{
  A -> B -> C
  A -> C
  B -> D
}')

# paths from B to D
# Setting "directed = TRUE" returns causal paths.
paths(fam, "B", "D")
paths(fam, "B", "D", directed = TRUE)
paths(fam, "D", "B")
paths(fam, "D", "B", directed = TRUE)

# paths from A to C
paths(fam, "A", "C")
paths(fam, "A", "C", directed = TRUE)

# paths from B to C
paths(fam, "B", "C")
paths(fam, "B", "C", directed = TRUE)

# paths from C to D
paths(fam, "C", "D")
paths(fam, "C", "D", directed = TRUE)

15.2.3 Ancestors and descendants

If we can reach node \(B\) along a directed path starting at node \(A\), then \(A\) is an ancestor of \(B\) and \(B\) is a descendant of \(A\). If there is an arrow from \(A\) to \(B\), then \(A\) is a parent of \(B\) and \(B\) is a child of \(A\). Parents are ancestors, and children are descendants.2 These relationships are labelled in Figure 15.2. If we intervene to set the value of the variable \(A\), this affects descendants of \(A\) on the DAG. The ancestors of \(A\) are unaffected.

dag-family-apcd.R
## Ancestors, parents, children, and descendants on a DAG

# load packages
library(dagitty)

# define the DAG
fam <- dagitty('dag{
  A -> B -> C
  A -> C
  B -> D
}')

# node A
# Proper ancestors and descendants do not include the node itself.
ancestors(fam, "A")
ancestors(fam, "A", proper = TRUE)
parents(fam, "A")
children(fam, "A")
descendants(fam, "A")
descendants(fam, "A", proper = TRUE)

# node B
ancestors(fam, "B", proper = TRUE)
parents(fam, "B")
children(fam, "B")
descendants(fam, "B", proper = TRUE)

# node C
ancestors(fam, "C", proper = TRUE)
parents(fam, "C")
children(fam, "C")
descendants(fam, "C", proper = TRUE)

# node D
ancestors(fam, "D", proper = TRUE)
parents(fam, "D")
children(fam, "D")
descendants(fam, "D", proper = TRUE)

A cycle is a directed path from a variable \(A\) back to itself. A DAG is acyclic because a cycle would require at least one arrow representing a causal effect that moves backward in time. Thus, a variable on a DAG cannot be its own ancestor or descendant. To show a feedback loop between variables \(A\) and \(B\) on a DAG, we represent each variable at multiple time points. Figure 15.3 shows a feedback loop between \(A\) and \(B\) over two time points. The links from \(A_1\) to \(A_2\) and \(B_1\) to \(B_2\) show the causal effect of each variable’s value at \(t = 1\) on its value at \(t = 2\). Unfortunately, time in the world of DAGs is stubbornly discrete.

Code
dag-feedback.R
# Feedback loop on a DAG

library(dagitty)
library(ggdag)
library(ggplot2)

# define the DAG
loop <- dagitty('dag{
  A1 -> B1 -> A2 -> B2
  A1 -> A2
  B1 -> B2
}')

# generate DAG data for plot
coordinates(loop) <- list(x = c(A1 = 0, B1 = 1, A2 = 2, B2 = 3),
                          y = c(A1 = 0, B1 = 0, A2 = 0, B2 = 0))
loop_dat <- tidy_dagitty(loop)

# plot DAG using ggplot() to allow curved arrows
# Uncomment coord_equal() to get a better-looking DAG in an R plot window.
(ggplot(loop_dat, aes(x = x, y = y, xend = xend, yend = yend))
  + theme_dag_gray_grid()
  # + coord_equal()
  + geom_dag_text(color = "black", size = 8, parse = TRUE,
                  label = c(expression(A[1]), expression(A[2]),
                            expression(B[1]), expression(B[2])))
  + geom_dag_edges_arc(curvature = c(0.5, 0, 0, 0, -0.5))
)
Figure 15.3: Directed acyclic graph (DAG) showing a feedback loop between \(A\) and \(B\).

15.2.4 Joint distributions consistent with a DAG*

The joint probability density (or mass) function of the variables on a causal DAG can be factored using the conditional distribution of each variable given its parents. If \(V_1, \ldots, V_n\) are variables on a DAG (where the subscript does not necessarily represent time), then the joint probability density is \[ \Pr(V_1 = v_1, \ldots, V_n = v_n) = \prod_{i = 1}^n \Pr(V_i = v_i \given{} \pa_i) \] where \(\pa_i\) represents the values of the parents of \(V_i\) and we let \(\Pr()\) represent probability density functions or probability mass functions as needed. A joint distribution of \(V_1, \ldots, V_n\) is consistent with the DAG if and only if it can be factored in this way. A given DAG is consistent with many possible joint distributions, but the conditional independence relations on it hold for all of them. Some of these distributions may have additional conditional independencies that are not implied by the DAG.3

15.2.5 Open paths and associations

A path from a variable \(A\) to a variable \(B\) can be open or closed. Each variable along the path (except \(A\) and \(B\)) acts like a valve, and the path is open only if all of these valves are open. A variable’s state depends on the path: A variable can be open for some paths and closed for other paths. If all paths between \(A\) and \(B\) are closed, then \(A\) and \(B\) are independent. If there is at least one open path from \(A\) to \(B\), then are distributions consistent with the DAG where \(A\) and \(B\) are not independent.

Whether a variable \(C\) is open or closed along a given path is determined by the rules of d-separation (or directional separation). The first two of these are:

  • A variable \(C\) on the path is a collider if both arrows adjacent to it along the path point toward \(C\). A collider is closed.

  • Any variable that is not a collider is open. A variable \(V\) can be a collider on one path and a non-collider on another path.

Two variables on a DAG are d-separated when all paths between them are closed. When there is at least one open path between them, they are d-connected. Thus, two variables that are d-separated are independent. If two variables are d-connected, they are dependent in most (but not necessarily all) joint distributions consistent with the DAG. The DAG in Figure 15.4 has the following open and closed paths:

  • The path \(A \rightarrow C \leftarrow B\) is closed by the collider at \(C\). There are no other paths between \(A\) and \(B\), so \(A\) and \(B\) are d-separated and \(A \indep B\) (i.e., \(A\) is independent of \(B\)).

  • The path \(B \rightarrow C \rightarrow D\) is open, so \(B\) and \(D\) are d-connected and it is possible that \(B \centernot\indep D\) (i.e., \(B\) is not independent of \(D\)). On this path, \(C\) is not a collider.

Code
dag-valves.R
## Open and closed paths on a DAG

# load R packages
library(dagitty)
library(ggdag)
library(ggplot2)

# define DAG
valves <- dagitty('dag{
  A -> C
  B -> C -> D
}')

# plot DAG using ggplot() directly to allow for curved arrows
# Uncomment coord_equal() to get a better-looking DAG in an R plot window.
coordinates(valves) <- list(x = c(A = 0, B = 1, C = 2, D = 3),
                            y = c(A = 0, B = 0, C = 0, D = 0))
valves_dat <- tidy_dagitty(valves)
(ggplot(valves_dat, aes(x = x, y = y, xend = xend, yend = yend))
  + theme_dag_gray_grid()
  + coord_equal()
  + ylim(-0.1, 0.6)
  + geom_dag_text(color = "black", size = 8, parse = TRUE)
  + geom_dag_edges_arc(curvature = c(0.4, 0, 0))
)
Figure 15.4: Directed acyclic graph (DAG) to illustrate open and closed paths.
dag-valves-paths.R
## Open and closed paths on a DAG

# load R packages
library(dagitty)
library(ggdag)

# define DAG
valves <- dagitty('dag{
  A -> C
  B -> C -> D
}')

# path from A to B is blocked by the collider at C
paths(valves, "A", "B")
dconnected(valves, "A", "B")
dseparated(valves, "A", "B")
isCollider(valves, "A", "C", "B")

# path from B to D is open because C is not a collider on it
paths(valves, "B", "D")
dconnected(valves, "B", "D")
dseparated(valves, "B", "D")
isCollider(valves, "B", "C", "D")

# conditional independence relations implied by the DAG
impliedConditionalIndependencies(valves)

15.2.6 Conditional associations

A DAG can also be used to evaluate conditional associations. Conditioning on a variable \(V\) means that we look at the joint distribution of the other variables in the subset of our population \(\Omega\) with \(V(\omega) = v\) for some possible value \(v\). In a DAG, conditioning on a variable is represented by putting a square around it. The effects of conditioning on a variable are determined by two more rules of d-separation:

  • Conditioning on a collider or a descendant of a collider opens the path through the collider. Conditioning on an ancestor of a collider does not open the path.

  • Conditioning on a non-collider closes the path through it. Conditioning on an ancestor or descendant of a non-collider does not close the path.

Conditioning on a variable switches its state along any path that goes through it. If the variable was open, it becomes closed. If the variable was closed, it becomes open. Conditioning on a descendant of a collider is the only way that conditioning on a variable not on a path can change whether the path is open or closed. The DAG in Figure 15.5 has the following open and closed paths:

  • The path \(A \rightarrow C \leftarrow B\) is opened by conditioning on the collider at \(C\). Because \(A\) and \(B\) are d-connected given \(C\), it is possible that \(A \centernot\indep B \given{} C\) (i.e., \(A\) is not conditionally independent of \(B\) given \(C\)) in a joint distribution consistent with the DAG.

  • Because \(D\) is a descendant of the collider \(C\) on the path from \(A\) to \(B\), conditioning on it opens the path. Thus, \(A\) and \(B\) are d-connected given \(D\) and it is possible that \(A \centernot\indep B \given{} D\).

  • The path \(B \rightarrow C \rightarrow D\) is closed by conditioning on the non-collider at \(C\), and there are no other paths from \(B\) to \(D\). Because \(B\) and \(D\) are d-separated given \(C\), the DAG implies that \(B \indep D \given{} C\) (i.e., \(B\) is conditionally independent of \(D\) given \(C\)).

Code
dag-cvalves.R
## Open and closed paths on a DAG

# load R packages
library(dagitty)
library(ggdag)
library(ggplot2)

# define DAG
valves <- dagitty('dag{
  A -> C
  B -> C -> D
}')

# generate data for plotting DAG using ggplot()
# Use geom_dag_label() for adjusted variables to get a box around them.
coordinates(valves) <- list(x = c(A = 0, B = 1, C = 2, D = 3),
                            y = c(A = 0, B = 0, C = 0, D = 0))
cvalves_dat <- control_for(valves, "C", activate_colliders = FALSE)
cvalves_adj <- filter(cvalves_dat, adjusted == "adjusted")
cvalves_unadj <- filter(cvalves_dat, adjusted == "unadjusted")

# plot DAG with ggplot() to allow for boxes and curved arrows
# Uncomment coord_equal() to get a better-looking DAG in an R plot window.
(ggplot(cvalves_dat, aes(x = x, y = y, xend = xend, yend = yend))
  + theme_dag_gray_grid()
  # + coord_equal()
  + ylim(-0.1, 0.6)
  + geom_dag_label(data = cvalves_adj, parse = TRUE, size = 8, fill = NA,
                   label.padding = unit(0.15, "lines"))
  + geom_dag_text(data = cvalves_unadj, parse = TRUE,
                  color = "black", size = 8)
  + geom_dag_edges_arc(curvature = c(0.4, 0, 0))
)
Figure 15.5: Directed acyclic graph (DAG) to illustrate conditionally open and closed paths. The box around the variable \(C\) indicates conditioning on \(C\).
dag-cvalves-paths.R
## Conditionally open and closed paths on a DAG

# load R packages
library(dagitty)
library(ggdag)

# define DAG
valves <- dagitty('dag{
  A -> C
  B -> C -> D
}')

# path from A to B opened by conditioning on the collider at C
# The third argument Z is a variable or vector of variables to condition on.
isCollider(valves, "A", "C", "B")
paths(valves, "A", "B", Z = "C")
dconnected(valves, "A", "B", "C")
dseparated(valves, "A", "B", "C")

# path from A to B opened by conditioning on D (descendant of collider C)
isCollider(valves, "A", "C", "D")
paths(valves, "A", "B", "D")
dconnected(valves, "A", "B", "D")
dseparated(valves, "A", "B", "D")

# path from B to D closed by conditioning on non-collider at C
isCollider(valves, "B", "D", "C")
paths(valves, "B", "D", "C")
dconnected(valves, "B", "D", "C")
dseparated(valves, "B", "D", "C")

# conditional independence relations implied by the DAG
impliedConditionalIndependencies(valves)

The same rules apply if we condition on multiple variables simultaneously. Conditioning on \(V_1, \ldots, V_k\) means that we look at the joint distribution of the other variables in the subset of our population \(\Omega\) with \(V_1(\omega) = v_1, \ldots, V_k(\omega) = v_k\). Variables that are d-separated when we condition on a variable or set of variables \(V\) are conditionally independent given \(V\).

15.3 Confounding and selection bias

The fact that association does not imply causation can be explained very simply using DAGs. An association between variables \(A\) and \(B\) (where we assume that \(A\) comes before \(B\)) means that there is at least open path between them on any causal DAG that includes both variables. If all of the open paths from \(A\) to \(B\) are causal, then this association represents a causal effect of \(A\) on \(B\) in the population where the association was observed. If some of these open paths are noncausal, the association does not accurately represent a causal effect in the population.

In the design and analysis of an epidemiologic study, our goal is to close all non-causal pathways from exposure \(X\) to disease outcome \(D\) while keeping all causal pathways open. This almost always require measuring variables other than \(X\) and \(D\).

15.3.1 Confounding and open backdoor paths

15.3.2 Selection bias and colliders

15.3.3 Simpson’s paradox revisited

Code
simpson-dag.R
## DAG for Simpson's paradox (Simpson, JRSSB, 1951)

# load R packages
library(dagitty)
library(ggdag)
library(ggplot2)
library(ggraph)   # for label_rect()

# define the DAG
simpson <- dagitty('dag{
  "Sex" -> "Death"
  "Sex" -> "Selection"
  "Sex" -> "Treatment"
  "Treatment" -> "Death"
}')

# generate tidy DAG data
coordinates(simpson) <- list(
  x = c("Sex" = 0, "Selection" = 1, "Treatment" = 1, "Death" = 2),
  y = c("Sex" = 0, "Selection" = -0.5, "Treatment" = 0, "Death" = 0))
# simpson_dat <- tidy_dagitty(simpson)
simpson_dat <- control_for(simpson, "Selection", activate_colliders = FALSE)
simpson_adj <- filter(simpson_dat, adjusted == "adjusted")
simpson_unadj <- filter(simpson_dat, adjusted == "unadjusted")

# plot the DAG using ggplot() to allow curved arrows
# Uncomment "+ coord_equal()" to get a better-looking plot in an R window.
# The start_cap and end_cap arguments keep the arrows from covering the text.
# The size for label_rect should be 3.57 * the text font size.
(ggplot(simpson_dat, aes(x = x, y = y, xend = xend, yend = yend))
  + theme_dag_gray_grid()
  + coord_cartesian(ylim = c(-0.6, 0.6))
  + geom_text(aes(label = name), data = simpson_unadj, parse = TRUE,
              size = 8, col = "black")
  + geom_dag_label(data = simpson_adj, parse = TRUE, size = 8, fill = NA,
                   label.padding = unit(0.1, "lines"))
  + geom_dag_edges_arc(aes(start_cap = label_rect(name, fontsize = 8 * 3.57),
                           end_cap = label_rect(to, fontsize = 8 * 3.57)),
                       curvature = c(0.5, 0, 0, 0), inherit = TRUE)
)
Figure 15.6: Directed acyclic graph (DAG) for the example from Simpson (1951).

15.3.4 Berkson’s bias


  1. Edward H. Simpson (1922–2019) was a British civil servant who grew up in Northern Ireland and became interested in statistics while working as a codebreaker at Bletchley Park in World War II. He went on to earn a PhD in mathematical statistics at the University of Cambridge in 1946 before joining the UK Ministry of Education.↩︎

  2. Sometimes the ancestors and descendants of a variable \(V\) are defined to include \(V\) itself. The proper ancestors and proper descendants of \(V\) do not include \(V\).↩︎

  3. Generating an independence between two variables that is not implied by the DAG requires us to find combinations of parameters that solve a system of equations. When the set of solutions has Lebesgue measure zero in the space of possible parameters, the dependencies implied by the DAG occur in almost all joint distributions consistent with the DAG.↩︎