Skip to content

Commit 8253c60

Browse files
Merge pull request #51 from r-causal/update_dags
2 parents 62ed106 + 08f4871 commit 8253c60

15 files changed

+310
-14
lines changed

exercises/04-dags-exercises.qmd

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,33 @@ cancer ~ ______
7979
cancer ~ ______
8080
```
8181

82+
## Your Turn 4
83+
84+
Time-ordering your DAGs is incredibly useful, because it makes it cleaner, easier to read, and easier to understand what to control for --- and *not* control for.
85+
86+
ggdag includes several ways to manually specify coordinates, including `time_ordered_coords()`. This functional automatically determines the time ordering of the DAG based on the causal relationships. After all, something that causes something else *must* come before, otherwise we'd be violating the time-space continuum.
87+
88+
Recreate the DAG we've been working with using `time_ordered_coords()`, then visualize the DAG. You don't need to use any arguments for this function, so `coords = time_ordered_coords()` will do.
89+
90+
```{r}
91+
coffee_cancer_dag_to <- dagify(
92+
cancer ~ smoking,
93+
smoking ~ addictive,
94+
coffee ~ addictive,
95+
exposure = "coffee",
96+
outcome = "cancer",
97+
coords = ________,
98+
labels = c(
99+
"coffee" = "Coffee",
100+
"cancer" = "Lung Cancer",
101+
"smoking" = "Smoking",
102+
"addictive" = "Addictive \nBehavior"
103+
)
104+
)
105+
106+
_____(_____, use_labels = TRUE, use_text = FALSE)
107+
```
108+
82109
# Take aways
83110

84111
* Draw your assumptions with DAGs! Use `dagify()` to specify them and `ggdag()` and friends to draw them.

slides/raw/04-dags.html

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ <h2><em>Your Turn 1</em> (<strong><code>04-dags-exercises.qmd</code></strong>)</
523523
<h3 id="specify-a-dag-with-dagify.-write-your-assumption-that-smoking-causes-cancer-as-a-formula.">Specify a DAG with <code>dagify()</code>. Write your assumption that <code>smoking</code> causes <code>cancer</code> as a formula.</h3>
524524
<h3 id="were-going-to-assume-that-coffee-does-not-cause-cancer-so-theres-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.">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).</h3>
525525
<h3 id="plot-the-dag-using-ggdag">Plot the DAG using <code>ggdag()</code></h3>
526-
<p></p><div class="countdown" id="timer_98fb95a5" data-update-every="1" tabindex="0" style="right:0;bottom:0;"> <div class="countdown-controls"><button class="countdown-bump-down"></button><button class="countdown-bump-up">+</button></div> <code class="countdown-time"><span class="countdown-digits minutes">05</span><span class="countdown-digits colon">:</span><span class="countdown-digits seconds">00</span></code> </div><p></p>
526+
<p></p><div class="countdown" id="timer_48e176ed" data-update-every="1" tabindex="0" style="right:0;bottom:0;"> <div class="countdown-controls"><button class="countdown-bump-down"></button><button class="countdown-bump-up">+</button></div> <code class="countdown-time"><span class="countdown-digits minutes">05</span><span class="countdown-digits colon">:</span><span class="countdown-digits seconds">00</span></code> </div><p></p>
527527
</section>
528528
<section id="your-turn-1-02-dags-exercises.qmd" class="slide level2">
529529
<h2><em>Your Turn 1</em> (<code>02-dags-exercises.qmd</code>)</h2>
@@ -579,7 +579,7 @@ <h2><code>ggdag_paths()</code></h2>
579579
<h2><em>Your Turn 2</em></h2>
580580
<h3 id="call-tidy_dagitty-on-coffee_cancer_dag-to-create-a-tidy-dag-then-pass-the-results-to-dag_paths.-whats-different-about-these-data">Call <code>tidy_dagitty()</code> on <code>coffee_cancer_dag</code> to create a tidy DAG, then pass the results to <code>dag_paths()</code>. What’s different about these data?</h3>
581581
<h3 id="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.">Plot the open paths with <code>ggdag_paths()</code>. (Just give it <code>coffee_cancer_dag</code> rather than using <code>dag_paths()</code>; the quick plot function will do that for you.) Remember, since we assume there is <em>no</em> causal path from coffee to lung cancer, any open paths must be confounding pathways.</h3>
582-
<p></p><div class="countdown" id="timer_4e8ed7b7" data-update-every="1" tabindex="0" style="right:0;bottom:0;"> <div class="countdown-controls"><button class="countdown-bump-down"></button><button class="countdown-bump-up">+</button></div> <code class="countdown-time"><span class="countdown-digits minutes">05</span><span class="countdown-digits colon">:</span><span class="countdown-digits seconds">00</span></code> </div><p></p>
582+
<p></p><div class="countdown" id="timer_831dae61" data-update-every="1" tabindex="0" style="right:0;bottom:0;"> <div class="countdown-controls"><button class="countdown-bump-down"></button><button class="countdown-bump-up">+</button></div> <code class="countdown-time"><span class="countdown-digits minutes">05</span><span class="countdown-digits colon">:</span><span class="countdown-digits seconds">00</span></code> </div><p></p>
583583
</section>
584584
<section id="your-turn-2-1" class="slide level2">
585585
<h2><em>Your Turn 2</em></h2>
@@ -594,13 +594,13 @@ <h2><em>Your Turn 2</em></h2>
594594
# Outcome: cancer
595595
#
596596
# A tibble: 5 × 11
597-
set name x y direction to xend yend
598-
&lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;fct&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;
599-
1 1 addictive 0.494 2.00 -&gt; coff… -0.423 2.81
600-
2 1 addictive 0.494 2.00 -&gt; smok… 1.50 1.11
601-
3 1 cancer 2.41 0.310 &lt;NA&gt; &lt;NA&gt; NA NA
602-
4 1 coffee -0.423 2.81 &lt;NA&gt; &lt;NA&gt; NA NA
603-
5 1 smoking 1.50 1.11 -&gt; canc… 2.41 0.310
597+
set name x y direction to xend yend
598+
&lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;fct&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;
599+
1 1 addictive 0.616 -1.27 -&gt; coff… 0.185 -0.127
600+
2 1 addictive 0.616 -1.27 -&gt; smok… 1.09 -2.52
601+
3 1 cancer 1.52 -3.66 &lt;NA&gt; &lt;NA&gt; NA NA
602+
4 1 coffee 0.185 -0.127 &lt;NA&gt; &lt;NA&gt; NA NA
603+
5 1 smoking 1.09 -2.52 -&gt; canc… 1.52 -3.66
604604
# ℹ 3 more variables: circular &lt;lgl&gt;, label &lt;chr&gt;,
605605
# path &lt;chr&gt;</code></pre>
606606
</div>
@@ -614,6 +614,30 @@ <h2><em>Your Turn 2</em></h2>
614614

615615
</div>
616616
<img data-src="04-dags_files/figure-revealjs/unnamed-chunk-14-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
617+
<section class="slide level2">
618+
619+
620+
<img data-src="04-dags_files/figure-revealjs/unnamed-chunk-15-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
621+
<section class="slide level2">
622+
623+
624+
<img data-src="04-dags_files/figure-revealjs/unnamed-chunk-16-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
625+
<section class="slide level2">
626+
627+
628+
<img data-src="04-dags_files/figure-revealjs/unnamed-chunk-17-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
629+
<section class="slide level2">
630+
631+
632+
<img data-src="04-dags_files/figure-revealjs/unnamed-chunk-18-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
633+
<section class="slide level2">
634+
635+
636+
<img data-src="04-dags_files/figure-revealjs/unnamed-chunk-19-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
637+
<section class="slide level2">
638+
639+
640+
<img data-src="04-dags_files/figure-revealjs/unnamed-chunk-20-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
617641
<section id="closing-backdoor-paths" class="slide level2 large">
618642
<h2>Closing backdoor paths</h2>
619643
<div class="fragment">
@@ -637,7 +661,7 @@ <h2>Identifying adjustment sets</h2>
637661
<div class="cell-output-display">
638662
<div class="quarto-figure quarto-figure-center">
639663
<figure>
640-
<p><img data-src="04-dags_files/figure-revealjs/unnamed-chunk-15-1.png" style="width:80.0%"></p>
664+
<p><img data-src="04-dags_files/figure-revealjs/unnamed-chunk-21-1.png" style="width:80.0%"></p>
641665
</figure>
642666
</div>
643667
</div>
@@ -657,7 +681,7 @@ <h2><em>Your Turn 3</em></h2>
657681
<h3 id="now-that-we-know-the-open-confounding-pathways-sometimes-called-backdoor-paths-we-need-to-know-how-to-close-them-first-well-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.">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.&nbsp;multivariable regression, weighting, or matching for the adjustment sets).</h3>
658682
<h3 id="use-ggdag_adjustment_set-to-visualize-the-adjustment-sets.-add-the-arguments-use_labels-label-and-text-false.">Use <code>ggdag_adjustment_set()</code> to visualize the adjustment sets. Add the arguments <code>use_labels = "label"</code> and <code>text = FALSE</code>.</h3>
659683
<h3 id="write-an-r-formula-for-each-adjustment-set-as-you-might-if-you-were-fitting-a-model-in-lm-or-glm">Write an R formula for each adjustment set, as you might if you were fitting a model in <code>lm()</code> or <code>glm()</code></h3>
660-
<p></p><div class="countdown" id="timer_ff1e9b32" data-update-every="1" tabindex="0" style="right:0;bottom:0;"> <div class="countdown-controls"><button class="countdown-bump-down"></button><button class="countdown-bump-up">+</button></div> <code class="countdown-time"><span class="countdown-digits minutes">05</span><span class="countdown-digits colon">:</span><span class="countdown-digits seconds">00</span></code> </div><p></p>
684+
<p></p><div class="countdown" id="timer_00419a1f" data-update-every="1" tabindex="0" style="right:0;bottom:0;"> <div class="countdown-controls"><button class="countdown-bump-down"></button><button class="countdown-bump-up">+</button></div> <code class="countdown-time"><span class="countdown-digits minutes">05</span><span class="countdown-digits colon">:</span><span class="countdown-digits seconds">00</span></code> </div><p></p>
661685
</section>
662686
<section id="your-turn-3-1" class="slide level2">
663687
<h2><em>Your Turn 3</em></h2>
@@ -674,7 +698,7 @@ <h2><em>Your Turn 3</em></h2>
674698
<div class="cell-output-display">
675699
<div class="quarto-figure quarto-figure-center">
676700
<figure>
677-
<p><img data-src="04-dags_files/figure-revealjs/unnamed-chunk-17-1.png" style="width:80.0%"></p>
701+
<p><img data-src="04-dags_files/figure-revealjs/unnamed-chunk-23-1.png" style="width:80.0%"></p>
678702
</figure>
679703
</div>
680704
</div>
@@ -718,7 +742,7 @@ <h2>Let’s prove it!</h2>
718742
<section id="lets-prove-it-2" class="slide level2">
719743
<h2>Let’s prove it!</h2>
720744

721-
<img data-src="04-dags_files/figure-revealjs/unnamed-chunk-21-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
745+
<img data-src="04-dags_files/figure-revealjs/unnamed-chunk-27-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
722746
<section id="choosing-what-variables-to-include" class="slide level2 large">
723747
<h2>Choosing what variables to include</h2>
724748
<div class="fragment">
@@ -749,7 +773,50 @@ <h2>Common trip ups</h2>
749773
<p><strong>Incorrect functional form for confounders (e.g.&nbsp;BMI often non-linear)</strong></p>
750774
</div>
751775
</section>
752-
<section id="resources-ggdag-vignettes" class="slide level2" data-background-color="#23373B">
776+
<section id="time-ordering" class="slide level2">
777+
<h2>Time-ordering</h2>
778+
779+
<img data-src="04-dags_files/figure-revealjs/unnamed-chunk-28-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
780+
<section id="section-2" class="slide level2 large center middle" data-background-color="#23373B">
781+
<h2></h2>
782+
<h3 id="dont-adjust-for-the-future"><strong>don’t adjust for the future!</strong></h3>
783+
</section>
784+
<section id="your-turn-4" class="slide level2">
785+
<h2><em>Your Turn 4</em></h2>
786+
<p>Recreate the DAG we’ve been working with using <code>time_ordered_coords()</code>, then visualize the DAG. You don’t need to use any arguments for this function, so <code>coords = time_ordered_coords()</code> will do.</p>
787+
</section>
788+
<section id="your-turn-4-1" class="slide level2">
789+
<h2><em>Your Turn 4</em></h2>
790+
<div class="cell" data-layout-align="center">
791+
<div class="sourceCode cell-code" id="cb18" data-code-line-numbers="|7"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1"></a>coffee_cancer_dag_to <span class="ot">&lt;-</span> <span class="fu">dagify</span>(</span>
792+
<span id="cb18-2"><a href="#cb18-2"></a> cancer <span class="sc">~</span> smoking,</span>
793+
<span id="cb18-3"><a href="#cb18-3"></a> smoking <span class="sc">~</span> addictive,</span>
794+
<span id="cb18-4"><a href="#cb18-4"></a> coffee <span class="sc">~</span> addictive,</span>
795+
<span id="cb18-5"><a href="#cb18-5"></a> <span class="at">exposure =</span> <span class="st">"coffee"</span>,</span>
796+
<span id="cb18-6"><a href="#cb18-6"></a> <span class="at">outcome =</span> <span class="st">"cancer"</span>,</span>
797+
<span id="cb18-7"><a href="#cb18-7"></a> <span class="at">coords =</span> <span class="fu">time_ordered_coords</span>(),</span>
798+
<span id="cb18-8"><a href="#cb18-8"></a> <span class="at">labels =</span> <span class="fu">c</span>(</span>
799+
<span id="cb18-9"><a href="#cb18-9"></a> <span class="st">"coffee"</span> <span class="ot">=</span> <span class="st">"Coffee"</span>, </span>
800+
<span id="cb18-10"><a href="#cb18-10"></a> <span class="st">"cancer"</span> <span class="ot">=</span> <span class="st">"Lung Cancer"</span>, </span>
801+
<span id="cb18-11"><a href="#cb18-11"></a> <span class="st">"smoking"</span> <span class="ot">=</span> <span class="st">"Smoking"</span>, </span>
802+
<span id="cb18-12"><a href="#cb18-12"></a> <span class="st">"addictive"</span> <span class="ot">=</span> <span class="st">"Addictive </span><span class="sc">\n</span><span class="st">Behavior"</span></span>
803+
<span id="cb18-13"><a href="#cb18-13"></a> )</span>
804+
<span id="cb18-14"><a href="#cb18-14"></a>)</span>
805+
<span id="cb18-15"><a href="#cb18-15"></a></span>
806+
<span id="cb18-16"><a href="#cb18-16"></a><span class="co">#TODO: UPDATE LABELS ARGS</span></span>
807+
<span id="cb18-17"><a href="#cb18-17"></a><span class="fu">ggdag</span>(coffee_cancer_dag_to, <span class="at">use_labels =</span> <span class="st">"label"</span>, <span class="at">text =</span> <span class="cn">FALSE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
808+
</div>
809+
810+
</section>
811+
<section id="your-turn-4-1-output" class="slide level2 output-location-slide"><h2><em>Your Turn 4</em></h2><div class="cell output-location-slide" data-layout-align="center">
812+
<div class="cell-output-display">
813+
<div class="quarto-figure quarto-figure-center">
814+
<figure>
815+
<p><img data-src="04-dags_files/figure-revealjs/unnamed-chunk-29-1.png" style="width:80.0%"></p>
816+
</figure>
817+
</div>
818+
</div>
819+
</div></section><section id="resources-ggdag-vignettes" class="slide level2" data-background-color="#23373B">
753820
<h2>Resources: ggdag vignettes</h2>
754821
<h3 id="an-introduction-to-ggdag"><a href="https://ggdag.malco.io/articles/intro-to-ggdag.html">An Introduction to ggdag</a></h3>
755822
<h3 id="an-introduction-to-directed-acyclic-graphs"><a href="https://ggdag.malco.io/articles/intro-to-dags.html">An Introduction to Directed Acyclic Graphs</a></h3>

0 commit comments

Comments
 (0)