繁体   English   中英

随机森林中的变量选择和预测精度

[英]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稍好一些。

不包括 x1 的随机森林

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM