@@ -47,9 +47,58 @@ smds <- tidy_smd(
47
47
)
48
48
```
49
49
50
+ ## Calculating SMDs
51
+
52
+ ``` {r}
53
+ #| message: false
54
+ #| warning: false
55
+ #| echo: false
56
+ #| eval: true
57
+ library(halfmoon)
58
+ library(tidyverse)
59
+ library(broom)
60
+ library(causaldata)
61
+ library(propensity)
62
+
63
+ propensity_model <- glm(
64
+ qsmk ~ sex +
65
+ race + age + I(age^2) + education +
66
+ smokeintensity + I(smokeintensity^2) +
67
+ smokeyrs + I(smokeyrs^2) + exercise + active +
68
+ wt71 + I(wt71^2),
69
+ family = binomial(),
70
+ data = nhefs_complete
71
+ )
72
+
73
+ nhefs_complete_wts <- propensity_model |>
74
+ augment(type.predict = "response", data = nhefs_complete) |>
75
+ mutate(w_ate = wt_ate(.fitted, qsmk))
76
+ ```
77
+
78
+
79
+ ``` {r}
80
+ #| output-location: slide
81
+ #| eval: true
82
+ vars <- c(
83
+ "sex", "race", "age", "education",
84
+ "smokeintensity", "smokeyrs",
85
+ "exercise", "active", "wt71"
86
+ )
87
+
88
+ smds <- tidy_smd(
89
+ nhefs_complete_wts,
90
+ .vars = all_of(vars),
91
+ .group = qsmk,
92
+ .wts = w_ate,
93
+ make_dummy_vars = TRUE
94
+ )
95
+
96
+ smds
97
+ ```
98
+
50
99
---
51
100
52
- ## SMD in R ![ ] ( img/hex/halfmoon.png ) {.absolute top=0 right=0 width=140}
101
+ ## Plotting SMDs ![ ] ( img/hex/halfmoon.png ) {.absolute top=0 right=0 width=140}
53
102
54
103
<br />
55
104
@@ -76,37 +125,7 @@ ggplot(
76
125
#| eval: true
77
126
#| message: false
78
127
#| warning: false
79
- library(halfmoon)
80
- library(tidyverse)
81
- library(broom)
82
- library(causaldata)
83
- library(propensity)
84
-
85
- propensity_model <- glm(
86
- qsmk ~ sex +
87
- race + age + I(age^2) + education +
88
- smokeintensity + I(smokeintensity^2) +
89
- smokeyrs + I(smokeyrs^2) + exercise + active +
90
- wt71 + I(wt71^2),
91
- family = binomial(),
92
- data = nhefs_complete
93
- )
94
-
95
- df <- propensity_model |>
96
- augment(type.predict = "response", data = nhefs_complete) |>
97
- mutate(w_ate = wt_ate(.fitted, qsmk))
98
-
99
- vars <- c("sex", "race", "age", "education", "smokeintensity", "smokeyrs",
100
- "exercise", "active", "wt71")
101
-
102
- plot_df <- tidy_smd(
103
- df,
104
- all_of(vars),
105
- qsmk,
106
- w_ate,
107
- make_dummy_vars = TRUE
108
- )
109
- plot_df |>
128
+ smds |>
110
129
arrange(method, abs(smd)) |>
111
130
mutate(variable = fct_inorder(variable)) |>
112
131
ggplot(
@@ -134,7 +153,7 @@ For continuous variables, it can be helpful to look at the _whole_ distribution
134
153
#| message: false
135
154
#| warning: false
136
155
#| eval: true
137
- ggplot(df , aes(x = wt71, color = factor(qsmk))) +
156
+ ggplot(nhefs_complete_wts , aes(x = wt71, color = factor(qsmk))) +
138
157
geom_ecdf() +
139
158
scale_color_manual("Quit smoking", values = c("#5154B8", "#5DB854"),
140
159
labels = c("Yes", "No")) +
@@ -148,7 +167,7 @@ ggplot(df, aes(x = wt71, color = factor(qsmk))) +
148
167
#| code-line-numbers: "|1|2"
149
168
#| eval: true
150
169
#| output-location: slide
151
- ggplot(df , aes(x = wt71, color = factor(qsmk))) +
170
+ ggplot(nhefs_complete_wts , aes(x = wt71, color = factor(qsmk))) +
152
171
geom_ecdf() +
153
172
scale_color_manual(
154
173
"Quit smoking",
@@ -166,7 +185,7 @@ ggplot(df, aes(x = wt71, color = factor(qsmk))) +
166
185
#| eval: true
167
186
#| code-line-numbers: "|2"
168
187
#| output-location: slide
169
- ggplot(df , aes(x = wt71, color = factor(qsmk))) +
188
+ ggplot(nhefs_complete_wts , aes(x = wt71, color = factor(qsmk))) +
170
189
geom_ecdf(aes(weights = w_ate)) +
171
190
scale_color_manual(
172
191
"Quit smoking",
@@ -228,7 +247,7 @@ library(gtsummary)
228
247
229
248
svy_des <- svydesign(
230
249
ids = ~ 1,
231
- data = df |> select(wt71, race, age, sex, smokeintensity, smokeyrs, exercise, active, w_ate, qsmk),
250
+ data = nhefs_complete_wts |> select(wt71, race, age, sex, smokeintensity, smokeyrs, exercise, active, w_ate, qsmk),
232
251
weights = ~ w_ate
233
252
)
234
253
theme_gtsummary_compact()
0 commit comments