1
$\begingroup$

I am running randomForest() to fit and evaluate models using randomForest R package. The R-squared values provided by the model do not match the observed relationship between y and predicted, and if I call predict(model, x) on the training data, that provides yet another correlation/R-squared between predicted and observed values.

Here's a minimal working example.

data(iris)
model <- randomForest(Sepal.Width~., data=iris[1:4], ntree=500)
model$rsq[500] #.5447001 #line3
model$rsq[500]^.5 #.738038 #line4
cor(model$y, model$predicted) #.7391252 #line5

#generate predictions from model on training data
preds <- predict(model, newdata=iris[1:4])
#correlate predictions with observed values
cor(preds, iris$Sepal.Width) #.9264183

I am purposefully exploring non-cross-validated results because of some prior research that (inappropriately) used Random Forest this way. They reported the results from line3, but as can be seen, there are some inconsistencies here. Additionally, in the real data I'm using for my project, the difference in values between line4 and line5 are considerably larger.

$\endgroup$
1
  • $\begingroup$ I am encountering a same issue, where predict(model, newdata=trainingset) gives a different result in terms of R2 and RMSE from the model itself. $\endgroup$
    – K.Mole
    Commented Nov 1, 2023 at 0:03

1 Answer 1

2
$\begingroup$

Whenever I am investigating how software works, I start by reading the documentation, so let's read ?randomForest. It tells us that model$predicted is based on out-of-bag samples. Using predict uses all trees to make predictions. There's not reason to expect that the OOB results and the predictions using all trees should be identical in general.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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