8
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ predict is in the stats package in R. $\endgroup$ Commented Jul 17, 2013 at 3:59

2 Answers 2

5
$\begingroup$
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.

$\endgroup$
1
  • $\begingroup$ Could you mind editing the answer and adding the format of newdata. Because newdata doesn't have class label, how to match columns $\endgroup$ Commented Jul 17, 2013 at 6:37
2
$\begingroup$

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)
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.