Skip to content

Commit

Permalink
broom -> broom.mixed #17
Browse files Browse the repository at this point in the history
  • Loading branch information
seananderson committed Feb 17, 2021
1 parent cd150ce commit a24cb5d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions 06-random-slopes.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ arm::display(m_int)
arm::display(m_slopes)
```

A useful package that we will use repeatedly is the broom package. This package provides a set of consistent functions for extracting information from nearly any type of model in R. In particular, we will use the function `broom::tidy`, which returns information about parameter estimates in a tidy data frame, and the function `broom::augment`, which returns a tidy data frame of predictions, residuals, and other useful columns.
A useful package that we will use repeatedly is the broom (and broom.mixed) package. These packages provide a set of consistent functions for extracting information from nearly any type of model in R. In particular, we will use the function `broom.mixed::tidy`, which returns information about parameter estimates in a tidy data frame, and the function `broom.mixed::augment`, which returns a tidy data frame of predictions, residuals, and other useful columns.

To read the documentation for the functions as they apply to `lme4`, see `?broom::tidy.merMod` or `?broom::augment.merMod`.
To read the documentation for the functions as they apply to `lme4`, see `?broom.mixed::tidy.merMod` or `?broom.mixed::augment.merMod`.

```{r}
library(broom)
library(broom.mixed)
tidy(m_slopes, conf.int = TRUE)
tidy(m_int, conf.int = TRUE)
```
Expand All @@ -79,7 +79,7 @@ What has changed? In particular, what has changed about the main effect slope es

# Criticizing the models

Let's extract residuals, predictions, and fitted values from the model objects using the `broom::augment` functions.
Let's extract residuals, predictions, and fitted values from the model objects using the `broom.mixed::augment` functions.

```{r}
aug_int <- augment(m_int)
Expand Down
4 changes: 2 additions & 2 deletions 08-temporal-autocorrelation.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ arm::display(m1)
Let's look at the model fit.

```{r}
aug <- broom::augment(m1)
aug <- broom.mixed::augment(m1)
ggplot(aug, aes(temperature_centered, log.depth_mean_day.)) +
ggplot(aug, aes(temperature_centered, `log(depth_mean_day)`)) +
geom_point(alpha = 0.3) +
facet_wrap(~fish) +
geom_line(aes(x = temperature_centered, y = .fitted), colour = "red")
Expand Down
4 changes: 2 additions & 2 deletions 10-variance-structures.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ Some of the most important ways that we can check our model are to:

What are the model assumptions that these various plots help us check? What are we looking for when we make these plots?

We will plot the residuals various ways now using the augment function from the broom package.
We will plot the residuals various ways now using the augment function from the broom.mixed package.

```{r}
aug <- broom::augment(m)
aug <- broom.mixed::augment(m)
ggplot(aug, aes(x, y)) + geom_point() + facet_wrap(~group) +
geom_line(aes(x, .fitted), colour = "red")
Expand Down
6 changes: 3 additions & 3 deletions 15-lmm-practice-gapminder.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ arm::display(m2)

Take a look at the fitted values versus the residuals for the first model you fit.

You might want to split these up by continent to make them easier to look at. You can do this using `broom::augment()` and plotting them yourself with ggplot, or you can use the shortcut syntax like we were using in the variance structure exercises.
You might want to split these up by continent to make them easier to look at. You can do this using `broom.mixed::augment()` and plotting them yourself with ggplot, or you can use the shortcut syntax like we were using in the variance structure exercises.

Also plot your residuals against the predictors in the model and any other projectors that were not included in the model but were in the data set. How do these look to you?

Expand Down Expand Up @@ -138,13 +138,13 @@ Controlling for changes in GDP per capita, what is the average increase in life
round(fixef(m3)["decade"], 1) # exercise
```

By looking at the standard errors in `arm::display(m3)` or by using the function `arm::se.fixef(m3)`, what is an approximate 95% confidence interval on the average increase in life expectancy per decade? (Bonus: can you get the same values from `broom::tidy()`?)
By looking at the standard errors in `arm::display(m3)` or by using the function `arm::se.fixef(m3)`, what is an approximate 95% confidence interval on the average increase in life expectancy per decade? (Bonus: can you get the same values from `broom.mixed::tidy()`?)

```{r}
fe <- fixef(m3)["decade"] # exercise
se <- arm::se.fixef(m3)["decade"] # exercise
fe + c(-1.96, 1.96) * se # exercise
broom::tidy(m3, conf.int = TRUE) # exercise
broom.mixed::tidy(m3, conf.int = TRUE) # exercise
```

Is the effect of GDP per capita stronger within or across countries? How do you know this and what does this mean?
Expand Down
2 changes: 1 addition & 1 deletion 16-binomial-glmm.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ One useful way of checking residuals for a binomial response is by using a binne
The idea is to group the residuals into bins based on the predictor values. The trick is picking an appropriate number of bins for this to be useful. We can make all the same types of plots with these binned residuals.

```{r}
aug <- broom::augment(m)
aug <- broom.mixed::augment(m)
aug <- aug %>% mutate(week_binned = findInterval(week_centered,
seq(min(week_centered), max(week_centered), length.out = 25)))
binned <- group_by(aug, species, week_binned, tank) %>%
Expand Down
6 changes: 3 additions & 3 deletions 17-poisson-negbin-glmm.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ save(mstan_nb, file = "data/generated/stan-models.rda")
```{r}
load("data/generated/stan-models.rda")
stan_est <- broom::tidyMCMC(mstan_nb, conf.int = TRUE)
stan_est <- broom.mixed::tidyMCMC(mstan_nb, conf.int = TRUE)
stan_est %>% filter(grepl("gbpu", term)) %>%
ggplot(aes(term, estimate, ymin = conf.low, ymax = conf.high)) +
Expand Down Expand Up @@ -312,10 +312,10 @@ cis[[1]] <- get_tmb_cis(m_tmb_pois, "TMB Poisson")
cis[[2]] <- get_tmb_cis(m_tmb_obs, "TMB Poisson w obs RE")
cis[[3]] <- get_tmb_cis(m_tmb_nb, "TMB Negative binomial (NB2)")
cis[[4]] <- get_tmb_cis(m_tmb_nb1, "TMB Negative binomial (NB1)")
cis[[5]] <- broom::tidy(m_glmer, conf.int = TRUE) %>%
cis[[5]] <- broom.mixed::tidy(m_glmer, conf.int = TRUE) %>%
dplyr::select(conf.low, conf.high, estimate, term) %>%
mutate(model = "lme4::glmer Poisson") %>%
filter(term != "sd_(Intercept).gbpu")
filter(term != "sd__(Intercept)")
cis[[6]] <- stan_est %>%
dplyr::select(conf.low, conf.high, estimate, term) %>%
filter(!grepl("Intercept", term), term != "reciprocal_dispersion") %>%
Expand Down

0 comments on commit a24cb5d

Please sign in to comment.