mab
mab

Reputation: 2774

Perform cross-validation on randomForest with R

I am using the randomForest package for R to train a model for classification. To compare it to other classifiers, I need a way to display all the information given by the rather verbose cross-validation method in Weka. Therefore, the R script should output somesthing like [a] from Weka.

  1. Is there a way to validate an R model via RWeka to produce those measures?
  2. If not, how is a cross-validation on a random forest done purely in R?
  3. Is it possble to use rfcv from the randomForest package here? I could not get it to work.

I do know that the out-of-bag error (OOB) used in randomForest is some kind of a cross-validation. But I need the full information for a suited comparison.

What I tried so far using R is [b]. However, the code also produces an error on my setup [c] due to missing values.

So, can you help me with the cross-validation?


Appendix

[a] Weka:

=== Stratified cross-validation ===
=== Summary ===

Correctly Classified Instances        3059               96.712  %
Incorrectly Classified Instances       104                3.288  %
Kappa statistic                          0.8199
Mean absolute error                      0.1017
Root mean squared error                  0.1771
Relative absolute error                 60.4205 %
Root relative squared error             61.103  %
Coverage of cases (0.95 level)          99.6206 %
Mean rel. region size (0.95 level)      78.043  %
Total Number of Instances             3163     

=== Detailed Accuracy By Class ===

                 TP Rate  FP Rate  Precision  Recall   F-Measure  MCC      ROC Area  PRC Area  Class
                 0,918    0,028    0,771      0,918    0,838      0,824    0,985     0,901     sick-euthyroid
                 0,972    0,082    0,991      0,972    0,982      0,824    0,985     0,998     negative
Weighted Avg.    0,967    0,077    0,971      0,967    0,968      0,824    0,985     0,989     

=== Confusion Matrix ===

    a    b   <-- classified as
  269   24 |    a = sick-euthyroid
   80 2790 |    b = negative

[b] Code so far:

library(randomForest) #randomForest() and rfImpute()
library(foreign) # read.arff()
library(caret) # train() and trainControl()

nTrees <- 2 # 200
myDataset <- 'D:\\your\\directory\\SE.arff' # http://hakank.org/weka/SE.arff

mydb = read.arff(myDataset)
mydb.imputed <- rfImpute(class ~ ., data=mydb, ntree = nTrees, importance = TRUE)
myres.rf <- randomForest(class ~ ., data=mydb.imputed, ntree = nTrees, importance = TRUE)
summary(myres.rf)

# specify type of resampling to 10-fold CV
fitControl <- trainControl(method = "rf",number = 10,repeats = 10)
set.seed(825)

# deal with NA | NULL values in categorical variables
#mydb.imputed[is.na(mydb.imputed)] <- 1
#mydb.imputed[is.null(mydb.imputed)] <- 1

rfFit <- train(class~ ., data=mydb.imputed,
             method = "rf",
             trControl = fitControl,
             ## This last option is actually one
             ## for rf() that passes through
             ntree = nTrees, importance = TRUE, na.action = na.omit)
rfFit

The error is:

Error in names(resamples) <- gsub("^\\.", "", names(resamples)) : 
  attempt to set an attribute on NULL

Using traceback()

5: nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo, 
       method = models, ppOpts = preProcess, ctrl = trControl, lev = classLevels, 
       ...)
4: train.default(x, y, weights = w, ...)
3: train(x, y, weights = w, ...)
2: train.formula(class~ ., data = mydb.imputed, method = "rf", 
       trControl = fitControl, ntree = nTrees, importance = TRUE, 
       sampsize = rep(minorityClassNum, 2), na.action = na.omit)
1: train(class~ ., data = mydb.imputed, method = "rf", trControl = fitControl, 
       ntree = nTrees, importance = TRUE, sampsize = rep(minorityClassNum, 
           2), na.action = na.omit) at #39

[c] R version information via sessionInfo()

R version 3.1.0 (2014-04-10)
Platform: i386-w64-mingw32/i386 (32-bit)

[...]

other attached packages:
 [1] e1071_1.6-3        caret_6.0-30       ggplot2_1.0.0      foreign_0.8-61     randomForest_4.6-7 DMwR_0.4.1        
 [7] lattice_0.20-29    JGR_1.7-16         iplots_1.1-7       JavaGD_0.6-1       rJava_0.9-6

Upvotes: 3

Views: 3409

Answers (1)

Koundy
Koundy

Reputation: 5503

I dont know about weka, but i have done randomForest modelling in R and I have always used predict function in R to do this.

Try using this function

predict(Model,data)

Bind the output with original values and use table command to get the confusion matrix.

Upvotes: 2

Related Questions