# How to use rpart's result in prediction

This may be a simple question but I stuck in this problem. I am using Recursive Partitioning (rpart) package in R for building a classification tree. I generated a tree from a sample data (for testing rpart). I fit the sample data using rpart's formula

   fit =  rpart(formula, data=, method=,control=)


This gave me the classification tree. I can see the summary, plot the tree, plot the result. But my question is how can I use its result for prediction? I want to supply a input data to the tree and I want the algorithm to give the correct classification for the input. But I think I have nothing to do with the tree unless I can predict. I may be interpreting the result in a wrong way. Please make me clear about this.

• predict is in the stats package in R. – Eric Peterson Jul 17 '13 at 3:59

fit = rpart(formula, data =, method =, control=)
fitVariablesUsed <- names(fit[,1:20])
preds <- predict(fit, data = newdata[,c(fitVariablesUsed)], type = c("prob"))


this will return a probability matrix for each of the observations. Meaning it will give a probability that the observation is in class 1, class2, etc.

make sure that the columns all line up correctly between the matrix you made the model with and the matrix you're going to make the predictions with.

The variables I created fitVaraiblesUsed which connected 20 variables (just for example) from the fit data frame can then be used in the new data data frame, so long as they're all named the same thing.

• Could you mind editing the answer and adding the format of newdata. Because newdata doesn't have class label, how to match columns – Bibek Subedi Jul 17 '13 at 6:37

To make a prediction based on a different dataframe than the one used to train your model (e.g. the test dataframe), you should use the newdata parameter to predict() rather than data, because data is not a real parameter (documentation).

predict(output_of_rpart, type=, newdata=testDataFrame)


## protected by kjetil b halvorsenJun 5 '18 at 14:48

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).