LoslegenKostenlos loslegen

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

Diese Übung ist Teil des Kurses

Machine Learning in the Tidyverse

Kurs anzeigen

Anleitung zur Übung

  • 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.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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)
Code bearbeiten und ausführen