Skip to content

Commit 7a21602

Browse files
update other uses of smd, remove survey and tableone where not needed
1 parent e32df98 commit 7a21602

15 files changed

+112
-130
lines changed

exercises/01-whole-game-exercises.Rmd

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ library(broom)
99
library(rsample)
1010
library(ggdag)
1111
library(causaldata)
12-
library(survey)
13-
library(tableone)
1412
```
1513

1614
## Causal Modeling: Whole Game
@@ -194,45 +192,39 @@ The main goal here is to *break* the non-causal associations between quitting sm
194192

195193
Standardized mean differences (SMD) are a simple measurement of differences that work across variable types. In general, the closer to 0 we are, the better job we have done eliminating the non-causal relationships we drew in our DAG. Note that low SMDs for everything we adjust for does *not* mean that there is not something else that might confound our study. Unmeasured confounders or misspecified DAGs can still distort our effects, even if our SMDs look great!
196194

197-
We'll use the {survey} and {tableone} package to calculate the SMDs, then visualize them.
195+
We'll use the {smd}t package to calculate the SMDs, then visualize them.
198196

199197
```{r}
200-
svy_des <- svydesign(
201-
ids = ~ 1,
202-
data = nhefs_complete_uc,
203-
weights = ~ wts)
204-
205-
smd_table_unweighted <- CreateTableOne(
206-
vars = c("sex", "race", "age", "education", "smokeintensity", "smokeyrs",
207-
"exercise", "active", "wt71"),
208-
strata = "qsmk",
209-
data = nhefs_complete_uc,
210-
test = FALSE)
211-
212-
smd_table <- svyCreateTableOne(
213-
vars = c("sex", "race", "age", "education", "smokeintensity", "smokeyrs",
214-
"exercise", "active", "wt71"),
215-
strata = "qsmk",
216-
data = svy_des,
217-
test = FALSE)
218-
219-
220-
plot_df <- data.frame(
221-
var = rownames(ExtractSmd(smd_table)),
222-
Unadjusted = as.numeric(ExtractSmd(smd_table_unweighted)),
223-
Weighted = as.numeric(ExtractSmd(smd_table))) %>%
224-
pivot_longer(-var, names_to = "Method", values_to = "SMD")
198+
vars <- c("sex", "race", "age", "education", "smokeintensity", "smokeyrs",
199+
"exercise", "active", "wt71")
200+
201+
plot_df <- nhefs_complete_uc %>%
202+
summarise(
203+
across(
204+
all_of(vars),
205+
list(
206+
unweighted = ~smd(.x, qsmk)$estimate,
207+
weighted = ~smd(.x, qsmk, wts)$estimate
208+
)
209+
)
210+
) %>%
211+
pivot_longer(
212+
everything(),
213+
values_to = "SMD",
214+
names_to = c("variable", "Method"),
215+
names_sep = "_"
216+
)
225217
226218
ggplot(
227219
data = plot_df,
228-
mapping = aes(x = var, y = SMD, group = Method, color = Method)
220+
aes(x = abs(SMD), y = variable, group = Method, color = Method)
229221
) +
230-
geom_line() +
222+
geom_line(orientation = "y") +
231223
geom_point() +
232-
geom_hline(yintercept = 0.1, color = "black", size = 0.1) +
233-
coord_flip() +
234-
theme_minimal()
235-
224+
geom_vline(xintercept = 0.1, color = "black", size = 0.1) +
225+
theme_minimal() +
226+
scale_color_manual(values = c("grey85", "#00BFC4")) +
227+
xlim(0, .3)
236228
```
237229

238230
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.

exercises/05-pscores-diagnostics-exercises.Rmd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ output: html_document
77
```{r}
88
library(tidyverse)
99
library(survey)
10-
library(tableone)
1110
library(broom)
1211
library(causaldata)
1312
```

exercises/06-outcome-model-exercises.Rmd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ output: html_document
66

77
```{r}
88
library(tidyverse)
9-
library(survey)
10-
library(tableone)
119
library(broom)
1210
library(causaldata)
1311
library(rsample)

slides/raw/01-causal_modeling_whole_game.Rmd

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ class: inverse
2222
options(htmltools.dir.version = FALSE, tibble.max_extra_cols = 6, tibble.width = 60)
2323
knitr::opts_chunk$set(warning = FALSE, message = FALSE, fig.align = "center", dpi = 320, fig.height = 4)
2424
library(tidyverse)
25+
library(smd)
2526
library(broom)
2627
library(rsample)
2728
library(ggdag)
2829
library(causaldata)
29-
library(survey)
30-
library(tableone)
3130
```
3231

3332
# Broad strokes
@@ -260,60 +259,53 @@ ggplot(nhefs_complete_uc, aes(wts)) +
260259
---
261260

262261
```{r, echo=FALSE, fig.height=5.5}
263-
svy_des <- svydesign(
264-
ids = ~ 1,
265-
data = nhefs_complete_uc,
266-
weights = ~ wts)
267-
268-
smd_table_unweighted <- CreateTableOne(
269-
vars = c("sex", "race", "age", "education", "smokeintensity", "smokeyrs",
270-
"exercise", "active", "wt71"),
271-
strata = "qsmk",
272-
data = nhefs_complete_uc,
273-
test = FALSE)
274-
275-
smd_table <- svyCreateTableOne(
276-
vars = c("sex", "race", "age", "education", "smokeintensity", "smokeyrs",
277-
"exercise", "active", "wt71"),
278-
strata = "qsmk",
279-
data = svy_des,
280-
test = FALSE)
281-
282-
plot_df <- data.frame(
283-
var = rownames(ExtractSmd(smd_table)),
284-
Unadjusted = as.numeric(ExtractSmd(smd_table_unweighted)),
285-
Weighted = as.numeric(ExtractSmd(smd_table))) %>%
286-
pivot_longer(-var, names_to = "Method", values_to = "SMD")
262+
vars <- c("sex", "race", "age", "education", "smokeintensity", "smokeyrs",
263+
"exercise", "active", "wt71")
264+
265+
plot_df <- nhefs_complete_uc %>%
266+
summarise(
267+
across(
268+
all_of(vars),
269+
list(
270+
unweighted = ~smd(.x, qsmk)$estimate,
271+
weighted = ~smd(.x, qsmk, wts)$estimate
272+
)
273+
)
274+
) %>%
275+
pivot_longer(
276+
everything(),
277+
values_to = "SMD",
278+
names_to = c("variable", "Method"),
279+
names_sep = "_"
280+
)
287281
```
288282

289283

290284
```{r, echo=FALSE, fig.height=5.5}
291285
ggplot(
292-
data = plot_df %>% filter(Method == "Unadjusted"),
293-
mapping = aes(x = var, y = SMD, group = Method, color = Method)
286+
data = plot_df %>% filter(Method == "unweighted"),
287+
aes(x = abs(SMD), y = variable, group = Method, color = Method)
294288
) +
295-
geom_line() +
289+
geom_line(orientation = "y") +
296290
geom_point() +
297-
geom_hline(yintercept = 0.1, color = "black", size = 0.1) +
298-
coord_flip() +
291+
geom_vline(xintercept = 0.1, color = "black", size = 0.1) +
299292
theme_minimal() +
300-
ylim(0, .3)
293+
xlim(0, .3)
301294
```
302295

303296
---
304297

305298
```{r, echo=FALSE, fig.height=5.5}
306299
ggplot(
307300
data = plot_df,
308-
mapping = aes(x = var, y = SMD, group = Method, color = Method)
301+
aes(x = abs(SMD), y = variable, group = Method, color = Method)
309302
) +
310-
geom_line() +
303+
geom_line(orientation = "y") +
311304
geom_point() +
312-
geom_hline(yintercept = 0.1, color = "black", size = 0.1) +
313-
coord_flip() +
305+
geom_vline(xintercept = 0.1, color = "black", size = 0.1) +
314306
theme_minimal() +
315307
scale_color_manual(values = c("grey85", "#00BFC4")) +
316-
ylim(0, .3)
308+
xlim(0, .3)
317309
```
318310

319311
---

0 commit comments

Comments
 (0)