+ - 0:00:00
Notes for current slide
Notes for next slide

Causal Diagrams in R

2020-07-29 (updated: 2020-07-28)

1 / 38

Draw your causal assumptions with causal directed acyclic graphs (DAGs)

2 / 38

The basic idea

  1. Specify your causal question
  2. Use domain knowledge
  3. Write variables as nodes
  4. Write causal pathways as arrows (edges)
3 / 38

ggdag

4 / 38

5 / 38

6 / 38

7 / 38

8 / 38

Step 1: Specify your DAG

9 / 38

Step 1: Specify your DAG

dagify(
cancer ~ smoking,
coffee ~ smoking
)
9 / 38

Step 1: Specify your DAG

dagify(
cancer ~ smoking,
coffee ~ smoking
)
10 / 38

Step 1: Specify your DAG

dagify(
cancer ~ smoking,
coffee ~ smoking
)
11 / 38

Step 1: Specify your DAG

dagify(
cancer ~ smoking,
coffee ~ smoking
) %>% ggdag()
12 / 38

Step 1: Specify your DAG

13 / 38

Step 1: Specify your DAG

dagify(
cancer ~ smoking + coffee,
coffee ~ smoking
) %>% ggdag()
14 / 38

Step 1: Specify your DAG

15 / 38

Your Turn 1 (02-dags-exercises.Rmd)

Specify a DAG with dagify(). Write your assumption that smoking causes cancer as a formula.

We're going to assume that coffee does not cause cancer, so there's no formula for that. But we still need to declare our causal question. Specify "coffee" as the exposure and "cancer" as the outcome (both in quotations marks).

Plot the DAG using ggdag()

03:00
16 / 38

Your Turn 1 (02-dags-exercises.Rmd)

coffee_cancer_dag <- dagify(
cancer ~ smoking,
smoking ~ addictive,
coffee ~ addictive,
exposure = "coffee",
outcome = "cancer",
labels = c(
"coffee" = "Coffee",
"cancer" = "Lung Cancer",
"smoking" = "Smoking",
"addictive" = "Addictive \nBehavior"
)
)
17 / 38
ggdag(coffee_cancer_dag)

18 / 38

Causal effects and backdoor paths

19 / 38

Causal effects and backdoor paths

Ok, correlation != causation. But why not?

20 / 38

Causal effects and backdoor paths

Ok, correlation != causation. But why not?

We want to know if x -> y...

21 / 38

Causal effects and backdoor paths

Ok, correlation != causation. But why not?

We want to know if x -> y...

But other paths also cause associations

22 / 38

ggdag_paths()

Identify "backdoor" paths

23 / 38

ggdag_paths()

Identify "backdoor" paths

ggdag_paths(smk_wt_dag)
23 / 38

24 / 38

Your Turn 2

Call tidy_dagitty() on coffee_cancer_dag to create a tidy DAG, then pass the results to dag_paths(). What's different about these data?

Plot the open paths with ggdag_paths(). (Just give it coffee_cancer_dag rather than using dag_paths(); the quick plot function will do that for you.) Remember, since we assume there is no causal path from coffee to lung cancer, any open paths must be confounding pathways.

03:00
25 / 38

Your Turn 2

coffee_cancer_dag %>%
tidy_dagitty() %>%
dag_paths()
## # A DAG with 4 nodes and 3 edges
## #
## # Exposure: coffee
## # Outcome: cancer
## #
## # A tibble: 5 x 11
## set name x y direction to xend yend
## <chr> <chr> <dbl> <dbl> <fct> <chr> <dbl> <dbl>
## 1 1 addi… 25.7 28.1 -> coff… 24.5 27.9
## 2 1 addi… 25.7 28.1 -> smok… 27.1 28.2
## 3 1 smok… 27.1 28.2 -> canc… 28.3 28.3
## 4 1 coff… 24.5 27.9 <NA> <NA> NA NA
## 5 1 canc… 28.3 28.3 <NA> <NA> NA NA
## # … with 3 more variables: circular <lgl>, label <chr>,
## # path <chr>
26 / 38
coffee_cancer_dag %>%
ggdag_paths()

27 / 38

Closing backdoor paths

28 / 38

Closing backdoor paths

We need to account for these open, non-causal paths

29 / 38

Closing backdoor paths

We need to account for these open, non-causal paths

Randomization

30 / 38

Closing backdoor paths

We need to account for these open, non-causal paths

Randomization

Stratification, adjustment, weighting, matching, etc.

31 / 38

Identifying adjustment sets

ggdag_adjustment_set(smk_wt_dag)
32 / 38

33 / 38

Your Turn 3

Now that we know the open, confounding pathways (sometimes called "backdoor paths"), we need to know how to close them! First, we'll ask {ggdag} for adjustment sets, then we would need to do something in our analysis to account for at least one adjustment set (e.g. multivariable regression, weighting, or matching for the adjustment sets).

Use ggdag_adjustment_set() to visualize the adjustment sets. Add the arguments use_labels = "label" and text = FALSE.

Write an R formula for each adjustment set, as you might if you were fitting a model in lm() or glm()

03:00
34 / 38

Your Turn 3

ggdag_adjustment_set(
coffee_cancer_dag,
use_labels = "label",
text = FALSE
)
35 / 38

36 / 38

Your Turn 3

cancer ~ coffee + addictive
cancer ~ coffee + smoking
37 / 38

Draw your causal assumptions with causal directed acyclic graphs (DAGs)

2 / 38
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow