4

I'm working through chapter 25 "Many Models" of Hadley Wickham's R for Data Science https://r4ds.had.co.nz/many-models.html , however I'm running into issues in recreating the examples in 25.2.2.

Here's what I have so far (and what's working):

require(gapminder); require(tidyverse); require(broom); require(modelr)

by_country <- gapminder %>% group_by(country,continent) %>% nest()
head(by_country)

# A tibble: 6 x 3
  country     continent data             
  <fct>       <fct>     <list>           
1 Afghanistan Asia      <tibble [12 × 4]>
2 Albania     Europe    <tibble [12 × 4]>
3 Algeria     Africa    <tibble [12 × 4]>
4 Angola      Africa    <tibble [12 × 4]>
5 Argentina   Americas  <tibble [12 × 4]>
6 Australia   Oceania   <tibble [12 × 4]>

Then defining the lm() to apply to each of country's set of data:

country_model <- function(df) {
  lm(lifeExp ~ year, data = df)
}

And then this next line doesn't work:

by_country <- by_country %>%
  mutate(model = map(data,country_model))

with error message

Error in eval(predvars, data, env) : object 'lifeExp' not found 

Despite, to my eyes, what I've written being the same as what's appeared in Hadley's chapter.

I'm unsure if this is a recent issue that used to work, as someone else has apparently had an issue with the example: https://github.com/hadley/r4ds/issues/766 (with no solution)

Any help would be greatly appreciated!

6
  • I've got dplyr v0.8.3 and purrr v0.3.2 and this runs just fine: I'm getting a data frame with a list-column of nested data and a list-column of lm objects. I think I'm a little behind on my updates though Commented Sep 22, 2019 at 15:25
  • What is the output of sessionInfo()? Commented Sep 22, 2019 at 16:15
  • @Parfait good idea, though it results in the following error: Error in UseMethod("unnest_") : no applicable method for 'unnest_' applied to an object of class "list" Commented Sep 22, 2019 at 16:46
  • @Aurèle my sessioninfo is linked here: pastebin.com/t0WhrURN Commented Sep 22, 2019 at 16:49
  • I am able to run the example. The only difference is that I call libraries instead of require, i.e. library(gapminder) library(tidyverse) library(broom) library(modelr) Commented Sep 22, 2019 at 17:56

2 Answers 2

1

You don't need to redefine "by_country" twice.

country_model <- function(df) {
lm(lifeExp ~ year, data = df)
}

by_country <- gapminder %>% 
group_by(country,continent) %>% 
nest()%>%
mutate(model = map(data,country_model))
Sign up to request clarification or add additional context in comments.

Comments

1

The group_by + nest combo can be replaced with nest_by, only thing different is the results is grouped rowwise so you would need to put functions in a list. Something like this:

results <- gapminder %>% 
    nest_by(continent, country) %>% 
    mutate(model = list(lm(lifeExp ~ year, data = data)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.