Answers:
编辑:正如下面的评论所指出的,这给出了预测的置信区间,而不是严格的预测区间。让我对我的答复感到满意,应该多加思考。
随意忽略此答案或尝试在代码上构建以获取预测间隔。
我已经使用简单的引导程序几次创建了预测间隔,但是可能还有其他(更好的)方法。
考虑包装中的oil
数据,caret
并假设我们要生成硬脂酸对Palmitic的影响的部分依赖性和95%的间隔。下面只是一个简单的示例,但是您可以根据需要进行操作。确保gbm
包更新,以使grid.points
论点plot.gbm
library(caret)
data(oil)
#train the gbm using just the defaults.
tr <- train(Palmitic ~ ., method = "gbm" ,data = fattyAcids, verbose = FALSE)
#Points to be used for prediction. Use the quartiles here just for illustration
x.pt <- quantile(fattyAcids$Stearic, c(0.25, 0.5, 0.75))
#Generate the predictions, or in this case, the partial dependencies at the selected points. Substitute plot() for predict() to get predictions
p <- plot(tr$finalModel, "Stearic", grid.levels = x.pt, return.grid = TRUE)
#Bootstrap the process to get prediction intervals
library(boot)
bootfun <- function(data, indices) {
data <- data[indices,]
#As before, just the defaults in this example. Palmitic is the first variable, hence data[,1]
tr <- train(data[,-1], data[,1], method = "gbm", verbose=FALSE)
# ... other steps, e.g. using the oneSE rule etc ...
#Return partial dependencies (or predictions)
plot(tr$finalModel, "Stearic", grid.levels = x.pt, return.grid = TRUE)$y
#or predict(tr$finalModel, data = ...)
}
#Perform the bootstrap, this can be very time consuming. Just 99 replicates here but we usually want to do more, e.g. 500. Consider using the parallel option
b <- boot(data = fattyAcids, statistic = bootfun, R = 99)
#Get the 95% intervals from the boot object as the 2.5th and 97.5th percentiles
lims <- t(apply(b$t, 2, FUN = function(x) quantile(x, c(0.025, 0.975))))
这是这样做的一种方法,该方法至少尝试考虑调整gbm所引起的不确定性。http://onlinelibrary.wiley.com/doi/10.2193/2006-503/abstract中使用了类似的方法
有时,点估计在间隔之外,但是修改调整网格(即,增加树的数量和/或深度)通常可以解决该问题。
希望这可以帮助!