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:
- First, you need to install and load the ipred package:
> install.packages("ipred") > library(ipred)
- 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
- Obtain an out of bag estimate of misclassification of the errors:
> mean(predict(churn.bagging) != trainset$churn) Output [1] 0.06047516
- 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")
- 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