Get startedGet started for free

Mapping many models

The gap_nested data frame available in your workspace contains the gapminder dataset nested by country.

You will use this data to build a linear model for each country to predict life expectancy using the year feature.

Note: The term feature is synonymous with the terms variable or predictor. It refers to an attribute of your data that can be used to build a machine learning model.

This exercise is part of the course

Machine Learning in the Tidyverse

View Course

Exercise instructions

  • Build a linear model for each country predicting life_expectancy using the year feature. Use the lm() function for this and save this new data frame containing models as gap_models.
  • Extract the first model from this data frame and save this as algeria_model.
  • View the information about the model using summary().

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Build a linear model for each country
gap_models <- gap_nested %>%
    mutate(model = map(___, ~lm(formula = life_expectancy~year, data = ___)))
    
# Extract the model for Algeria    
algeria_model <- gap_models$model[[___]]

# View the summary for the Algeria model
summary(___)
Edit and Run Code