library(tidyverse) # your friend and mine
library(dagitty) # for working with DAGs
library(ggdag) # for drawing DAGs in R
3.2 — DAGs — R Practice
Required Packages
Load all the required packages we will use by running (clicking the green play button) the chunk below:
set.seed(20) # using this number means all "random" generated objects will be identical for all of us!
For each of the following examples:
- Write out all of the causal pathways from
X
(treatment of interest) toY
(outcome of interest). - Identify which variable(s) need to be controlled to estimate the causal effect of
X
onY
. You can usedagitty.net
to help you, but you should start trying to recognize these on your own! - Draw the DAGs in
r
usingggdag
. After setting up the dag withdagify()
(and specifyingexposure
andoutcome
insidedagify
), pipe that intoggdag()
. Try again piping it instead intoggdag_status()
(to highlight what is X and what is Y). Try again piping it instead intoggdag_adjustment_set()
to show what needs to be controlled.
Don’t forget to install ggdag
and dagitty
!
Question 1
Part I
Pathways:
(causal, front door) (not causal, back door)
Part II
Part III
<- dagify(Y ~ X + Z,
dag1 ~ Z,
X exposure = "X",
outcome = "Y")
%>%
dag1 ggdag_status(seed = 1)+
theme_dag_blank()
%>%
dag1 ggdag_paths(seed = 1)+
theme_dag_blank()
%>%
dag1 ggdag_adjustment_set()+
theme_dag_blank()
Question 2
Part I
Pathways:
(causal, front door) (causal, front door)
Part II
Nothing should be controlled for, since
Part III
<- dagify(Y ~ X + M,
dag2 ~ X,
M exposure = "X",
outcome = "Y")
%>%
dag2 ggdag_status(seed = 1)+
theme_dag_blank()
%>%
dag2 ggdag_paths(seed = 1)+
theme_dag_blank()
%>%
dag2 ggdag_adjustment_set()+
theme_dag_blank()
Question 3
Part I
Pathways:
(causal, front door) (not causal, back door) (not causal, back door)
Part II
Backdoor path 3 is closed by the collider at
Alternatively, we could control for
Part III
<- dagify(Y ~ X + Z + B,
dag3 ~ B + A,
Z ~ A,
X exposure = "X",
outcome = "Y")
%>%
dag3 ggdag_status(seed = 1)+
theme_dag_blank()
%>%
dag3 ggdag_paths(seed = 1)+
theme_dag_blank()
%>%
dag3 ggdag_adjustment_set()+
theme_dag_blank()
Question 4
Part I
Pathways:
(causal, front door) (causal, front door) (not causal, back door)
Part II
Path 2 is a front door path we want to leave open. Backdoor path 3 is closed by the collider at
Part III
<- dagify(Y ~ X + B + C,
dag4 ~ B + A,
Z ~ A,
X ~ X,
C exposure = "X",
outcome = "Y")
%>%
dag4 ggdag_status(seed = 1)+
theme_dag_blank()
%>%
dag4 ggdag_paths(seed = 1)+
theme_dag_blank()
%>%
dag4 ggdag_adjustment_set()+
theme_dag_blank()
Question 5
Part I
Pathways:
(causal, front door) (causal, front door) (not causal, back door) (not causal, back door)
Part II
Path 2 is a front door path we want to leave open. Backdoor path 3 is closed by the collider at
Part III
<- dagify(Y ~ X + Z + A,
dag5 ~ X + A + B,
Z ~ A,
B exposure = "X",
outcome = "Y")
%>%
dag5 ggdag_status(seed = 1)+
theme_dag_blank()
%>%
dag5 ggdag_paths(seed = 1)+
theme_dag_blank()
%>%
dag5 ggdag_adjustment_set()+
theme_dag_blank()