建立更好的模型
先前你已建立一組以 year 特徵來配適 life expectancy 的簡單模型。你之前的分析顯示,有些模型的配適並不理想。
在這個練習中,你將為每個國家建立使用所有可用特徵的多元迴歸模型。你可能會想比較先前配適最差的 4 個模型,因此以下提供它們的調整後 $R^2$:
| Country | Adjusted \(R^2\) |
|---|---|
| Botswana | -0.0060772 |
| Lesotho | -0.0169851 |
| Zambia | 0.1668999 |
| Zimbabwe | 0.2083979 |
本練習屬於課程
Tidyverse 的 Machine Learning
練習說明
- 為每個國家建立線性模型,使用資料集中所有特徵來預測
life_expectancy。 - 新增一個欄位(
fit),放入各模型的配適統計量,並將此資料框簡化。 - 在
fullmodel_perf中列印worst_fit資料框中那四個國家的調整後 $R^2$。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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)