# predict.bagging

##### Predicts from a fitted bagging object

Classifies a dataframe using a fitted bagging object.

##### Usage

```
# S3 method for bagging
predict(object, newdata, newmfinal=length(object$trees), ...)
```

##### Arguments

- object
fitted model object of class

`bagging`

. This is assumed to be the result of some function that produces an object with the same named components as that returned by the`bagging`

function.- newdata
data frame containing the values at which predictions are required. The predictors referred to in the right side of

`formula(object)`

must be present by name in`newdata`

.- newmfinal
The number of trees of the bagging object to be used in the prediction. This argument allows the user to prune the ensemble. By default all the trees in the bagging object are used

- ...
further arguments passed to or from other methods.

##### Value

An object of class `predict.bagging`

, which is a list with the following components:

the formula used.

a matrix describing, for each observation, the number of trees that assigned it to each class.

a matrix describing, for each observation, the posterior probability or degree of support of each class. These probabilities are calculated using the proportion of votes in the final ensemble.

the class predicted by the ensemble classifier.

the confusion matrix which compares the real class with the predicted one.

returns the average error.

##### References

Alfaro, E., Gamez, M. and Garcia, N. (2013): ``adabag: An R Package for Classification with Boosting and Bagging''. Journal of Statistical Software, Vol 54, 2, pp. 1--35.

Alfaro, E., Garcia, N., Gamez, M. and Elizondo, D. (2008): ``Bankruptcy forecasting: An empirical comparison of AdaBoost and neural networks''. Decision Support Systems, 45, pp. 110--122.

Breiman, L. (1996): "Bagging predictors". Machine Learning, Vol 24, 2, pp. 123--140.

Breiman, L. (1998). "Arcing classifiers". The Annals of Statistics, Vol 26, 3, pp. 801--849.

##### See Also

##### Examples

```
# NOT RUN {
#library(rpart)
#data(iris)
#sub <- c(sample(1:50, 25), sample(51:100, 25), sample(101:150, 25))
#iris.bagging <- bagging(Species ~ ., data=iris[sub,], mfinal=5)
#iris.predbagging<- predict.bagging(iris.bagging, newdata=iris[-sub,])
#iris.predbagging
## rpart and mlbench libraries should be loaded
library(rpart)
library(mlbench)
data(BreastCancer)
l <- length(BreastCancer[,1])
sub <- sample(1:l,2*l/3)
BC.bagging <- bagging(Class ~.,data=BreastCancer[,-1],mfinal=5,
control=rpart.control(maxdepth=3))
BC.bagging.pred <- predict.bagging(BC.bagging,newdata=BreastCancer[-sub,-1])
BC.bagging.pred$prob
BC.bagging.pred$confusion
BC.bagging.pred$error
# }
```

*Documentation reproduced from package adabag, version 4.2, License: GPL (>= 2)*