There's more...

Besides adabag, the ipred package provides a bagging method for a classification tree. We demonstrate here how to use the bagging method of the ipred package to train a classification model:

  1. First, you need to install and load the ipred package:
        > install.packages("ipred")
        > library(ipred)
  1. You can then use the bagging method to fit the classification method:
        > churn.bagging = bagging(churn ~ ., data = trainset, coob = T)
        > churn.bagging
        Output
        Bagging classification trees with 25 bootstrap replications 
    
        Call: bagging.data.frame(formula = churn ~ ., data = trainset, coob
= T) Out-of-bag estimate of misclassification error: 0.0605
  1. Obtain an out of bag estimate of misclassification of the errors:
        > mean(predict(churn.bagging) != trainset$churn)
        Output
        [1] 0.06047516  
  1. You can then use the predict function to obtain the predicted labels of the testing dataset:
        > churn.prediction = predict(churn.bagging, newdata=testset,
type="class")
  1. Obtain the classification table from the labels of the testing dataset and prediction results:
        > prediction.table = table(churn.prediction, testset$churn)
        Output
        churn.prediction yes  no
                     no   31 869
                     yes 110   8 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset