r - 在 R 中使用 pROC 的随机森林拟合对象的 ROC 曲线,使用正或负“投票”作为预测器
问题描述
Obese 是一个二元响应变量,1 表示肥胖,0 表示不肥胖。体重是一个连续的预测指标。
使用 RF 对肥胖进行分类:
library(randomFores)
rf <- randomForest(factor(obese)~weight)
为我们提供了一个 fit 对象,其中包含:
> summary(rf)
Length Class Mode
call 2 -none- call
type 1 -none- character
predicted 100 factor numeric
err.rate 1500 -none- numeric
confusion 6 -none- numeric
votes 200 matrix numeric
oob.times 100 -none- numeric
classes 2 -none- character
importance 1 -none- numeric
importanceSD 0 -none- NULL
localImportance 0 -none- NULL
proximity 0 -none- NULL
ntree 1 -none- numeric
mtry 1 -none- numeric
forest 14 -none- list
y 100 factor numeric
test 0 -none- NULL
inbag 0 -none- NULL
terms 3 terms call
我相信投票矩阵显示了 rF 从 0 到 1 的投票数,用于将每个案例分类到任一类别;不肥胖=0,肥胖=1:
> head(rf$votes, 20)
0 1
1 0.9318182 0.06818182
2 0.9325843 0.06741573
3 0.2784091 0.72159091
4 0.9040404 0.09595960
5 0.3865979 0.61340206
6 0.9689119 0.03108808
7 0.8187135 0.18128655
8 0.7170732 0.28292683
9 0.6931217 0.30687831
10 0.9831461 0.01685393
11 0.3425414 0.65745856
12 1.0000000 0.00000000
13 0.9728261 0.02717391
14 0.9848485 0.01515152
15 0.8783069 0.12169312
16 0.8553459 0.14465409
17 1.0000000 0.00000000
18 0.3389831 0.66101695
19 0.9316770 0.06832298
20 0.9435897 0.05641026
服用那些:
votes_2 <- rf$votes[,2]
votes_1 <- rf$votes[,1]
我的问题是为什么这样做:
pROC::plot.roc(obese, votes_1)
和
pROC::plot.roc(obese, votes_2)
产生相同的结果。
解决方案
首先要意识到的是,ROC 分析并不关心数据的确切值。相反,它着眼于数据点的排名,以及排名如何分开。
其次,正如上面评论中提到的,0 类和 1 类的投票在每个观察中的总和为 1。这意味着在排名方面,两者是等价的(以排序方向为模)。
难题的最后一块是 pROC 不假设您提供预测变量作为属于正类的概率。相反,您可以传递任何类型的分数,并且会自动检测比较的方向。默认情况下这是静默完成的,但您可以通过将quiet
标志设置为来查看发生了什么FALSE
:
> pROC::roc(obese, votes_1, quiet = FALSE)
Setting levels: control = 0, case = 1
Setting direction: controls < cases
> pROC::roc(obese, votes_2, quiet = FALSE)
Setting levels: control = 0, case = 1
Setting direction: controls > cases
请注意votes_2
它如何检测到负类具有更高的值(基于中位数)并相应地设置比较的方向。
如果这不是您想要的,您始终可以明确设置类级别和方向参数:
> pROC::roc(obese, votes_2, levels = c(0, 1), direction = "<")
这将导致“反转”曲线显示votes_2
在检测具有较高值的正类时表现如何比随机差。
推荐阅读
- react-native - 在 React Native 中是否可以在多个视图上进行“拖动多选”类型的选择?
- c++ - 乘法表无法正常工作
- elasticsearch - 如何通过在 Elasticsearch 中搜索“food”找到单词“food2u”?
- javascript - 如何单击图标并做出反应 datepicker 将打开
- git - Git重命名初始分支:强制还是不强制?
- c# - JSON到数据表导致c#中出现此错误“读取DataTable时出现意外的JSON令牌>。预期的StartArray,得到字符串。路径”
- android-studio - 错误:找不到符号 (Android Java) (DataBinding)
- python - 如何为一组网址生成词云?
- rust - 如何将从配置文件加载的参数传递给程序宏函数?
- python - Geopandas 没有绘制正确的颜色