R-随机森林预测因预测变量中的NA而失败 [英] R- Random forest predict fails with NAs in predictors

查看:363
本文介绍了R-随机森林预测因预测变量中的NA而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档(如果我没看错的话)说,如果随机森林预测函数遇到某些观测值的NA预测因子,则会产生NA预测.

The documentation (If I'm reading it correctly) says that the random forest predict function produces NA predictions if it encounters NA predictors for certain observations.

注意:如果对象继承自randomForest.formula,则所有数据 在预测中默默地省略了带有NA的值.返回值 将在聚合树和单个树中相应地包含NA 预测(如果需要),但不在邻近矩阵或节点矩阵中

NOTE: If the object inherits from randomForest.formula, then any data with NA are silently omitted from the prediction. The returned value will contain NA correspondingly in the aggregated and individual tree predictions (if requested), but not in the proximity or node matrices

但是,如果我尝试在预测变量中使用某些NA的数据集上使用预测函数[2688中有7个观察结果中的NA],则会遇到以下错误情况,并且预测失败.

However, if I try to use the predict function on a dataset with some NA's in predictors [NA's in 7 observations out of 2688] I encounter the following error condition, and prediction fails.

predict.randomForest(模型, new.ds):newdata中缺少值

Error in predict.randomForest(model, new.ds) : missing values in newdata

如果可能的话,我想避免一些混乱的解决方法.

There is a slightly messy work-around that I would like to avoid if possible.

我在做/读错东西吗?它与"inherits from randomForest.formula"子句有什么关系吗?

Am I doing/reading something wrong? Does it have to do something with the "inherits from randomForest.formula" clause?

推荐答案

使用文档中的一些示例:

Using some examples from the documentation:

set.seed(1)
x <- data.frame(x1=gl(32, 5), x2=runif(160), y=rnorm(160))
rf1 <- randomForest(x[-3], x[[3]], ntree=10)
> inherits(rf1,"randomForest.formula")
[1] FALSE

> iris.rf <- randomForest(Species ~ ., data=iris, importance=TRUE,
                         proximity=TRUE)
> inherits(iris.rf,"randomForest.formula")
[1] TRUE

因此,您可能在未使用公式界面适合您的模型的情况下调用了randomForest.

So you probably called randomForest without using the formula interface to fit your model.

这篇关于R-随机森林预测因预测变量中的NA而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆