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 |
Deze oefening maakt deel uit van de cursus
Machine Learning in the Tidyverse
Oefeninstructies
- Build a linear model for each country predicting
life_expectancyusing 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_perfof the four countries fromworst_fitdata frame.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# 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)