我想我得到了 OOB RMSE 的解决方案,使用keep.inbag=Tfrom randomForest。
首先,您可以使用predict从模型中获取响应的预测,而不是简单地使用RMSE公式进行评估:
Rf_model <- randomForest(mpg ~., data = mtcars)
rf_pred <- predict(Rf_model, mtcars) # predictions
sqrt(sum(rf_pred - mtcars$mpg)^2) #RMSE
#[1] 0.1781314
您可以花哨并制作自定义 rmse 函数来调用:
rmse_function <- function(pred, actual) {
sqrt(sum(pred - actual)^2)
}
rmse_function(rf_pred, mtcars$mpg)
#[1] 0.1781314
但这是火车数据的整体 RMSE。不是 OOB。
我们可能可以通过跟踪n_tree森林中每个“外部”的观测值来计算 OOB RMSE。
然后我们可以使用它对数据进行子集化,以便仅使用这些行进行预测。(out of bag obs)
按照这个想法,我们将不得不做出n_tree预测,只使用每棵树都被“排除在外”的观察子集。
然后我们将获得n_treeRMSE,我们可以对它们进行平均,以获得 OOB 观测值的平均 RMSE。
n_tree = 50
Rf_model <- randomForest(mpg ~., ntree = n_tree, data = mtcars, keep.inbag=T) # we use keep.inbag = T
inbag <- lapply(1:n_tree, function(x) which(Rf_model[["inbag"]][ ,x] == 0)) # we get only the "zeros"
# to look inside use View(Rf_model[["inbag"]]), I think that the zeros are the OOB
rf_pred <- lapply(inbag, function(x) predict(Rf_model, mtcars[x, ])) # predictions
(oob_err <- map2_dbl(rf_pred, inbag, function(x, y) rmse_function(x, mtcars[y, ]$mpg)))
# [1] 1.03926667 0.01556667 2.98096667 1.27210000 1.86380000 2.25883333 3.49130000 0.18763333 1.59326667 0.11236667
# [11] 6.92163333 0.40183333 3.36586667 1.19960000 1.31833333 2.88373333 4.48326667 1.67406667 6.92566667 8.51793333
# [21] 3.32893333 0.65510000 3.87440000 1.89276667 3.51290000 3.13026667 4.81453333 0.59756667 1.56783333 6.12180000
# [31] 3.54490000 0.57406667 0.20236667 2.20220000 0.23226667 1.61360000 0.32690000 1.86300000 3.38393333 3.33723333
# [41] 1.43760000 6.63860000 0.13120000 1.48580000 1.32950000 2.85310000 2.01306667 2.16363333 4.80706667 1.74310000
mean(oob_err) # mean of the RMSEs
#[1] 2.477725