Get startedGet started for free

Build better models

Earlier you built a collection of simple models to fit life expectancy using the year feature. Your previous analysis showed that some of these models didn't fit very well.

In this exercise you will build multiple regression models for each country using all available features. You may be interested in comparing the performance of the four worst fitting models so their adjusted \(R^2\) are provided below:

Country Adjusted \(R^2\)
Botswana -0.0060772
Lesotho -0.0169851
Zambia 0.1668999
Zimbabwe 0.2083979

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 every feature in the dataset.
  • Append a column (fit) containing fit statistics for each model and simplify this data frame.
  • Print the adjusted \(R^2\) in fullmodel_perf of the four countries from worst_fit data frame.

Hands-on interactive exercise

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

# Build a linear model for each country using all features
gap_fullmodel <- gap_nested %>% 
  mutate(model = map(data, ~lm(formula = ___, data = .x)))

fullmodel_perf <- gap_fullmodel %>% 
  # Extract the fit statistics of each model into data frames
  mutate(fit = map(model, ~___(.x))) %>% 
  # Simplify the fit data frames for each model
  unnest(___)
  
# View the performance for the four countries with the worst fitting four simple models you looked at before
fullmodel_perf %>% 
  ___(country %in% worst_fit$country) %>% 
  select(country, adj.r.squared)
Edit and Run Code