Excel IFERROR的R等同物是什么? [英] What is the R equivalent for Excel IFERROR?

查看:691
本文介绍了Excel IFERROR的R等同物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将IFERROR条件置于R中,就像Excel IFERROR函数一样。我正在建立一个随机的森林模型。要微调,我使用tuneRF功能。它有助于给出最佳的mtry参数。

 #选择最优MTRY参数
mtry < - tuneRF(dat3 [ -36],dat3 [,36],ntreeTry = 1000,stepFactor = 1.5,improvement = 0.01,trace = TRUE,plot = TRUE)
best.m< - mtry [mtry [,2] == min (mtry [,2]),1]

有时,上述函数返回错误if OOB错误在不同的迭代中不会改善。



if(改进>改进)中的错误{:缺少值TRUE / FALSE
需要。



下一步:如果上述功能正常工作,我在下面的代码中使用best.m的值。



在tuneRF函数中没有错误 - 运行下面的代码。

  rf< -randomForest(classe〜。,data = dat3,mtry = best.m,important = TRUE,ntree = 1000)

tuneRF功能中的错误 - 运行下面的代码

  #Train Random Forest 
rf< -randomForest(classe〜。,data = dat3,important = TRUE,ntree = 1000)

预期!任何帮助将被高度赞赏。

解决方案

您需要使用 try tryCatch 。这应该工作:

  mtry < -  try(tuneRF(dat3 [,-36],dat3 [,36],ntreeTry = $,
stepFactor = 1.5,改进= 0.01,trace = TRUE,plot = TRUE))
if(!inherits(mtry,try-error)){
best.m < - mtry [mtry [,2] == min(mtry [,2]),1]
rf< - randomForest(classe〜。,data = dat3,mtry = best.m,important = TRUE ,ntree = 1000)
} else {
rf< - randomForest(classe〜。,data = dat3,important = TRUE,ntree = 1000)
}

但是,给出的错误可能表示 tuneRF 函数中的错误。你能给出一个可重复的例子,即使用最小的数据集会产生错误?


I am trying to put IFERROR condition in R like Excel IFERROR Function. I am building a random forest model. To fine tune, i use tuneRF function. It helps to give optimal mtry parameter.

#Selecting Optimal MTRY parameter
mtry <- tuneRF(dat3[, -36], dat3[,36], ntreeTry=1000, stepFactor=1.5,improve=0.01, trace=TRUE, plot=TRUE)
best.m <- mtry[mtry[, 2] == min(mtry[, 2]), 1]

SOMETIMES, the above function returns error if OOB error would not improve in different iterations.

Error in if (Improve > improve) { : missing value where TRUE/FALSE needed.

Next Step : If the above function works fine, i use the value of best.m in the code below.

No ERROR in tuneRF function - Run the code below.

rf <-randomForest(classe~.,data=dat3, mtry=best.m, importance=TRUE,ntree=1000)

ERROR in tuneRF function - Run the code below.

#Train Random Forest
rf <-randomForest(classe~.,data=dat3, importance=TRUE,ntree=1000)

Thanks in anticipation! Any help would be highly appreciated.

解决方案

You need to use try or tryCatch. This should work:

mtry <- try(tuneRF(dat3[, -36], dat3[,36], ntreeTry=1000,
  stepFactor=1.5,improve=0.01, trace=TRUE, plot=TRUE))
if (!inherits(mtry, "try-error")) {
  best.m <- mtry[mtry[, 2] == min(mtry[, 2]), 1]
  rf <- randomForest(classe~.,data=dat3, mtry=best.m, importance=TRUE,ntree=1000)
} else {
  rf <- randomForest(classe~.,data=dat3, importance=TRUE,ntree=1000)
}

However, the error given may represent a bug in the tuneRF function. Can you give a reproducible example, i.e. with a minimal dataset that will produce the error?

这篇关于Excel IFERROR的R等同物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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