R中h2o包predict.H2OModel()是否输出h2o.randomForest()的OOB预测?
predict.H2OModel() Results OOB Predictions? Great question—let’s break this down clearly:
1. Does predict.H2OModel() return OOB predictions?
No, it does not. When you run predict(h2o_model, newdata=training_data), H2O uses the entire trained random forest (all trees) to generate predictions for every sample in your input data. This is exactly equivalent to using predict(rf, newdata=d) in the randomForest package—these are standard in-sample predictions, not out-of-bag ones.
Your test results confirm this: the mean squared error between d$h2o.pred and d$rf.pred is much smaller than between d$h2o.pred and d$rf.oob.pred, because both the H2O predict call and the randomForest newdata call use all trees for prediction.
2. How to get OOB predictions for an H2O Random Forest?
H2O calculates OOB predictions automatically during model training, and you can extract them directly from the trained model object using the h2o.predictions() function. Here’s how to adjust your code to get these:
set.seed(123) library(randomForest) library(h2o) data(mtcars) d = mtcars[,c('mpg', 'cyl', 'disp', 'hp', 'wt' )] ## Common settings for both random forests n.trees=1000 mtry = 3 min.node = 3 ## Prep for h2o.randomForest h2o.init() d.h2o= as.h2o(d) x.names = colnames(d)[2:5] ## Fit both models set.seed(123); rf = randomForest(mpg ~ ., data = d , ntree=n.trees, mtry = mtry, nodesize=min.node) h2o_rf = h2o.randomForest(y='mpg', x=x.names, training_frame = d.h2o, ntrees=n.trees, mtries = mtry, min_rows=min.node) ## Extract OOB predictions for both models d$rf.oob.pred = predict(rf) # OOB predictions from randomForest package d$h2o.oob.pred = as.vector(h2o.predictions(h2o_rf)) # OOB predictions from H2O ## Compare the OOB predictions mean((d$rf.oob.pred - d$h2o.oob.pred)^2)
Key Notes:
h2o.predictions()pulls the OOB predictions computed during H2O random forest training. These follow the same logic as therandomForestpackage’s OOB predictions: each sample is only predicted by trees that did not include it in their bootstrap sample.- Never confuse these with the results of
predict(h2o_rf, newdata=d.h2o)—that will always produce full-model predictions, not OOB ones.
内容的提问来源于stack exchange,提问作者bmacGTPM

