问题
I have a model generated from a random forest. Inside it, there is a attribute called call, that will give me the what was actually the randomForest called function.
I want to get this parameter, remove one column from the model and run it again.
ex:
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)
I have tried converting to a list, pasting the new argument and then adding it again to iris.rf[[2]], but it paste in all parts of the formula.
I cannot get rid of the class call, to change it and then call eval() to run it again.
回答1:
You could use parse
on the paste0
object to get a new expression. You could then evaluate that new object as a call.
Maybe something like this:
> 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
Note that eval(parse(text = ...))
is not something that is recommended, although it does work nicely for this job.
来源:https://stackoverflow.com/questions/25475519/get-a-call-object-change-parameters-and-run-it-again-with-the-new-parameters