DAGs
In-class example
Here’s the code we’ll be using in class. Download it and store it with the rest of your materials for this course. If simply clicking doesn’t trigger download, you should right-click and select “save link as…”.
Making DAGs with ggdag()
To make DAGs we use dagify()
from the ggdag
package. Note the basic format:
- Y ~ A + B + C + … = think of this as “Y is caused by A, B, C, …”.
- you need to specify the treatment variable with
exposure
(another word for “treatment”) and the outcome variable withoutcome
library(ggdag)
library(tidyverse)
# make a dag
dag = dagify(Y ~ X + A + B + C,
X ~ A,
A ~ B + C, exposure = "X", outcome = "Y")
We can then plot with ggdag
:
# plot it
ggdag(dag)
We can make prettier in the following ways (but no need):
ggdag(dag) + theme_dag()
We can use ggdag_paths
to identify front and backdoor paths from treatment to outcome:
ggdag_paths(dag)
We can use ggdag_adjustment_set
to see what variables we need to control for:
ggdag_adjustment_set(dag)
We can also use words instead of letters:
## made up exqmple: shuttle service --> turnout
dag = dagify(turnout ~ shuttle + income + distance_poll + schedule_flex,
income ~ job + location,
distance_poll ~ location + car,
shuttle ~ cost + partisan + location,
exposure = "shuttle", outcome = "turnout")
If we want to use labels instead of text to make it easier to read:
ggdag(dag, use_labels = "name", text = FALSE) + theme_dag()