r中树包的树交叉验证

tree cross validation of tree package in r

有谁知道 r 中 tree 包的 cv.tree 函数是如何工作的? 默认设置为 10 折,但结果显示 8 个树模型而不是 10 个:

此外,如果我设置 5 折,结果将显示 8 个模型:

我使用的代码如下:

library (MASS)
library(tree)
set.seed (1)
train = sample (1: nrow(Boston ), nrow(Boston )/2)
tree.boston =tree(medv~.,Boston ,subset =train)
summary (tree.boston )
cv.boston =cv.tree(tree.boston,K=10)
cv.boston

谢谢

输出中显示的八个东西不是交叉验证的折叠。 cv.tree 的文档说输出:

Value

A copy of FUN applied to object, with component dev replaced by the cross-validated results from the sum of the dev components of each fit.

由于您没有为 cv.tree 指定 FUN 参数,您得到默认值 prune.treeprune.tree 的输出是什么?文档说:

Determines a nested sequence of subtrees of the supplied tree by recursively "snipping" off the least important splits, based upon the cost-complexity measure. prune.misclass is an abbreviation for prune.tree(method = "misclass") for use with cv.tree.

注意你的树正好有 8 片叶子。

plot(tree.boston)
text(tree.boston)

prune.tree给你看八棵树的变态,一根一根剪掉叶子。 cv.tree 向您展示了一个经过交叉验证的版本。它不是计算完整训练数据的偏差,而是对八个连续修剪中的每一个使用交叉验证值。

将仅使用 prune.tree 的输出中的偏差与交叉验证的偏差进行比较。

prune.tree(tree.boston)

$dev
[1]  3098.610  3354.268  3806.195  4574.704  5393.592  6952.719 11229.299
[8] 20894.657

cv.tree(tree.boston, K=5)

$dev
[1]  4768.281  4783.625  5718.441  6309.655  6329.011  7078.719 12907.505
[8] 20974.393

请注意,每一步的交叉验证值都相当高。仅对训练数据使用 prune.tree 测试,因此未充分报告偏差。 cv 值更真实。