Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This question is similar to some other questions on Stackoverflow (here, here and here), but different enough so that I cannot extrapolate those answers to my case.

I have a function in which I fit a C5.0 model and than try to plot the model.

train_d <- globald[train_ind,c(features,21)]
model <- C5.0(binclass ~ .,data=train_d,trials=10)

binclass is a column name in my training/test data (globald is a dataframe from which I subset rows with _ind indices and columns c(3:12,21), where column 21 is named binclass). Fitting works well. However, when I also add the line

plot(model,trial=0)

then I get the following error: Error in is.data.frame(data) : object 'train_d' not found.

How is it possible that when fitting the model, train_d is found and used correctly, but while plotting, train_d is nowhere to be found? And, any suggestion of how to solve this issue. Namespaces in [r] remain a mystery to me.

A minimal running example is the following:

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)
    plot(model)   
}

Calling f() results in the following error: Error in is.data.frame(data) : object 'd' not found

Edit: As per the answer from MrFlick, it seems that the cause of this problem is a bug in the C5.0 code. There are some workarounds are indicated by Pascal and MrFlick.

like image 805
user989762 Avatar asked Jul 31 '15 04:07

user989762


People also ask

Is a data frame an object in R?

A data. frame object in R has similar dimensional properties to a matrix but it may contain categorical data, as well as numeric. The standard is to put data for one sample across a row and covariates as columns. On one level, as the notation will reflect, a data frame is a list.

Can you plot a Dataframe in R?

We just have to pass our dataframe in the plot. ts() function, and it will plot all the dataframe columns in a time series plot. In the Y-axis we can see the names of the columns of our dataframe.

Which objects in R are used to store tabular data?

Dataframes. Dataframes are generic data objects of R which are used to store the tabular data.

What is a data frame?

A DataFrame is a data structure that organizes data into a 2-dimensional table of rows and columns, much like a spreadsheet. DataFrames are one of the most common data structures used in modern data analytics because they are a flexible and intuitive way of storing and working with data.


2 Answers

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)   
}
like image 87
MrFlick Avatar answered Sep 30 '22 16:09

MrFlick


@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)
like image 29
Cybernetic Avatar answered Sep 30 '22 15:09

Cybernetic