我正在使用软件包randomForest
为物种制作栖息地适宜性模型。在我开始用getTree()
查看单个树之前,我认为一切都在正常工作。文档(见randomForest的第4页)指出,对于分类变量,拆分点将是一个整数,这是有意义的。然而,在我看过的树上,我的结果却不是这样。
我用来构建模型的数据框架被格式化为作为因素的分类变量:
> str(df.full)
'data.frame': 27087 obs. of 23 variables:
$ sciname : Factor w/ 2 levels "Laterallus jamaicensis",..: 1 1 1 1 1 1 1 1 1 1 ...
$ estid : Factor w/ 2 levels "7694","psabs": 1 1 1 1 1 1 1 1 1 1 ...
$ pres : Factor w/ 2 levels "1","0": 1 1 1 1 1 1 1 1 1 1 ...
$ stratum : Factor w/ 89 levels "poly_0","poly_1",..: 1 1 1 1 1 1 1 1 1 1 ...
$ ra : Factor w/ 3 levels "high","low","medium": 3 3 3 3 3 3 3 3 3 3 ...
$ eoid : Factor w/ 2 levels "0","psabs": 1 1 1 1 1 1 1 1 1 1 ...
$ avd3200 : num 0.1167 0.0953 0.349 0.1024 0.3765 ...
$ biocl05 : num 330 330 330 330 330 ...
$ biocl06 : num 66 65.8 66 65.8 66 ...
$ biocl08 : num 277 277 277 277 277 ...
$ biocl09 : num 170 170 170 170 170 ...
$ biocl13 : num 186 186 185 186 185 ...
$ cti : num 19.7 19 10.4 16.4 14.7 ...
$ dtnhdwat : num 168 240 39 206 309 ...
$ dtwtlnd : num 0 0 0 0 0 0 0 0 0 0 ...
$ e2em1n99 : num 0 0 0 0 0 0 0 0 0 0 ...
$ ems30_53 : Factor w/ 53 levels "0","602","2206",..: 19 4 17 4 19 19 4 4 19 19 ...
$ ems5607_46: num 0 0 1 0 0.4 ...
$ ksat : num 0.21 0.21 0.21 0.21 0.21 ...
$ lfevh_53 : Factor w/ 53 levels "0","11","16",..: 38 38 38 38 38 38 38 38 38 38 ...
$ ned : num 1.46 1.48 1.54 1.48 1.47 ...
$ soilec : num 14.8 14.8 19.7 14.8 14.8 ...
$ wtlnd_53 : Factor w/ 50 levels "0","3","7","11",..: 4 31 7 31 7 31 7 7 31 31 ...
这是函数调用:
# rfStratum and sampSizeVec were previously defined
> rf.full$call
randomForest(x = df.full[, c(7:23)], y = df.full[, 3],
ntree = 2000, mtry = 7, replace = TRUE, strata = rfStratum,
sampsize = sampSizeVec, importance = TRUE, norm.votes = TRUE)
下面是示例树的前15行(请注意,第1、5和15行中的变量应该是绝对的,即它们应该有整数拆分值):
> tree100
left daughter right daughter split var split point status prediction
1 2 3 ems30_53 9.007198e+15 1 <NA>
2 4 5 biocl08 2.753206e+02 1 <NA>
3 6 7 biocl06 6.110518e+01 1 <NA>
4 8 9 biocl06 1.002722e+02 1 <NA>
5 10 11 lfevh_53 9.006718e+15 1 <NA>
6 0 0 <NA> 0.000000e+00 -1 0
7 12 13 biocl05 3.310025e+02 1 <NA>
8 14 15 ned 2.814818e+00 1 <NA>
9 0 0 <NA> 0.000000e+00 -1 1
10 16 17 avd3200 4.199712e-01 1 <NA>
11 18 19 e2em1n99 1.724138e-02 1 <NA>
12 20 21 biocl09 1.738916e+02 1 <NA>
13 22 23 ned 8.837864e-01 1 <NA>
14 24 25 biocl05 3.442437e+02 1 <NA>
15 26 27 lfevh_53 9.007199e+15 1 <NA>
附加信息:我遇到这种情况是因为我正在调查预测结果返回到研究区域时所遇到的一个错误,说明新数据中的预测器类型与培训数据的类型不匹配。我使用相同的数据框架和脚本(只是使用不同的预测器子集)对该模型进行了6次其他迭代,而且以前从未收到过此消息。与其他运行相比,我发现这次运行中的随机森林对象之间唯一不同的地方是,the rf.full$forest$ncat
组件存储为双倍而不是整数。
> for(i in 1:length(rf.full$forest$ncat)){
+ cat(names(rf.full$forest$ncat)[[i]], ": ", class(rf.full$forest$ncat[[i]]), "\n")
+ }
avd12800 : numeric
cti : numeric
dtnhdwat : numeric
dtwtlnd : numeric
ems2207_99 : numeric
ems30_53 : numeric
ems5807_99 : numeric
hydgrp : numeric
ksat : numeric
lfevh_53 : numeric
ned : numeric
soilec : numeric
wtlnd_53 : numeric
>
> rf.full$forest$ncat
avd12800 cti dtnhdwat dtwtlnd ems2207_99 ems30_53 ems5807_99 hydgrp ksat lfevh_53
1 1 1 1 1 53 1 1 1 53
ned soilec wtlnd_53
1 1 50
但是,xlevel(它似乎是所使用的预测变量及其类型的列表)都显示了每个预测器的正确数据类型。
> for(i in 1:length(rf.full$forest$xlevels)){
+ cat(names(rf.full$forest$xlevels)[[i]], ": ", class(rf.full$forest$xlevels[[i]]),"\n")
+ }
avd12800 : numeric
cti : numeric
dtnhdwat : numeric
dtwtlnd : numeric
ems2207_99 : numeric
ems30_53 : character
ems5807_99 : numeric
hydgrp : character
ksat : numeric
lfevh_53 : character
ned : numeric
soilec : numeric
wtlnd_53 : character
# example continuous predictor
> rf.full$forest$xlevels$avd12800
[1] 0
# example categorical predictor
> rf.full$forest$xlevels$ems30_53
[1] "0" "602" "2206" "2207" "4504" "4507" "4702" "4704" "4705" "4706" "4707" "4717" "5207" "5307" "5600"
[16] "5605" "5607" "5616" "5617" "5707" "5717" "5807" "5907" "6306" "6307" "6507" "6600" "7002" "7004" "9107"
[31] "9116" "9214" "9307" "9410" "9411" "9600" "4607" "4703" "6402" "6405" "6407" "6610" "7005" "7102" "7104"
[46] "7107" "9000" "9104" "9106" "9124" "9187" "9301" "9505"
ncat组件只是每个变量的类别数的向量,而连续变量(如前所述)为1,因此,如果将其存储为整数或双变量,似乎并不重要,但这一切似乎都是相关的。
问题
1)在randomForest林的任何给定树中,范畴预测器的拆分点不应该是一个整数吗?如果是的话,对于为什么此处用作randomForest调用输入的数据帧中的因素不被用作整数,有什么想法吗?
( 2) randomForest对象的ncat组件的数字类型(双对整数)在任何方面是否与建模有关,以及如何使它在前6次运行中从整数切换到在最后一次运行中加倍(每次运行包含相同数据的不同子集)?
发布于 2019-03-08 13:02:07
randomforest::randomForest
算法编码低基数(最多32类)和高基数(32到64?类别)分类划分不同。注意-所有“有问题”的特性都属于后一类,并使用64位浮点值进行编码。
虽然控制台输出对人工观察者没有意义,但randomForest
模型对象/算法本身是正确的(即。将这些变量视为绝对变量),并作出正确的预测。
如果您想研究决策树和决策树集成模型的结构,那么可以考虑将它们导出为PMML数据格式。例如,您可以为此使用R2PMML包:
library("r2pmml")
r2pmml(rf.full, "MyRandomForest.pmml")
然后,在文本编辑器中打开MyRandomForest.pmml,您将对模型的内部(分支、拆分条件、叶值等)有一个很好的概述。
https://stackoverflow.com/questions/55070733
复制