[英]randomForest using factor variables as continuous?
我正在使用包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 次其他迭代,但之前從未收到此消息。 我發現這次運行中的 randomforest 對象與其他運行中的唯一不同的the rf.full$forest$ncat
組件存儲為雙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
但是,xlevels(似乎是所使用的預測變量及其類型的列表)都顯示了每個預測變量的正確數據類型。
> 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 組件的數字類型(雙精度 vs 整數)是否與模型構建有任何關系,以及任何可能導致前 6 次運行中的整數轉換為雙精度的想法最后一次運行(每次運行包含相同數據的不同子集)?
randomforest::randomForest
算法對低基數(最多 32 個類別)和高基數(32 到 64? 個類別)類別拆分進行編碼。 請注意 - 所有“有問題”的功能都屬於后一類,並使用 64 位浮點值進行編碼。
雖然控制台輸出對人類觀察者沒有意義,但randomForest
模型對象/算法本身是正確的(即,將這些變量視為分類變量),並且正在做出正確的預測。
如果您想研究決策樹的結構和決策樹集成模型,那么您可以考慮將它們導出為 PMML 數據格式。 例如,您可以為此使用R2PMML包:
library("r2pmml")
r2pmml(rf.full, "MyRandomForest.pmml")
然后,在文本編輯器中打開 MyRandomForest.pmml,您將對模型的內部結構(分支、拆分條件、葉值等)有一個很好的概述。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.