From a24cb5d3de596c21334669cb1105d69e9a7f9d50 Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Wed, 17 Feb 2021 13:29:41 -0800 Subject: [PATCH] broom -> broom.mixed #17 --- 06-random-slopes.Rmd | 8 ++++---- 08-temporal-autocorrelation.Rmd | 4 ++-- 10-variance-structures.Rmd | 4 ++-- 15-lmm-practice-gapminder.Rmd | 6 +++--- 16-binomial-glmm.Rmd | 2 +- 17-poisson-negbin-glmm.Rmd | 6 +++--- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/06-random-slopes.Rmd b/06-random-slopes.Rmd index effec1c..e556861 100644 --- a/06-random-slopes.Rmd +++ b/06-random-slopes.Rmd @@ -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) ``` @@ -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) diff --git a/08-temporal-autocorrelation.Rmd b/08-temporal-autocorrelation.Rmd index 6b0cf0d..f42c310 100644 --- a/08-temporal-autocorrelation.Rmd +++ b/08-temporal-autocorrelation.Rmd @@ -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") diff --git a/10-variance-structures.Rmd b/10-variance-structures.Rmd index 9c6e00f..13caa09 100644 --- a/10-variance-structures.Rmd +++ b/10-variance-structures.Rmd @@ -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") diff --git a/15-lmm-practice-gapminder.Rmd b/15-lmm-practice-gapminder.Rmd index bf4875e..7d24ece 100644 --- a/15-lmm-practice-gapminder.Rmd +++ b/15-lmm-practice-gapminder.Rmd @@ -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? @@ -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? diff --git a/16-binomial-glmm.Rmd b/16-binomial-glmm.Rmd index 34e4257..f248eff 100644 --- a/16-binomial-glmm.Rmd +++ b/16-binomial-glmm.Rmd @@ -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) %>% diff --git a/17-poisson-negbin-glmm.Rmd b/17-poisson-negbin-glmm.Rmd index d05a631..c6b87df 100644 --- a/17-poisson-negbin-glmm.Rmd +++ b/17-poisson-negbin-glmm.Rmd @@ -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)) + @@ -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") %>%