开始使用免费开始使用

构建更优的模型

之前,您已经用 year 特征为 life expectancy 构建了一组简单模型。先前的分析显示,其中一些模型的拟合效果并不好。

在本练习中,您将为每个国家基于所有可用特征构建多元回归模型。您可能想比较拟合最差的 4 个模型的表现,因此它们的调整后 \(R^2\) 如下所示:

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

本练习是课程的一部分

Tidyverse 中的机器学习

查看课程

练习说明

  • 为每个国家构建线性模型,使用数据集中所有特征来预测 life_expectancy
  • 添加一列(fit),包含每个模型的拟合统计量,并将该数据框简化。
  • fullmodel_perf 中打印来自 worst_fit 数据框的 4 个国家的调整后 $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)
编辑并运行代码