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!
lmobjects. I think I'm a little behind on my updates thoughsessionInfo()?require, i.e.library(gapminder) library(tidyverse) library(broom) library(modelr)