开始使用免费开始使用

构建逐步回归模型

当缺乏领域知识时,逐步回归有助于寻找对目标结果最重要的预测变量。

在本练习中,您将使用前向逐步的方法,将预测变量逐一加入模型,直到加入更多变量不再带来额外收益为止。donors 数据集已为您加载。

本练习是课程的一部分

R 中的监督学习:分类

查看课程

练习说明

  • 使用 R 公式接口配合 glm() 指定不含任何预测变量的基准模型。将解释变量设为 1
  • 再次使用 R 公式接口配合 glm() 指定包含所有预测变量的模型。
  • 对这些模型应用 step() 执行前向逐步回归。将第一个参数设为 null_model,并设置 direction = "forward"。这可能需要一些时间(最长约 10 到 15 秒),因为您的计算机需要拟合相当多的模型来完成逐步选择。
  • 使用 predict() 函数创建一个预测概率向量。
  • 使用 roc()plot() 绘制 ROC 曲线,并用 auc() 计算逐步模型的 AUC。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Specify a null model with no predictors
null_model <- ___(___, data = ___, family = "___")

# Specify the full model using all of the potential predictors
full_model <- ___

# Use a forward stepwise algorithm to build a parsimonious model
step_model <- step(___, scope = list(lower = null_model, upper = full_model), direction = "___")

# Estimate the stepwise donation probability
step_prob <- ___

# Plot the ROC of the stepwise model
library(pROC)
ROC <- ___
plot(___, col = "red")
auc(___)
编辑并运行代码