Search code examples
rfunctionplyrrpart

Running rpart over multiple subsets of a data frame


I'm creating a decision tree with the R rpart package based on x number of variables and a dataframe:

fit<-rpart(y~x1+x2+x3+x4,data=(mydataframe),
  control=rpart.control(minsplit = 20, minbucket = 0, cp=.01))

But instead of using the entire dataframe, I have four or five subsets of data that are factors, let's say separated out by x4. How can I run decision trees on all of these factors at once instead of having to call subsets of the data again and again?

Based on a search of SO, it looks like either BY or ddply might be the right choice. Here's what I've tried for ddply:

fit<-ddply(mydataframe, dataframe$x4, function (df)  
    rpart(y~x1+x2+x3+x4,data=(df), 
    control=rpart.control(minsplit = 20, minbucket = 0, cp=.01)))

but what I'm getting back is:

Error in eval(expr, envir, enclos) : object 'x4value' not found

where x4value is one of the variable values I'd like to split out by. So I have a column of values:

x4
BucketName1
BucketName2
BucketName3
BucketName4

str(mydataframe) shows that $x4 is a : Factor w/ 8 levels and no symbols.

Additionally, I ran mydataframe = na.omit(dataframe) at the very beginning to avoid nulls.

Possible issues I've already troubleshooted:

The rpart bit runs fine when I run it manually as such:

mydataframe<-subset(trainData, x4=="BucketName1")

fit<-rpart(y~x1+x2+x3+x4,data=(mydataframe), 
    control=rpart.control(minsplit = 20, minbucket = 0, cp=.01))

but borks whenever I try to loop through all subsets using ddply.

Complete reproducible sample code:

mydataframe<-data.frame  ( x1=sample(1:10),
                           x2=sample(1:10),
                           x3=sample(1:10),
                           x4= sample(letters[1:4], 20, replace = TRUE))
str(mydataframe)

fit<-ddply(mydataframe, mydataframe$x4, function (df)
    rpart(y~x1+x2+x3+x4,data=(df), control=rpart.control(minsplit = 20,      minbucket = 0, cp=.01)))

Output:

str(mydataframe) 'data.frame':  20 obs. of  4 variables:  $ x1: int  1 6 8 4 7 9 3 2 10 5 ...  $ x2: int  9 4 5 8 6 3 7 10 2 1 ...  $ x3: int 2 6 5 3 1 4 9 7 10 8 ...  $ x4: Factor w/ 4 levels "a","b","c","d": 4 4 3 2 3 4 3 3 1 3 ...
> fit<-ddply(mydataframe, mydataframe$x4, function (df) rpart(y~x1+x2+x3+x4,data=(df), control=rpart.control(minsplit = 20, minbucket = 0, cp=.01))) Error in eval(expr, envir, enclos) : object 'd' not found

Solution

  • You want to do two things with your code:

    1. Use dlply instead of ddply, since you want a list of rpart objects instead of a data frame of (?). ddply would be useful if you wanted to show predicted values of the original data, since that can be formatted into a data frame.

    2. Use .(x4) instead of dataframe$x4 in the dlply. Using the latter will produce unpredictable results.

    Additionally, in your example, you should specify a y value and remove the .... from after x4