in R: Error in is.data.frame(data) : object '' not found, C5.0 plot

点点圈 提交于 2019-12-05 11:44:14

There does appear to be a bug in the code when it comes to evaluating the command in the proper environment. The problem appears to be in the C50::model.frame.C5.0 function. The "cleanest" work around I could find was to add a terms property to your model. This will help encapsulate the function environment.

f <- function(){
    library(C50)
    set.seed(1)
    class = c(1,2)
    d <- data.frame(feature1 = sample(1:10,10,replace=TRUE), feature2 = 1:10, binclass = class)
    d$binclass <- as.factor(d$binclass)
    model <- C5.0(binclass ~ ., data=d)
    model$terms <- eval(model$call$formula)   #<---- Added line
    plot(model)   
}

@MrFlick almost had it but not quite. This problem for plotting is particularly annoying when trying to pass arbitrary data and target features to the C50 method. As pointed out by MrFlick it was to do with renaming terms. By renaming the x and y terms in the method call the plotting function won't get confused.

tree_model$call$x <- data_train[, -target_index]
tree_model$call$y <- data_train[[target_feature]] 

For example, here is a method for passing in arbitrary data and a target feature and still being able to plot the result:

boosted_trees <- function(data_train, target_feature, iter_choice) {

    target_index <- grep(target_feature, colnames(data_train))
    model_boosted <- C5.0(x = data_train[, -target_index], y = data_train[[target_feature]], trial=iter_choice)
    model_boosted$call$x <- data_train[, -target_index]
    model_boosted$call$y <- data_train[[target_feature]]
    return(model_boosted)

}

The model object returned by the above method can be plotted as normal.

model <- boosted_trees(data_train, 'my_target', 10)
plot(model)

You can use the special assignment operator <<- instead of the standard one (<-). It will save the object to the global environment and that can solve your problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!