Skip to content

Commit 62ed106

Browse files
Merge pull request #50 from r-causal/cleanup-post-nyc
2 parents 872e880 + dbf53b4 commit 62ed106

26 files changed

+141
-38
lines changed

exercises/05-pscores-exercises.qmd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ format: html
44
---
55

66
```{r}
7+
#| label: setup
78
library(tidyverse)
89
library(broom)
910
library(touringplans)
@@ -20,7 +21,7 @@ We are interested in examining the relationship between whether there were "Extr
2021

2122
Below is a proposed DAG for this question.
2223

23-
*Knit this document to see the DAG or refer to the slides*.
24+
*Render this document to see the DAG or refer to the slides*.
2425

2526
```{r}
2627
set.seed(1234)
@@ -82,7 +83,7 @@ Here's a data dictionary of the variables we need in the `seven_dwarfs` data set
8283

8384
## Your Turn
8485

85-
*After updating the code chunks below, change `eval = TRUE` before knitting.*
86+
*After updating the code chunks below, change `eval: true` before rendering*
8687

8788
Now, fit a propensity score model for `extra_magic_morning` using the above proposed confounders.
8889

exercises/06-pscores-weighting-exercises.qmd renamed to exercises/06-pscores-using-exercises.qmd

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
---
2-
title: "Propensity Score Weighting"
2+
title: "Using Propensity Scores"
33
format: html
44
---
55

66

7-
```{r setup}
7+
```{r}
8+
#| label: setup
89
library(tidyverse)
910
library(broom)
1011
library(touringplans)
@@ -32,7 +33,7 @@ seven_dwarfs_prop <- propensity_model |>
3233

3334
## Your Turn 1 (Matching)
3435

35-
_After updating the code chunks below, change `eval = TRUE` before knitting._
36+
*After updating the code chunks below, change `eval: true` before rendering*
3637

3738
Create at "matched" data set using the same propensity score model as above and a caliper of 0.2.
3839

@@ -51,7 +52,7 @@ matched_df <- ___(matched_dwarfs)
5152

5253
## Your Turn 2 (Weighting)
5354

54-
_After updating the code chunks below, change `eval = TRUE` before knitting._
55+
*After updating the code chunks below, change `eval: true` before rendering*
5556

5657
Add the ATE weights to the data frame, `seven_dwarfs_prop`
5758

@@ -76,16 +77,6 @@ Stretch Goal 2:
7677

7778
Update the code below to examine the distribution of the weighted sample. **HINT** the part that needs to be updated is the `weight` parameter in two of the `geom_mirror_histogram()` call.
7879

79-
```{r}
80-
#| eval: false
81-
seven_dwarfs_prop_wide <- seven_dwarfs_prop |>
82-
pivot_wider(
83-
names_from = extra_magic_morning,
84-
values_from = .fitted,
85-
names_prefix = "extra_magic_morning_p"
86-
)
87-
```
88-
8980

9081
```{r}
9182
#| eval: false

exercises/07-pscores-diagnostics-exercises.qmd

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ format: html
55

66

77
```{r}
8+
#| label: setup
89
library(tidyverse)
910
library(broom)
1011
library(touringplans)
1112
library(halfmoon)
13+
library(propensity)
1214
```
1315

1416
We are interested in examining the relationship between whether there were "Extra Magic Hours" in the morning (the **exposure**) and the average wait time for the Seven Dwarfs Mine Train the same day between 9am and 10am (the **outcome**).
@@ -28,12 +30,12 @@ propensity_model <- glm(
2830
2931
seven_dwarfs_ps <- propensity_model |>
3032
augment(type.predict = "response", data = seven_dwarfs) |>
31-
mutate(w_ate = 1 / ifelse(extra_magic_morning == 0, 1 - .fitted, .fitted))
33+
mutate(w_ate = wt_ate(.fitted, extra_magic_morning))
3234
```
3335

3436
## Your Turn 1
3537

36-
_After updating the code chunks below, change `eval = TRUE` before knitting._
38+
*After updating the code chunks below, change `eval: true` before rendering*
3739

3840
Calculate the standardized mean differences with and without weights
3941

exercises/08-outcome-model-exercises.qmd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ format: html
55

66

77
```{r}
8+
#| label: setup
89
library(tidyverse)
910
library(broom)
1011
library(touringplans)
@@ -18,7 +19,7 @@ We are interested in examining the relationship between whether there were "Extr
1819

1920
## Your turn
2021

21-
_After updating the code chunks below, change `eval = TRUE` before knitting._
22+
*After updating the code chunks below, change `eval: true` before rendering*
2223

2324
Create a function called `ipw_fit` that fits the propensity score model from Exercise 03, incorporates the ATE weights calculated in Exercise 04, and fits a weighted outcome model.
2425

exercises/09-continuous-exposures.qmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ format: html
88
library(tidyverse)
99
library(broom)
1010
library(touringplans)
11+
library(propensity)
1112
```
1213

1314
For this set of exercises, we'll use propensity scores for continuous exposures.

exercises/10-g-computation-exercises.qmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ format: html
88
library(tidyverse)
99
library(broom)
1010
library(touringplans)
11+
library(propensity)
1112
1213
seven_dwarfs <- seven_dwarfs_train_2018 |>
1314
filter(hour == 9)

exercises/11-tipr.qmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ format: html
44
---
55

66
```{r}
7+
#| label: setup
78
library(tipr)
89
```
910

exercises/13-quartets.qmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ format: html
44
---
55

66
```{r}
7+
#| label: setup
78
library(quartets)
89
```
910

slides/raw/01-causal_modeling_whole_game.html

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ <h1 class="title">Causal Modeling in R: Whole Game</h1>
406406
<section id="section" class="slide level2" data-background-color="#23373B">
407407
<h2></h2>
408408
<ol type="1">
409-
<li class="fragment">Specify causal question</li>
409+
<li class="fragment">Specify causal question (e.g.&nbsp;target trial)</li>
410410
<li class="fragment">Draw assumptions (causal diagram)</li>
411411
<li class="fragment">Model assumptions (e.g.&nbsp;propensity score)</li>
412412
<li class="fragment">Analyze propensities (diagnostics)</li>
@@ -493,6 +493,10 @@ <h3 id="what-do-i-need-to-control-for">What do I need to control for?</h3>
493493

494494

495495
<img data-src="01-causal_modeling_whole_game_files/figure-revealjs/unnamed-chunk-7-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
496+
<section class="slide level2">
497+
498+
499+
<img data-src="01-causal_modeling_whole_game_files/figure-revealjs/unnamed-chunk-8-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
496500
<section id="multivariable-regression-whats-the-association" class="slide level2">
497501
<h2>Multivariable regression: what’s the association?</h2>
498502
<div class="cell" data-layout-align="center">
@@ -557,23 +561,23 @@ <h3 id="diagnose-your-model-assumptions"><strong>diagnose your model assumptions
557561
<section id="whats-the-distribution-of-weights" class="slide level2">
558562
<h2>What’s the distribution of weights?</h2>
559563

560-
<img data-src="01-causal_modeling_whole_game_files/figure-revealjs/unnamed-chunk-11-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
564+
<img data-src="01-causal_modeling_whole_game_files/figure-revealjs/unnamed-chunk-12-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
561565
<section id="what-are-the-weights-doing-to-the-sample" class="slide level2">
562566
<h2>What are the weights doing to the sample?</h2>
563567

564-
<img data-src="01-causal_modeling_whole_game_files/figure-revealjs/unnamed-chunk-12-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
568+
<img data-src="01-causal_modeling_whole_game_files/figure-revealjs/unnamed-chunk-13-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
565569
<section id="what-are-the-weights-doing-to-the-sample-1" class="slide level2">
566570
<h2>What are the weights doing to the sample?</h2>
567571

568-
<img data-src="01-causal_modeling_whole_game_files/figure-revealjs/unnamed-chunk-13-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
572+
<img data-src="01-causal_modeling_whole_game_files/figure-revealjs/unnamed-chunk-14-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
569573
<section class="slide level2">
570574

571575

572-
<img data-src="01-causal_modeling_whole_game_files/figure-revealjs/unnamed-chunk-15-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
576+
<img data-src="01-causal_modeling_whole_game_files/figure-revealjs/unnamed-chunk-16-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
573577
<section class="slide level2">
574578

575579

576-
<img data-src="01-causal_modeling_whole_game_files/figure-revealjs/unnamed-chunk-16-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
580+
<img data-src="01-causal_modeling_whole_game_files/figure-revealjs/unnamed-chunk-17-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
577581
<section id="section-8" class="slide level2 center" data-background-color="#23373B">
578582
<h2></h2>
579583
<h3 id="estimate-the-causal-effects"><strong>estimate the causal effects</strong></h3>
@@ -712,7 +716,7 @@ <h2>Using {rsample} to bootstrap our causal effect</h2>
712716
</div></section><section id="section-10" class="slide level2 center">
713717
<h2></h2>
714718

715-
<img data-src="01-causal_modeling_whole_game_files/figure-revealjs/unnamed-chunk-25-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
719+
<img data-src="01-causal_modeling_whole_game_files/figure-revealjs/unnamed-chunk-26-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
716720
<section id="section-11" class="slide level2 center" data-background-color="#23373B">
717721
<h2></h2>
718722
<h3 id="our-causal-effect-estimate-3.5-kg-95-ci-2.4-kg-4.4-kg"><em>Our causal effect estimate: <strong>3.5 kg (95% CI 2.4 kg, 4.4 kg)</strong></em></h3>
@@ -724,8 +728,8 @@ <h3 id="review-the-quarto-file-later"><strong>Review the Quarto file… later!</
724728
<section id="resources" class="slide level2" data-background-color="#23373B">
725729
<h2>Resources</h2>
726730
<h3 id="causal-inference-comprehensive-text-on-causal-inference.-free-online."><a href="https://www.hsph.harvard.edu/miguel-hernan/causal-inference-book/">Causal Inference</a>: Comprehensive text on causal inference. Free online.</h3>
727-
<h3 id="causal-inference-notebook-r-code-to-go-along-with-causal-inference"><a href="http://causalinferencebookr.netlify.com">Causal Inference Notebook</a>: R code to go along with Causal Inference</h3>
728731
<h3 id="bootstrap-confidence-intervals-with-rsample"><a href="https://rsample.tidymodels.org/articles/Applications/Intervals.html">Bootstrap confidence intervals with {rsample}</a></h3>
732+
<h3 id="r-causal-our-github-org-with-r-packages-and-examples"><a href="https://github.com/r-causal">R-causal</a>: Our GitHub org with R packages and examples</h3>
729733
<div class="footer footer-default">
730734

731735
</div>

slides/raw/01-causal_modeling_whole_game.qmd

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,17 @@ nhefs_complete_uc |>
9292

9393
```{r}
9494
#| echo: false
95-
#| fig.height: 5.5
95+
#| fig.width: 9
9696
library(ggdag)
9797
# set up DAG
98+
coords <- time_ordered_coords(
99+
list(
100+
c("active", "age", "education", "exercise", "race", "sex", "smokeintensity",
101+
"smokeyrs", "wt71"),
102+
"qsmk",
103+
"wt82_71"
104+
)
105+
)
98106
smk_wt_dag <- dagify(
99107
# specify causes of quitting smoking and weight gain:
100108
qsmk ~ sex + race + age + education +
@@ -104,6 +112,7 @@ smk_wt_dag <- dagify(
104112
# specify causal question:
105113
exposure = "qsmk",
106114
outcome = "wt82_71",
115+
coords = coords,
107116
# set up labels:
108117
# here, I'll use the same variable names as the data set, but I'll label them
109118
# with clearer names
@@ -128,7 +137,7 @@ smk_wt_dag <- dagify(
128137
"smokeyrs" = "yrs of\nsmoking"
129138
)
130139
) |>
131-
tidy_dagitty(layout = "star")
140+
tidy_dagitty()
132141
133142
smk_wt_dag |>
134143
filter(name %in% c("qsmk", "wt82_71")) |>
@@ -142,7 +151,7 @@ smk_wt_dag |>
142151

143152
```{r}
144153
#| echo: false
145-
#| fig.height: 5.5
154+
#| fig.width: 9
146155
smk_wt_dag |>
147156
ggdag(text = FALSE, use_labels = "label") +
148157
ylim(min(smk_wt_dag$data$y) - 0.25, max(smk_wt_dag$data$y) + 0.25) +
@@ -154,6 +163,80 @@ smk_wt_dag |>
154163

155164
### What do I need to control for?
156165

166+
---
167+
168+
```{r}
169+
#| echo: false
170+
#| fig.width: 15
171+
library(ggokabeito)
172+
glyph <- function(data, params, size) {
173+
data$shape <- 15
174+
data$size <- 12
175+
ggplot2::draw_key_point(data, params, size)
176+
}
177+
178+
smk_wt_dag |>
179+
dag_paths() |>
180+
mutate(
181+
effects = case_when(
182+
set == "1" & path == "open path" ~ "true effect",
183+
path == "open path" ~ "confounding effect",
184+
TRUE ~ NA_character_
185+
),
186+
effects = factor(effects, c("true effect", "confounding effect"))
187+
) |>
188+
ggplot(aes(x = x, y = y, xend = xend, yend = yend, color = effects, alpha = path)) +
189+
geom_dag_edges(aes(edge_alpha = path, edge_colour = effects), show.legend = FALSE) +
190+
geom_dag_point(
191+
data = function(.x) dplyr::filter(.x, is.na(path)),
192+
key_glyph = glyph
193+
) +
194+
geom_dag_point(
195+
data = function(.x) dplyr::filter(.x, !is.na(path)),
196+
key_glyph = glyph
197+
) +
198+
facet_wrap(vars(fct_inorder(factor(set)))) +
199+
expand_plot(
200+
expand_x = expansion(c(0.25, 0.25)),
201+
expand_y = expansion(c(0.1, 0.1))
202+
) +
203+
theme_dag() +
204+
theme(
205+
legend.position = "top",
206+
legend.spacing.x = unit(8, "mm"),
207+
legend.text = element_text(size = rel(2.5)),
208+
legend.box.margin = margin(b = 20),
209+
strip.text = element_blank()
210+
) +
211+
coord_cartesian(clip = "off") +
212+
scale_alpha_manual(
213+
drop = FALSE,
214+
values = c("open path" = 1),
215+
na.value = .5,
216+
breaks = "open path"
217+
) +
218+
ggraph::scale_edge_alpha_manual(
219+
drop = FALSE,
220+
values = c("open path" = 1),
221+
na.value = .5,
222+
breaks = "open path"
223+
) +
224+
scale_color_okabe_ito(
225+
name = NULL,
226+
na.value = "grey90",
227+
order = c(3, 6),
228+
breaks = c("true effect", "confounding effect")
229+
) +
230+
scale_edge_color_okabe_ito(
231+
name = NULL,
232+
na.value = "grey90",
233+
order = c(3, 6),
234+
breaks = c("true effect", "confounding effect")
235+
) +
236+
guides(alpha = "none", edge_alpha = "none")
237+
```
238+
239+
157240
---
158241

159242
```{r}
@@ -291,7 +374,7 @@ plot_df <- nhefs_complete_uc |>
291374
#| echo: false
292375
#| fig.height: 5.5
293376
ggplot(
294-
data = plot_df |> filter(method == "wts"),
377+
data = plot_df |> filter(method == "observed"),
295378
aes(x = abs(smd), y = variable, group = method, color = method, fill = method)
296379
) +
297380
geom_line(orientation = "y", size = 1) +

0 commit comments

Comments
 (0)