【问题标题】:get a call object, change parameters and run it again with the new parameters获取一个调用对象,更改参数并使用新参数再次运行它
【发布时间】:2014-10-17 23:45:55
【问题描述】:

我有一个从随机森林生成的模型。在其中,有一个名为 call 的属性,它会告诉我 randomForest 调用的函数。

我想获取这个参数,从模型中删除一列并再次运行它。

例如:

library(randomForest)
data(iris)
iris.rf <- randomForest(Species~.-Sepal.Length,  data=iris, prox=TRUE)
iris.rf$call

# want to remove the field Sepal.length as well
# the call should be then 
# randomForest(Species~.-Sepal.Length-Sepal.Width,  data=iris, prox=TRUE)

我尝试转换为列表,粘贴新参数,然后再次将其添加到 iris.rf[[2]],但它粘贴在公式的所有部分。

我无法摆脱类调用,更改它然后调用 eval() 再次运行它。

【问题讨论】:

标签: r machine-learning call dynamically-generated random-forest


【解决方案1】:

您可以在paste0 对象上使用parse 来获取新表达式。然后,您可以将该新对象评估为调用。

可能是这样的:

> iris.rf$call[[2]][3] <- parse(text = with(iris.rf, {
      paste0(call[[2]][3], " - ", rownames(importance)[1])
  }))
> eval(iris.rf$call)
#
# Call:
#  randomForest(formula = Species ~ . - Sepal.Length - Sepal.Width,      
#               data = iris, prox = TRUE) 
#                Type of random forest: classification
#                      Number of trees: 500
# No. of variables tried at each split: 1
#
#         OOB estimate of  error rate: 3.33%
# Confusion matrix:
#            setosa versicolor virginica class.error
# setosa         50          0         0        0.00
# versicolor      0         47         3        0.06
# virginica       0          2        48        0.04

请注意,不推荐使用eval(parse(text = ...)),尽管它确实适用于这项工作。

【讨论】:

  • 快到了。我想以编程方式进行。该程序将获取当前使用的列,然后删除最不重要的列(我有列名,在订购 iris.rf$importance 之后)。
  • @AdrianoAlmeida - 查看我的更新,虽然它可能并不理想
  • 是的,你成功了。非常感谢!!
  • 哦,太好了...很高兴为您提供帮助。 :-)
猜你喜欢
  • 1970-01-01
  • 2014-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-10
  • 2013-11-14
  • 1970-01-01
  • 2012-10-15
相关资源
最近更新 更多