使用r proc的ROC曲线用于随机森林拟合对象,以使用“态度”或“否定”作为预测指标



肥胖是二进制响应var,其中1表示肥胖,0不是肥胖。重量是连续的预测因子。

使用RF分类肥胖:

library(randomFores)
rf <- randomForest(factor(obese)~weight)

给我们一个包含:

的适合对象
> 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  

我相信投票矩阵显示了从0到1的投票,RF给出了将每个案例分类为任何一个班级的票数;不肥胖= 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分析并不关心数据的确切值。相反,它查看数据点上的排名以及排名如何分开。

第二,正如上面的评论中所述,每次观察中最多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在检测具有较高值的正类别时的性能要比随机差。

最新更新