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
Exercise instructions
- Build a linear model for each country predicting
life_expectancy
using theyear
feature. Use thelm()
function for this and save this new data frame containing models asgap_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(___)