Skip to content

Commit eb3b015

Browse files
Solomon KurzSolomon Kurz
authored andcommitted
minor code/prose updates
1 parent 02aa781 commit eb3b015

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

12.Rmd

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,12 @@ post_mdn %>%
210210
geom_vline(xintercept = c(16.5, 32.5), size = 1/4) +
211211
geom_point(aes(y = propsurv), color = "orange2") +
212212
geom_point(aes(y = post_mdn), shape = 1) +
213-
coord_cartesian(ylim = c(0, 1)) +
213+
annotate(geom = "text", x = c(8, 16 + 8, 32 + 8), y = 0,
214+
label = c("small tanks", "medium tanks", "large tanks")) +
214215
scale_x_continuous(breaks = c(1, 16, 32, 48)) +
215216
labs(title = "Multilevel shrinkage!",
216217
subtitle = "The empirical proportions are in orange while the model-\nimplied proportions are the black circles. The dashed line is\nthe model-implied average survival proportion.") +
217-
annotate(geom = "text", x = c(8, 16 + 8, 32 + 8), y = 0,
218-
label = c("small tanks", "medium tanks", "large tanks")) +
218+
coord_cartesian(ylim = c(0, 1)) +
219219
theme_fivethirtyeight() +
220220
theme(panel.grid = element_blank())
221221
```
@@ -235,9 +235,9 @@ p1 <-
235235
ggplot(aes(x = x, group = iter)) +
236236
geom_line(aes(y = dnorm(x, b_Intercept, sd_tank__Intercept)),
237237
alpha = .2, color = "orange2") +
238+
scale_y_continuous(NULL, breaks = NULL) +
238239
labs(title = "Population survival distribution",
239240
subtitle = "log-odds scale") +
240-
scale_y_continuous(NULL, breaks = NULL) +
241241
coord_cartesian(xlim = c(-3, 4))
242242
```
243243

@@ -484,12 +484,12 @@ dsim %>%
484484
y = mean_error, yend = mean_error),
485485
color = rep(c("orange2", "black"), each = 4),
486486
linetype = rep(1:2, each = 4)) +
487-
scale_x_continuous(breaks = c(1, 10, 20, 30, 40, 50, 60)) +
488487
annotate("text", x = c(15 - 7.5, 30 - 7.5, 45 - 7.5, 60 - 7.5), y = .45,
489488
label = c("tiny (5)", "small (10)", "medium (25)", "large (35)")) +
490-
labs(title = "Estimate error by model type",
489+
scale_x_continuous(breaks = c(1, 10, 20, 30, 40, 50, 60)) +
490+
labs(title = "Estimate error by model type",
491491
subtitle = "The horizontal axis displays pond number. The vertical axis measures\nthe absolute error in the predicted proportion of survivors, compared to\nthe true value used in the simulation. The higher the point, the worse\nthe estimate. No-pooling shown in orange. Partial pooling shown in black.\nThe orange and dashed black lines show the average error for each kind\nof estimate, across each initial density of tadpoles (pond size). Smaller\nponds produce more error, but the partial pooling estimates are better\non average, especially in smaller ponds.",
492-
y = "absolute error") +
492+
y = "absolute error") +
493493
theme_fivethirtyeight() +
494494
theme(panel.grid = element_blank(),
495495
plot.subtitle = element_text(size = 10))
@@ -810,11 +810,11 @@ post %>%
810810
geom_density(size = 0, fill = "orange1", alpha = 3/4) +
811811
geom_density(aes(x = sd_block__Intercept),
812812
size = 0, fill = "orange4", alpha = 3/4) +
813-
scale_y_continuous(NULL, breaks = NULL) +
814-
coord_cartesian(xlim = c(0, 4)) +
815-
ggtitle(expression(sigma["[x]"])) +
816813
annotate(geom = "text", x = 2/3, y = 2, label = "block", color = "orange4") +
817814
annotate(geom = "text", x = 2, y = 3/4, label = "actor", color = "orange1") +
815+
scale_y_continuous(NULL, breaks = NULL) +
816+
ggtitle(expression(sigma["[x]"])) +
817+
coord_cartesian(xlim = c(0, 4)) +
818818
theme_fivethirtyeight()
819819
```
820820

@@ -1058,7 +1058,7 @@ fix_ef <-
10581058
ran_and_fix_ef <-
10591059
bind_cols(ran_ef, fix_ef) %>%
10601060
mutate(intercept = fixed_effect + random_effect) %>%
1061-
mutate(prob = inv_logit_scaled(intercept))
1061+
mutate(prob = inv_logit_scaled(intercept))
10621062
10631063
# to simplify things, we'll reduce them to summaries
10641064
(
@@ -1096,9 +1096,9 @@ p3 <-
10961096
filter(iter %in% c(1:50)) %>%
10971097
10981098
ggplot(aes(x = condition, y = prob, group = iter)) +
1099+
geom_line(alpha = 1/2, color = "orange3") +
10991100
ggtitle("50 simulated actors") +
11001101
coord_cartesian(ylim = 0:1) +
1101-
geom_line(alpha = 1/2, color = "orange3") +
11021102
theme_fivethirtyeight() +
11031103
theme(plot.title = element_text(size = 14, hjust = .5))
11041104
@@ -1341,7 +1341,7 @@ and we've been grappling with the relation between the grand mean $\alpha$ and t
13411341

13421342
For our first step, we'll introduce the models.
13431343

1344-
### Intercepts-only models with one or two grouping variables
1344+
### Intercepts-only models with one or two grouping variables.
13451345

13461346
If you recall, `b12.4` was our first multilevel model with the chimps data. We can retrieve the model formula like so.
13471347

@@ -1435,22 +1435,22 @@ print(b12.8)
14351435

14361436
Now we've fit our two intercepts-only models, let's get to the heart of this section. We are going to practice four methods for working with the posterior samples. Each method will revolve around a different primary function. In order, they are
14371437

1438-
* `brms::posterior_samples()`
1439-
* `brms::coef()`
1440-
* `brms::fitted()`
1441-
* `tidybayes::spread_draws()`
1438+
* `brms::posterior_samples()`,
1439+
* `brms::coef()`,
1440+
* `brms::fitted()`, and
1441+
* `tidybayes::spread_draws()`.
14421442

14431443
We've already had some practice with the first three, but I hope this section will make them even more clear. The `tidybayes::spread_draws()` method will be new, to us. I think you'll find it's a handy alternative.
14441444

1445-
With each of the four methods, we'll practice three different model summaries.
1445+
With each of the four methods, we'll practice three different model summaries:
14461446

1447-
* Getting the posterior draws for the `actor`-level estimates from the `b12.7` model
1448-
* Getting the posterior draws for the `actor`-level estimates from the cross-classified `b12.8` model, averaging over the levels of `block`
1449-
* Getting the posterior draws for the `actor`-level estimates from the cross-classified `b12.8` model, based on `block == 1`
1447+
* getting the posterior draws for the `actor`-level estimates from the `b12.7` model;
1448+
* getting the posterior draws for the `actor`-level estimates from the cross-classified `b12.8` model, averaging over the levels of `block`; and
1449+
* getting the posterior draws for the `actor`-level estimates from the cross-classified `b12.8` model, based on `block == 1`.
14501450

14511451
So to be clear, our goal is to accomplish those three tasks with four methods, each of which should yield equivalent results.
14521452

1453-
### `brms::posterior_samples()`
1453+
### `brms::posterior_samples()`.
14541454

14551455
To warm up, let's take a look at the structure of the `posterior_samples()` output for the simple `b12.7` model.
14561456

@@ -1521,7 +1521,7 @@ str(p3)
15211521

15221522
Again, I like this method because of how close the wrangling code within `transmute()` is to the statistical model formula. I wrote a lot of code like this in my early days of working with these kinds of models, and I think the pedagogical insights were helpful. But this method has its limitations. It works fine if you're working with some small number of groups. But that's a lot of repetitious code and it would be utterly un-scalable to situations where you have 50 or 500 levels in your grouping variable. We need alternatives.
15231523

1524-
### `brms::coef()`
1524+
### `brms::coef()`.
15251525

15261526
First, let's review what the `coef()` function returns.
15271527

@@ -1598,7 +1598,7 @@ $$10 + \operatorname{Normal}(0, 1).$$
15981598

15991599
Conversely, it can be a little abstract. Let's keep expanding our options.
16001600

1601-
### `brms::fitted()`
1601+
### `brms::fitted()`.
16021602

16031603
As is often the case, we're going to want to define our predictor values for `fitted()`.
16041604

@@ -1684,7 +1684,7 @@ str(f3)
16841684

16851685
Let's learn one more option.
16861686

1687-
### `tidybayes::spread_draws()`
1687+
### `tidybayes::spread_draws()`.
16881688

16891689
Up till this point, we've really only used the tidybayes package for plotting (e.g., with `geom_halfeyeh()`) and summarizing (e.g., with `median_qi()`). But tidybayes is more general; it offers a handful of convenience functions for wrangling posterior draws from a tidyverse perspective. One such function is `spread_draws()`, which you can learn all about in Matthew Kay's vignette, [*Extracting and visualizing tidy draws from brms models*](https://mjskay.github.io/tidybayes/articles/tidy-brms.html). Let's take a look at how we'll be using it.
16901690

0 commit comments

Comments
 (0)