Skip to content

Commit 2488bf5

Browse files
switch to halfmoon and name-names
1 parent 7a9e866 commit 2488bf5

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

exercises/06-pscores-weighting-exercises.qmd

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ format: html
44
---
55

66

7-
```{r}
7+
```{r setup}
88
library(tidyverse)
99
library(broom)
1010
library(touringplans)
11+
library(propensity)
1112
```
1213

1314
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**).
@@ -25,7 +26,7 @@ propensity_model <- glm(
2526
family = binomial()
2627
)
2728
28-
df <- propensity_model |>
29+
seven_dwarfs_prop <- propensity_model |>
2930
augment(type.predict = "response", data = seven_dwarfs)
3031
```
3132

@@ -38,65 +39,67 @@ Create at "matched" data set using the same propensity score model as above and
3839
```{r}
3940
#| eval: false
4041
library(MatchIt)
41-
m <- matchit(___,
42-
data = seven_dwarfs,
43-
___,
44-
___
45-
)
46-
matched_df <- ___(m)
42+
matched_dwarfs <- matchit(
43+
___,
44+
data = seven_dwarfs,
45+
___,
46+
___
47+
)
48+
49+
matched_df <- ___(matched_dwarfs)
4750
```
4851

4952
## Your Turn 2 (Weighting)
5053

5154
_After updating the code chunks below, change `eval = TRUE` before knitting._
5255

53-
Add the ATE weights to the data frame, `df`
56+
Add the ATE weights to the data frame, `seven_dwarfs_prop`
5457

5558
```{r}
5659
#| eval: false
57-
df <- df |>
60+
seven_dwarfs_prop <- seven_dwarfs_prop |>
5861
mutate(w_ate = ___)
5962
```
6063

6164

6265
Stretch Goal 1:
6366

64-
Add ATM weights to the data frame, `df`
67+
Add ATM weights to the data frame, `seven_dwarfs_prop`
6568

6669
```{r}
6770
#| eval: false
68-
df <- df |>
71+
seven_dwarfs_prop <- seven_dwarfs_prop |>
6972
mutate(w_atm = ___)
7073
```
7174

7275
Stretch Goal 2:
7376

74-
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_histogram()` calls.
77+
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.
7578

7679
```{r}
7780
#| eval: false
78-
d <- df |>
79-
pivot_wider(names_from = extra_magic_morning,
80-
values_from = .fitted,
81-
names_prefix = "extra_magic_morning_p")
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+
)
8287
```
8388

8489

8590
```{r}
8691
#| eval: false
8792
#| warning: false
88-
ggplot(d) +
89-
geom_histogram(bins = 50, aes(extra_magic_morning_p1), alpha = 0.5) +
90-
geom_histogram(bins = 50, aes(extra_magic_morning_p1, weight = ____), fill = "green", alpha = 0.5) +
91-
geom_histogram(bins = 50, alpha = 0.5, aes(x = extra_magic_morning_p0, y = -after_stat(count))) +
92-
geom_histogram(bins = 50, aes(x = extra_magic_morning_p0, weight = ____, y = -after_stat(count)), fill = "blue", alpha = 0.5) +
93-
ylab("count") + xlab("p") +
93+
ggplot(
94+
seven_dwarfs_prop,
95+
aes(.fitted, fill = factor(extra_magic_morning))
96+
) +
97+
geom_mirror_histogram(bins = 50, alpha = .5) +
98+
geom_mirror_histogram(aes(weight = ____), alpha = .5, bins = 50) +
9499
geom_hline(yintercept = 0, lwd = 0.5) +
95-
scale_y_continuous(label = abs) +
96-
theme_minimal() +
97-
geom_rect(aes(xmin = 0.95, xmax = 1, ymin = 5, ymax = 100), fill = "#5DB854") +
98-
annotate("text", x = 0.975, y = 50, label = "trt", angle = 270, color = "white") +
99-
geom_rect(aes(xmin = 0.95, xmax = 1, ymin = -100, ymax = -5), fill = "#5154B8") +
100-
annotate("text", x = 0.975, y = -50, label = "control", angle = 270, color = "white")
101-
100+
theme_minimal() +
101+
scale_y_continuous(labels = abs) +
102+
scale_fill_manual(values = c("blue", "green")) +
103+
labs(x = "p", fill = "Extra Magic Morning") +
104+
xlim(0, 1)
102105
```

0 commit comments

Comments
 (0)