本文介绍了如何在 R 中使用缺失值的随机森林?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

library(randomForest)
rf.model <- randomForest(WIN ~ ., data = learn)

我想拟合随机森林模型,但出现此错误:

I would like to fit a random forest model, but I get this error:

Error in na.fail.default(list(WIN = c(2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L,  :
missing values in object

我有 16 个数字属性学习数据框,WIN 是一个因子,级别为 0 1.

I have data frame learn with 16 numeric atributes and WIN is a factor with levels 0 1.

推荐答案

我对这个问题的最初反应是它没有显示出太多的研究工作,因为每个人"都知道随机森林不处理预测变量中的缺失值.但是在检查 ?randomForest 时,我必须承认它可以更明确地说明这一点.

My initial reaction to this question was that it didn't show much research effort, since "everyone" knows that random forests don't handle missing values in predictors. But upon checking ?randomForest I must confess that it could be much more explicit about this.

(尽管,文档中链接到的 Breiman 的 PDF 确实明确说缺失值根本没有被处理.)

(Although, Breiman's PDF linked to in the documentation does explicitly say that missing values are simply not handled at all.)

我能看到的官方文档中唯一明显的线索是na.action参数的默认值是na.fail,这可能太神秘了新用户.

The only obvious clue in the official documentation that I could see was that the default value for the na.action parameter is na.fail, which might be too cryptic for new users.

无论如何,如果您的预测变量有缺失值,您(基本上)有两种选择:

In any case, if your predictors have missing values, you have (basically) two choices:

  1. 使用不同的工具(rpart 可以很好地处理缺失值.)
  2. 估算缺失值
  1. Use a different tool (rpart handles missing values nicely.)
  2. Impute the missing values

毫不奇怪,randomForest 包有一个函数可以做到这一点,rfImpute.?rfImpute 中的文档贯穿了其使用的基本示例.

Not surprisingly, the randomForest package has a function for doing just this, rfImpute. The documentation at ?rfImpute runs through a basic example of its use.

如果只有少数案例缺少值,您也可以尝试设置 na.action = na.omit 以简单地删除这些案例.

If only a small number of cases have missing values, you might also try setting na.action = na.omit to simply drop those cases.

当然,这个答案有点猜测您的问题真的只是缺少值.

And of course, this answer is a bit of a guess that your problem really is simply having missing values.

这篇关于如何在 R 中使用缺失值的随机森林?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 18:51