r - 如何消除 "NA/NaN/Inf in foreign function call (arg 7)"使用 randomForest 运行预测

标签 r runtime-error random-forest predict

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

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 摘要提供给 [已删除] 进行调查 回溯并没有透露太多信息(无论如何对我来说):

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)

最佳答案

您的代码并不完全可重现(没有运行实际的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

关于r - 如何消除 "NA/NaN/Inf in foreign function call (arg 7)"使用 randomForest 运行预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21964078/

相关文章:

在 R 中删除 spplot 上的色标?

R Notebook 在呈现时拆分代码块

php - 在php中上传文件时出错

r - 我们可以创建一个比 R 中的值多 n 个级别的因子变量子集吗?

python - 随机森林回归 - 如何分析其性能? - python ,sklearn

r - 如何使用 ggplot2 绘制 'segmented' 包的结果?

r - 谁能解释为什么我在file(file, “r”): cannot open the connection?中出错

php - 如何处理错误或异常到PHP?

java - 数独 block 检查器 Java

python - 如何使用不同的数据集进行 GridSearchCV 训练和测试?