Skip to content

Commit 88945a8

Browse files
committed
Update
1 parent 2859beb commit 88945a8

File tree

5 files changed

+48
-48
lines changed

5 files changed

+48
-48
lines changed

Gay/gay_tv.Rmd

+13-13
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Data
4343
naes <-
4444
file_naes %>%
4545
read_csv() %>%
46-
select(!X1)
46+
select(!...1)
4747
4848
naes
4949
```
@@ -65,7 +65,7 @@ Let's now look at `age`.
6565
```{r}
6666
age_count <- function(var) {
6767
naes %>%
68-
drop_na({{var}}) %>%
68+
drop_na({{ var }}) %>%
6969
count(age) %>%
7070
arrange(desc(age))
7171
}
@@ -89,12 +89,12 @@ Create indicator variable `y` from `var` and add to naes.
8989
```{r}
9090
indicator <- function(var) {
9191
naes %>%
92-
drop_na({{var}}) %>%
92+
drop_na({{ var }}) %>%
9393
mutate(
9494
y =
9595
case_when(
96-
{{var}} == "Yes" ~ 1,
97-
{{var}} == "No" ~ 0,
96+
{{ var }} == "Yes" ~ 1,
97+
{{ var }} == "No" ~ 0,
9898
TRUE ~ NA_real_
9999
)
100100
)
@@ -106,10 +106,10 @@ Return tibble with proportion of "Yes" responses and total number of responses f
106106
```{r}
107107
yes_prop <- function(var) {
108108
naes %>%
109-
drop_na({{var}}) %>%
109+
drop_na({{ var }}) %>%
110110
group_by(age) %>%
111111
summarize(
112-
y = sum({{var}} == "Yes") / n(),
112+
y = sum({{ var }} == "Yes") / n(),
113113
n = n()
114114
)
115115
}
@@ -122,14 +122,14 @@ pred <- function(var, method = c("loess", "splines")) {
122122
method <- match.arg(method)
123123
124124
if (method == "loess") {
125-
data <- indicator({{var}})
125+
data <- indicator({{ var }})
126126
fit <- loess(y ~ age, data = data)
127127
tibble(
128128
age = seq_range(data$age),
129129
y = predict(fit, newdata = tibble(age))
130130
)
131131
} else if (method == "splines") {
132-
data <- yes_prop({{var}})
132+
data <- yes_prop({{ var }})
133133
fit <- stan_gamm4(y ~ s(age), data = data, refresh = 0, adapt_delta = 0.99)
134134
tibble(age = seq_range(data$age)) %>%
135135
predictive_intervals(fit = fit)
@@ -150,7 +150,7 @@ plot <- function(var, method = c("", "loess", "splines")) {
150150
)
151151
152152
plot <-
153-
yes_prop({{var}}) %>%
153+
yes_prop({{ var }}) %>%
154154
ggplot(aes(age)) +
155155
geom_point(aes(y = y, size = n), shape = "circle filled", fill = "grey75") +
156156
coord_cartesian(ylim = c(0, NA)) +
@@ -161,18 +161,18 @@ plot <- function(var, method = c("", "loess", "splines")) {
161161
) +
162162
theme(legend.position = "none") +
163163
labs(
164-
title = title %>% pull({{var}}),
164+
title = title %>% pull({{ var }}),
165165
x = "Age",
166166
y = "Percentage of yes responses"
167167
)
168168
169169
if (method == "loess") {
170170
plot <-
171171
plot +
172-
geom_line(aes(age, y), data = pred(var = {{var}}, method = "loess")) +
172+
geom_line(aes(age, y), data = pred(var = {{ var }}, method = "loess")) +
173173
labs(subtitle = "Loess fit")
174174
} else if (method == "splines") {
175-
v <- pred(var = {{var}}, method = "splines")
175+
v <- pred(var = {{ var }}, method = "splines")
176176
plot <-
177177
plot +
178178
geom_ribbon(aes(ymin = `5%`, ymax = `95%`), data = v, alpha = 0.25) +

Gay/gay_tv.md

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Regression and Other Stories: Gay
22
================
33
Andrew Gelman, Aki Vehtari
4-
2021-04-20
4+
2021-09-06
55

66
- [22 Advanced regression and multilevel
77
models](#22-advanced-regression-and-multilevel-models)
@@ -46,24 +46,24 @@ Data
4646
naes <-
4747
file_naes %>%
4848
read_csv() %>%
49-
select(!X1)
49+
select(!...1)
5050

5151
naes
5252
```
5353

54-
#> # A tibble: 81,422 x 6
55-
#> age gender race gayFavorFederalMarri… gayFavorStateMarri… gayKnowSomeone
56-
#> <dbl> <chr> <chr> <chr> <chr> <chr>
57-
#> 1 70 Female Hispan… No <NA> <NA>
58-
#> 2 54 Female White No <NA> <NA>
59-
#> 3 74 Male White No <NA> <NA>
60-
#> 4 73 Female Other Yes No No
61-
#> 5 48 Female White No Yes Yes
62-
#> 6 58 Male White No Yes Yes
63-
#> 7 35 Female White Yes <NA> <NA>
64-
#> 8 74 Female White No <NA> <NA>
65-
#> 9 63 Female White No No No
66-
#> 10 64 Male White Yes <NA> <NA>
54+
#> # A tibble: 81,422 × 6
55+
#> age gender race gayFavorFederalMarri… gayFavorStateMarr… gayKnowSomeone
56+
#> <dbl> <chr> <chr> <chr> <chr> <chr>
57+
#> 1 70 Female Hispanic No <NA> <NA>
58+
#> 2 54 Female White No <NA> <NA>
59+
#> 3 74 Male White No <NA> <NA>
60+
#> 4 73 Female Other Yes No No
61+
#> 5 48 Female White No Yes Yes
62+
#> 6 58 Male White No Yes Yes
63+
#> 7 35 Female White Yes <NA> <NA>
64+
#> 8 74 Female White No <NA> <NA>
65+
#> 9 63 Female White No No No
66+
#> 10 64 Male White Yes <NA> <NA>
6767
#> # … with 81,412 more rows
6868

6969
Let’s understand the `NA`s in the data.
@@ -94,15 +94,15 @@ Let’s now look at `age`.
9494
``` r
9595
age_count <- function(var) {
9696
naes %>%
97-
drop_na({{var}}) %>%
97+
drop_na({{ var }}) %>%
9898
count(age) %>%
9999
arrange(desc(age))
100100
}
101101

102102
age_count(gayFavorStateMarriage)
103103
```
104104

105-
#> # A tibble: 81 x 2
105+
#> # A tibble: 81 × 2
106106
#> age n
107107
#> <dbl> <int>
108108
#> 1 97 1
@@ -121,7 +121,7 @@ age_count(gayFavorStateMarriage)
121121
age_count(gayKnowSomeone)
122122
```
123123

124-
#> # A tibble: 81 x 2
124+
#> # A tibble: 81 × 2
125125
#> age n
126126
#> <dbl> <int>
127127
#> 1 97 1
@@ -151,12 +151,12 @@ Create indicator variable `y` from `var` and add to naes.
151151
``` r
152152
indicator <- function(var) {
153153
naes %>%
154-
drop_na({{var}}) %>%
154+
drop_na({{ var }}) %>%
155155
mutate(
156156
y =
157157
case_when(
158-
{{var}} == "Yes" ~ 1,
159-
{{var}} == "No" ~ 0,
158+
{{ var }} == "Yes" ~ 1,
159+
{{ var }} == "No" ~ 0,
160160
TRUE ~ NA_real_
161161
)
162162
)
@@ -169,10 +169,10 @@ responses for each age for variable `var`.
169169
``` r
170170
yes_prop <- function(var) {
171171
naes %>%
172-
drop_na({{var}}) %>%
172+
drop_na({{ var }}) %>%
173173
group_by(age) %>%
174174
summarize(
175-
y = sum({{var}} == "Yes") / n(),
175+
y = sum({{ var }} == "Yes") / n(),
176176
n = n()
177177
)
178178
}
@@ -186,14 +186,14 @@ pred <- function(var, method = c("loess", "splines")) {
186186
method <- match.arg(method)
187187

188188
if (method == "loess") {
189-
data <- indicator({{var}})
189+
data <- indicator({{ var }})
190190
fit <- loess(y ~ age, data = data)
191191
tibble(
192192
age = seq_range(data$age),
193193
y = predict(fit, newdata = tibble(age))
194194
)
195195
} else if (method == "splines") {
196-
data <- yes_prop({{var}})
196+
data <- yes_prop({{ var }})
197197
fit <- stan_gamm4(y ~ s(age), data = data, refresh = 0, adapt_delta = 0.99)
198198
tibble(age = seq_range(data$age)) %>%
199199
predictive_intervals(fit = fit)
@@ -214,7 +214,7 @@ plot <- function(var, method = c("", "loess", "splines")) {
214214
)
215215

216216
plot <-
217-
yes_prop({{var}}) %>%
217+
yes_prop({{ var }}) %>%
218218
ggplot(aes(age)) +
219219
geom_point(aes(y = y, size = n), shape = "circle filled", fill = "grey75") +
220220
coord_cartesian(ylim = c(0, NA)) +
@@ -225,18 +225,18 @@ plot <- function(var, method = c("", "loess", "splines")) {
225225
) +
226226
theme(legend.position = "none") +
227227
labs(
228-
title = title %>% pull({{var}}),
228+
title = title %>% pull({{ var }}),
229229
x = "Age",
230230
y = "Percentage of yes responses"
231231
)
232232

233233
if (method == "loess") {
234234
plot <-
235235
plot +
236-
geom_line(aes(age, y), data = pred(var = {{var}}, method = "loess")) +
236+
geom_line(aes(age, y), data = pred(var = {{ var }}, method = "loess")) +
237237
labs(subtitle = "Loess fit")
238238
} else if (method == "splines") {
239-
v <- pred(var = {{var}}, method = "splines")
239+
v <- pred(var = {{ var }}, method = "splines")
240240
plot <-
241241
plot +
242242
geom_ribbon(aes(ymin = `5%`, ymax = `95%`), data = v, alpha = 0.25) +
367 Bytes
Loading

Poststrat/poststrat2_tv.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Function to return simulation parameter.
6767
param <- function(var_, value_, param) {
6868
params %>%
6969
filter(var == var_, value == value_) %>%
70-
pull({{param}})
70+
pull({{ param }})
7171
}
7272
7373
param("sex", "Female", pop_prop)

Poststrat/poststrat2_tv.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Regression and Other Stories: Poststratification 2
22
================
33
Andrew Gelman, Jennifer Hill, Aki Vehtari
4-
2021-06-23
4+
2021-09-06
55

66
- [17 Poststratification and missing-data
77
imputation](#17-poststratification-and-missing-data-imputation)
@@ -74,7 +74,7 @@ Function to return simulation parameter.
7474
param <- function(var_, value_, param) {
7575
params %>%
7676
filter(var == var_, value == value_) %>%
77-
pull({{param}})
77+
pull({{ param }})
7878
}
7979

8080
param("sex", "Female", pop_prop)
@@ -304,7 +304,7 @@ poststrat %>%
304304
summarize(yes_pop = sum(yes_pred * n_prop))
305305
```
306306

307-
#> # A tibble: 1 x 1
307+
#> # A tibble: 1 × 1
308308
#> yes_pop
309309
#> <dbl>
310310
#> 1 0.586
@@ -319,7 +319,7 @@ tibble(
319319
summarize(across(yes_pop, list(mean = mean, sd = sd)))
320320
```
321321

322-
#> # A tibble: 1 x 2
322+
#> # A tibble: 1 × 2
323323
#> yes_pop_mean yes_pop_sd
324324
#> <dbl> <dbl>
325325
#> 1 0.586 0.0170
@@ -333,7 +333,7 @@ poststrat %>%
333333
summarize(yes_pop_true = sum(yes_prob * n_prop))
334334
```
335335

336-
#> # A tibble: 1 x 1
336+
#> # A tibble: 1 × 1
337337
#> yes_pop_true
338338
#> <dbl>
339339
#> 1 0.593

0 commit comments

Comments
 (0)