首页 > 解决方案 > ROC 逐步回归

问题描述

我正在 DataCamp 上使用 R 学习数据科学。在一个练习中,我必须建立一个逐步回归模型。即使我成功创建了逐步模型,roc() 函数也不接受响应,它会给出如下错误:“'response' 有两个以上的级别。考虑明确设置 'levels' 或使用 'multiclass.roc' "

我想学习如何处理这个问题,所以我在下面写了我的代码。

# Specify a null model with no predictors
null_model <- glm(donated ~ 1, data = donors, family = "binomial")

# Specify the full model using all of the potential predictors
full_model <- glm(donated ~ ., data = donors, family = "binomial")

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

# Estimate the stepwise donation probability
step_prob <- predict(step_model, type = "response")
# Plot the ROC of the stepwise model
library(pROC)
ROC <- roc( step_prob, donors$donated)
plot(ROC, col = "red")
auc(ROC)

标签: rregressionauc

解决方案


我改变了 roc 函数参数的顺序,错误就解决了。

library(pROC)
ROC <- roc( donors$donated, step_prob)
plot(ROC, col = "red")
auc(ROC) 

推荐阅读