我正在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)
答案 0 :(得分:0)
我更改了roc函数参数的顺序,并解决了错误。
library(pROC)
ROC <- roc( donors$donated, step_prob)
plot(ROC, col = "red")
auc(ROC)