Rpart Prediction – How to Use Rpart’s Result for Classification

classificationrpart

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.

Best Answer

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.