Does anyone knows how the cv.tree function of tree package in r, works? The default is set to 10 folds, but the results show 8 tree models instead of 10:
Moreover if i set 5 folds the results show 8 models: 
The code i used is below:
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 Thank you
21 Answer
The eight things that are displayed in the output are not the folds from the cross-validation. The documentation for cv.tree says of the output:
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.
Since you did not specify the FUN argument to cv.tree, you get the default prune.tree. What is the output of prune.tree? The documentation says:
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.
Notice that your tree has exactly 8 leaves.
plot(tree.boston) text(tree.boston) prune.tree is showing you the deviance of the eight trees, snipping off the leaves one by one. cv.tree is showing you a cross-validated version of this. Instead of computing the deviance on the full training data, it uses cross-validated values for each of the eight successive prunings.
Compare the deviance in the outputs of just using prune.tree with the cross validated deviance.
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 Notice that the cross-validated values are rather higher at every step. Just using prune.tree tests on the training data and so under-reports the deviance. The cv values are more realistic.
