You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercises/04-dags-exercises.qmd
+27Lines changed: 27 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -79,6 +79,33 @@ cancer ~ ______
79
79
cancer ~ ______
80
80
```
81
81
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
+
82
109
# Take aways
83
110
84
111
* Draw your assumptions with DAGs! Use `dagify()` to specify them and `ggdag()` and friends to draw them.
<h3id="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>
524
524
<h3id="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>
525
525
<h3id="plot-the-dag-using-ggdag">Plot the DAG using <code>ggdag()</code></h3>
<h3id="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>
581
581
<h3id="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>
<h3id="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. multivariable regression, weighting, or matching for the adjustment sets).</h3>
658
682
<h3id="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>
659
683
<h3id="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>
<sectionid="section-2" class="slide level2 large center middle" data-background-color="#23373B">
781
+
<h2></h2>
782
+
<h3id="dont-adjust-for-the-future"><strong>don’t adjust for the future!</strong></h3>
783
+
</section>
784
+
<sectionid="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
+
<sectionid="your-turn-4-1" class="slide level2">
789
+
<h2><em>Your Turn 4</em></h2>
790
+
<divclass="cell" data-layout-align="center">
791
+
<divclass="sourceCode cell-code" id="cb18" data-code-line-numbers="|7"><preclass="sourceCode numberSource r number-lines code-with-copy"><codeclass="sourceCode r"><spanid="cb18-1"><ahref="#cb18-1"></a>coffee_cancer_dag_to <spanclass="ot"><-</span><spanclass="fu">dagify</span>(</span>
792
+
<spanid="cb18-2"><ahref="#cb18-2"></a> cancer <spanclass="sc">~</span> smoking,</span>
0 commit comments