随机森林:可视化
现在需要对预测结果进行可视化。在梯度提升树模型中,您绘制了预测值与真实值的散点图,以及残差的密度图。接下来,您将调整这些图表,一次性展示两个模型的结果。
本练习是课程的一部分
R 中使用 sparklyr 的 Spark 入门
练习说明
已预先定义本地 tibble both_responses,其中包含两个模型的预测年份和真实年份。
- 更新"预测值 vs 真实值"的散点图。
- 使用
both_responses数据集。 - 添加颜色美学映射,以区分不同模型。使用
color = model。 - 不绘制散点,改用
geom_smooth()为每个模型绘制平滑曲线。
- 使用
- 创建名为
residuals的残差 tibble。- 对
both_responses调用mutate()。 - 新列命名为
residual。 residual等于预测值减去真实值。
- 对
- 更新残差密度图。
- 添加颜色美学映射,以区分不同模型。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# both_responses has been pre-defined
both_responses
# Draw a scatterplot of predicted vs. actual
ggplot(___, aes(actual, predicted, ___)) +
# Add a smoothed line
___ +
# Add a line at actual = predicted
geom_abline(intercept = 0, slope = 1)
# Create a tibble of residuals
residuals <- ___
# Draw a density plot of residuals
ggplot(residuals, aes(residual, ___)) +
# Add a density curve
geom_density() +
# Add a vertical line through zero
geom_vline(xintercept = 0)