Tidy each linear regression model
You've now performed a linear regression on each nested dataset and have a linear model stored in the list column model. But you can't recombine the models until you've tidied each into a table of coefficients. To do that, you'll need to use map() one more time and the tidy() function from the broom package.
Recall that you can simply give a function to map() (e.g. map(models, tidy)) in order to apply that function to each item of a list.
This exercise is part of the course
Case Study: Exploratory Data Analysis in R
Exercise instructions
- Load the
broompackage. - Use the
map()function to apply thetidy()function to each linear model in themodelcolumn, creating a new column calledtidied.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Load the broom package
# Add another mutate that applies tidy() to each model
by_year_country %>%
nest(-country) %>%
mutate(model = map(data, ~ lm(percent_yes ~ year, data = .)))