開始使用免費開始

建立逐步迴歸模型

當缺乏領域專家知識時,逐步迴歸(stepwise regression) 可以幫助你找出對目標結果影響最大的預測變數。

在這個練習中,你會採用前向逐步法,將預測變數一個一個加進模型,直到加入更多變數不再帶來額外效益為止。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(___)
編輯並執行程式碼