2

我正在尝试计算二进制(0,1)与决策树预测的目标变量的 ROC。

当我将预测值设置为二进制时,它给了我以下错误:

> roc(as.numeric(pred),as.numeric(data$target))

Setting levels: control = 0, case = 1
Setting direction: controls < cases

当我将预测值设置为概率时,它给了我以下错误:

> roc(pred[,2],as.numeric(data$target))

'response' has more than two levels. Consider setting 'levels' 
explicitly or using 'multiclass.roc' insteadSetting levels: 
control = 0.166666666666667, case = 0.232876712328767
Setting direction: controls < cases

所以我很困惑我应该为预测设置什么格式以便正确计算 ROC?为什么我的函数显示这些错误?

4

1 回答 1

0

If you look at pROC's roc function documentation, you will see that the formal definition has the following form:

## Default S3 method:
roc(response, predictor, [...]

The prediction is therefore the second argument, not the first as you are using. Therefore your call should look like:

roc(data$target, pred[,2])

If you forget the order you can always use named argument in order to ignore the order:

roc(predictor = pred[,2], response = data$target)

Also note it is not necessary and even not recommended to convert the response to a numeric vector, so I removed as.numeric from the calls above.

于 2020-05-29T12:49:03.070 回答