Get startedGet started for free

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

View Course

Exercise instructions

  • Load the broom package.
  • Use the map() function to apply the tidy() function to each linear model in the model column, creating a new column called tidied.

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 = .)))
Edit and Run Code