Skip to content

Commit 7a9e866

Browse files
update whole game notes
1 parent 7df6529 commit 7a9e866

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

exercises/01-whole-game-exercises.qmd

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ library(ggdag)
1212
library(causaldata)
1313
library(halfmoon)
1414
library(propensity)
15+
library(ggokabeito)
1516
1617
set.seed(1234)
1718
```
@@ -33,13 +34,11 @@ nhefs_complete_uc
3334
Let's look at the distribution of weight gain between the two groups.
3435

3536
```{r}
36-
colors <- c("#E69F00", "#56B4E9")
37-
3837
nhefs_complete_uc |>
3938
ggplot(aes(wt82_71, fill = factor(qsmk))) +
4039
geom_vline(xintercept = 0, color = "grey60", size = 1) +
4140
geom_density(color = "white", alpha = .75, size = .5) +
42-
scale_fill_manual(values = colors) +
41+
scale_color_okabe_ito(order = c(1, 5)) +
4342
theme_minimal() +
4443
theme(legend.position = "bottom") +
4544
labs(
@@ -132,8 +131,8 @@ What do we need to control for to estimate an unbiased effect of quitting smokin
132131
smk_wt_dag |>
133132
ggdag_adjustment_set(text = FALSE, use_labels = "label") +
134133
theme_dag() +
135-
scale_color_manual(values = colors) +
136-
scale_fill_manual(values = colors)
134+
scale_color_okabe_ito(order = c(1, 5)) +
135+
scale_fill_okabe_ito(order = c(1, 5))
137136
```
138137

139138
Let's fit a model with these variables. Note that we'll fit all continuous variables with squared terms, as well, to allow them a bit of flexibility.
@@ -214,11 +213,9 @@ plot_df <- tidy_smd(
214213
215214
ggplot(
216215
data = plot_df,
217-
mapping = aes(x = abs(smd), y = variable, group = weights, color = weights)
216+
mapping = aes(x = abs(smd), y = variable, group = method, color = method)
218217
) +
219-
geom_line(orientation = "y") +
220-
geom_point() +
221-
geom_vline(xintercept = 0.1, color = "black", size = 0.1)
218+
geom_love()
222219
```
223220

224221
These look pretty good! Some variables are better than others, but weighting appears to have done a much better job eliminating these differences than an unadjusted analysis.
@@ -269,10 +266,10 @@ But we have other problem that we need to address. While we're just using `lm()`
269266
```{r}
270267
# also see robustbase, survey, gee, and others
271268
library(estimatr)
272-
ipw_model_robust <- lm_robust( #<<
273-
wt82_71 ~ qsmk, #<<
269+
ipw_model_robust <- lm_robust(
270+
wt82_71 ~ qsmk,
274271
data = nhefs_complete_uc,
275-
weights = wts #<<
272+
weights = wts
276273
)
277274
278275
ipw_estimate_robust <- ipw_model_robust |>
@@ -373,4 +370,3 @@ So, we have a final estimate for our causal effect: on average, a person who qui
373370
# Take aways
374371
* The broad strokes for a causal analysis are: 1) identify your causal question 2) make your assumptions clear 3) check your assumptions as best you can and 4) use the right estimator for the question you're trying to ask. As scientists, we should be able to critique each of these steps, and that's a good thing!
375372
* To create marginal structural models, first fit a propensity model for the weights with the exposure as the outcome. Then, use the inverse of the predicted probabilities as weights in a model with just the outcome and exposure.
376-
* See more at https://causalinferencebookr.netlify.com

0 commit comments

Comments
 (0)