[英]Variable selection in Random forest and prediction accuracy
我有一個橫截面數據集重復了 2 年,2009 年和 2010 年。我使用第一年(2009 年)作為訓練集來訓練回歸問題的隨機森林,第二年(2010 年)作為測試集.
df <- read.csv("https://www.dropbox.com/s/t4iirnel5kqgv34/df.cv?dl=1")
在 2009 年訓練隨機森林后,變量重要性表明變量x1
是最重要的變量。
set.seed(89)
rf2009 <- randomForest(y ~ x1 + x2 + x3 + x4 + x5 + x6,
data = df[df$year==2009,],
ntree=500,
mtry = 6,
importance = TRUE)
print(rf2009)
Call:
randomForest(formula = y ~ x1 + x2 + x3 + x4 + x5 + x6, data = df[df$year == 2009, ], ntree = 500, mtry = 6, importance = TRUE)
Type of random forest: regression
Number of trees: 500
No. of variables tried at each split: 6
Mean of squared residuals: 5208746
% Var explained: 75.59
imp.all <- as.data.frame(sort(importance(rf2009)[,1],decreasing = TRUE),optional = T)
names(imp.all) <- "% Inc MSE"
imp.all
% Inc MSE
x1 35.857840
x2 16.693059
x3 15.745721
x4 15.105710
x5 9.002924
x6 6.160413
然后我轉到測試集,我收到以下准確度指標。
test.pred.all <- predict(rf2009,df[df$year==2010,])
RMSE.forest.all <- sqrt(mean((test.pred.all-df[df$year==2010,]$y)^2))
RMSE.forest.all
[1] 2258.041
MAE.forest.all <- mean(abs(test.pred.all-df[df$year==2010,]$y))
MAE.forest.all
[1] 299.0751
然后當我在沒有變量x1
的情況下訓練 model 時,這是上面最重要的變量,並在測試集上應用經過訓練的 model ,我觀察到以下內容:
正如預期的那樣,用x1
解釋的方差高於沒有x1
的方差
但是沒有x1
的測試數據的RMSE
更好( RMSE
:2258.041 與x1
對比 1885.462 沒有x1
)
盡管如此,使用x1
(299.0751) 與不使用 x1 (302.3382) 相比, MAE
稍好一些。
rf2009nox1 <- randomForest(y ~ x2 + x3 + x4 + x5 + x6,
data = df[df$year==2009,],
ntree=500,
mtry = 5,
importance = TRUE)
print(rf2009nox1)
Call:
randomForest(formula = y ~ x2 + x3 + x4 + x5 + x6, data = df[df$year == 2009, ], ntree = 500, mtry = 5, importance = TRUE)
Type of random forest: regression
Number of trees: 500
No. of variables tried at each split: 5
Mean of squared residuals: 6158161
% Var explained: 71.14
imp.nox1 <- as.data.frame(sort(importance(rf2009nox1)[,1],decreasing = TRUE),optional = T)
names(imp.nox1) <- "% Inc MSE"
imp.nox1
% Inc MSE
x2 37.369704
x4 11.817910
x3 11.559375
x5 5.878555
x6 5.533794
test.pred.nox1 <- predict(rf2009nox1,df[df$year==2010,])
RMSE.forest.nox1 <- sqrt(mean((test.pred.nox1-df[df$year==2010,]$y)^2))
RMSE.forest.nox1
[1] 1885.462
MAE.forest.nox1 <- mean(abs(test.pred.nox1-df[df$year==2010,]$y))
MAE.forest.nox1
[1] 302.3382
我知道變量重要性是指訓練 model 而不是測試,但這是否意味着x1
變量不應包含在 model 中?
那么,我應該在 model 中包含x1
嗎?
我認為您需要有關 model 性能的更多信息。 只有一個測試樣本,您可以推測很多為什么沒有 x1 的 RMSE 會更好,盡管 x1 的重要性最高。 可能是變量之間的相關性或從訓練集中的噪聲中解釋。
要獲得更多信息,我建議查看袋外錯誤並使用交叉驗證進行超參數優化。 如果您在測試不同的測試數據集后看到相同的行為,您可以使用和不使用 x1 進行交叉驗證。
希望它有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.