I can try to give you an answer but it seems like your question might be more of "why do these two sets of code give different results?".
I can answer what you've asked but I can't answer that I think you are really asking unless I can reproduce tree3
, pred3
, rf2
, and pred2
.
A few reasons:
-
The are different S3 methods for different types of models
-
They were written by different people in different packages
There are informal conventions in R for doing things but they are not rigidly enforced (as they are in other languages). When we want to predict new samples, the convention is to use a predict
method. However, the arguments and syntax between models and packages are allowed to be different.
what I think you are asking...
I suspect that this is not true.
I'm going to guess that tree3
is a train
object that used method = "rpart"
since you used the predict
code that corresponds to a train
object. I can't tell without a reproducible example. I don't know what data were used, what version of R or caret
, if this is a classification or regression model, and so on.
One other thing... it's probably a bad idea to repredict the training set. That random forest model should give you close to zero errors (is this a classification model?) no matter how good it is in reality.
Can you please provide a minimal reprex (reproducible example)? The goal of a reprex is to make it as easy as possible for me to recreate your problem so that I can fix it: please help me help you!
If you've never heard of a reprex before, start by reading "What is a reprex", and follow the advice further down that page.