如何消除在使用randomForest进行预测时出现的“外部函数调用中的NA/NaN/Inf (arg 7)”错误

18

我进行了广泛的研究,但没有找到解决方案。我已按如下方式清理了我的数据集:

library("raster")
impute.mean <- function(x) replace(x, is.na(x) | is.nan(x) | is.infinite(x) , 
mean(x, na.rm = TRUE))
losses <- apply(losses, 2, impute.mean)
colSums(is.na(losses))
isinf <- function(x) (NA <- is.infinite(x))
infout <- apply(losses, 2, is.infinite)
colSums(infout)
isnan <- function(x) (NA <- is.nan(x))
nanout <- apply(losses, 2, is.nan)
colSums(nanout)

运行预测算法时出现问题:

options(warn=2)
p  <-   predict(default.rf, losses, type="prob", inf.rm = TRUE, na.rm=TRUE, nan.rm=TRUE)

所有研究都说数据中应该有NA、Inf或NaN,但我没有找到。我会将数据和randomForest摘要提供给您进行调查,网址为[删除]。 Traceback并没有透露太多信息(至少对我而言):

4: .C("classForest", mdim = as.integer(mdim), ntest = as.integer(ntest), 
       nclass = as.integer(object$forest$nclass), maxcat = as.integer(maxcat), 
       nrnodes = as.integer(nrnodes), jbt = as.integer(ntree), xts = as.double(x), 
       xbestsplit = as.double(object$forest$xbestsplit), pid = object$forest$pid, 
       cutoff = as.double(cutoff), countts = as.double(countts), 
       treemap = as.integer(aperm(object$forest$treemap, c(2, 1, 
           3))), nodestatus = as.integer(object$forest$nodestatus), 
       cat = as.integer(object$forest$ncat), nodepred = as.integer(object$forest$nodepred), 
       treepred = as.integer(treepred), jet = as.integer(numeric(ntest)), 
       bestvar = as.integer(object$forest$bestvar), nodexts = as.integer(nodexts), 
       ndbigtree = as.integer(object$forest$ndbigtree), predict.all = as.integer(predict.all), 
       prox = as.integer(proximity), proxmatrix = as.double(proxmatrix), 
       nodes = as.integer(nodes), DUP = FALSE, PACKAGE = "randomForest")
3: predict.randomForest(default.rf, losses, type = "prob", inf.rm = TRUE, 
       na.rm = TRUE, nan.rm = TRUE)
2: predict(default.rf, losses, type = "prob", inf.rm = TRUE, na.rm = TRUE, 
       nan.rm = TRUE)
1: predict(default.rf, losses, type = "prob", inf.rm = TRUE, na.rm = TRUE, 
       nan.rm = TRUE)

没有更多关于森林本身的信息很难判断(您的文件只包含数据)。但我想知道您从哪里得到了predict.randomForest的参数inf.rmna.rmnan.rm的想法。它们在文档中肯定没有提到。 - joran
压缩文件包含了RF摘要。它已不再可用。NA、Inf和NaN是缺失或无法计算的数据形式,可能会阻止RF运行。Nate的答案有效。 - Elliott
我非常清楚NA、Inf和NaN是什么。我指出的是,对于那个预测函数来说,这些参数根本不存在。它们被完全忽略了。 - joran
@joran 问题是它们没有被忽略,谢谢。 - Elliott
1
我不明白我说的任何话怎么可能被视为敌意,但如果你这样看待了,我很抱歉。也许我们之间有些误解。预测语句没有运行,因为(如下面正确答案中指出的)你没有完全删除NAs、NaNs等。但是inf.rm = TRUE, na.rm=TRUE, nan.rm=TRUE参数确实被忽略了,根本没有任何效果。那就是我的观点。你必须手动删除这些值;predict.randomForest没有这些名称的参数。 - joran
显示剩余3条评论
2个回答

16

你的代码不完全可重现(没有运行实际的randomForest算法),但是你没有用列向量的均值替换Inf值。这是因为在调用impute.mean函数中的mean()时,na.rm = TRUE参数确切地执行它所说的--删除NA值(而不是Inf值)。

例如,你可以看到:

impute.mean <- function(x) replace(x, is.na(x) | is.nan(x) | is.infinite(x), mean(x, na.rm = TRUE))
losses <- apply(losses, 2, impute.mean)
sum( apply( losses, 2, function(.) sum(is.infinite(.))) )
# [1] 696

要消除无限值,请使用:

impute.mean <- function(x) replace(x, is.na(x) | is.nan(x) | is.infinite(x), mean(x[!is.na(x) & !is.nan(x) & !is.infinite(x)]))
losses <- apply(losses, 2, impute.mean)
sum(apply( losses, 2, function(.) sum(is.infinite(.)) ))
# [1] 0

13

出现错误消息的一个原因是:

在调用外部函数时出现NA/NaN/Inf(参数X)

当训练随机森林时,你的数据框中有character类型的变量。如果出现以下警告:

强制转换导致引入NAs

请检查所有字符变量是否已转换为因子。

示例

set.seed(1)
dat <- data.frame(
  a = runif(100),
  b = rpois(100, 10),
  c = rep(c("a","b"), 100),
  stringsAsFactors = FALSE
)

library(randomForest)
randomForest(a ~ ., data = dat)

产生如下错误:

在调用外部函数时出现 NA/NaN/Inf (参数 1)。 此外:警告消息:在 data.matrix(x) 中:强制转换时引入了 NAs

但将其更改为 stringsAsFactors = TRUE 后,它就可以运行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接