In the book by Wickham he works with the txhousing data set and produces the data frame in question by using the glance function from the broom package. That function has been removed and now in the tidyverse glimpse exists. However, you cannot simple replace glance with glimpse. I have been searching diligently but cannot seem to find an answer.

glimpse(models)

model_sum <- models %>% (mod)

model_sum

The following error occurs:

Error in models %>% (mod) : attempt to apply non-function
1

There are 1 best solutions below

0
Jon Spring On

It looks like you're looking at the 2016 edition of "ggplot2: Elegant Graphics for Data Analysis." http://www.ievbras.ru/ecostat/Kiril/R/Biblio_N/R_Eng/Wickham2016.pdf

The current version of the ggplot2 book doesn't go into modeling and leaves that for another Wickham book, the R for Data Science book that focuses more on dplyr and tidyr.

In the time since 2016, the tidyverse team has updated some of the approaches and syntax used there, so it doesn't look like that code will run verbatim any more.

In the 2016 ggplot2 book, there is code that defines

models <- txhousing %>% 
  group_by(city) %>% 
  do(mod = lm(log2(sales) ~ factor(month), data = ., na.action = na.exclude))

so that models has a mod column. Older versions of broom could run

model_sum <- models %>%
  broom::glance(mod) # I'm using v1.0.1

but now doing so gives you an error that could understandably make one think that glance was replaced by glimpse:

Error: There is no glance method for tibbles. Did you mean `tibble::glimpse()`?

The problem is that broom 0.5.0 in 2018 included breaking changes that made it more selective about how it could work; it could no longer run on a list column directly, but rather was expected to be in a mutate - map syntax.

If you want to practice modeling with the tidyverse as it exists in 2023, I'd focus on the updated approaches in the R4DS link above. It would suggest something like:

models2 <- txhousing %>%
  group_by(city) %>%
  nest() %>%
  mutate(mod =    map(data, 
                      ~lm(log2(sales) ~ factor(month),
                      data = ., 
                      na.action = na.exclude)),
         glance = map(mod,
                      broom::glance))

You might also look at this useful vignette: https://cran.r-project.org/web/packages/broom/vignettes/broom_and_dplyr.html