构建并评估最佳模型
通过交叉验证,您已经找到了在使用 gapminder 中所有特征来预测 life_expectancy 的最佳模型。现在模型已选定,您可以使用之前预留的独立数据集(testing_data)来估计该模型在新数据上的表现。
您将使用全部的 training_data 训练该模型,并使用 testing_data 进行评估。
本练习是课程的一部分
Tidyverse 中的机器学习
练习说明
- 使用
ranger()在全部训练数据上构建表现最好的模型(mtry = 4),并将其赋值给best_model。 - 从
testing_data中提取life_expectancy列,赋值给test_actual。 - 使用
best_model在testing数据上预测life_expectancy,并赋值给test_predicted。 - 使用
test_actual和test_predicted向量计算 MAE。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Build the model using all training data and the best performing parameter
best_model <- ranger(formula = ___, data = ___,
mtry = ___, num.trees = 100, seed = 42)
# Prepare the test_actual vector
test_actual <- testing_data$___
# Predict life_expectancy for the testing_data
test_predicted <- predict(___, ___)$predictions
# Calculate the test MAE
mae(___, ___)