cvloss

Class: RegressionTree

Regression error by cross validation

Syntax

E = cvloss(tree)
[E,SE] = cvloss(tree)
[E,SE,Nleaf] = cvloss(tree)
[E,SE,Nleaf,BestLevel] = cvloss(tree)
[E,...] = cvloss(tree,Name,Value)

Description

E = cvloss(tree) returns the cross-validated regression error (loss) for tree, a regression tree.

[E,SE] = cvloss(tree) returns the standard error of E.

[E,SE,Nleaf] = cvloss(tree) returns the number of leaves (terminal nodes) in tree.

[E,SE,Nleaf,BestLevel] = cvloss(tree) returns the optimal pruning level for tree.

[E,...] = cvloss(tree,Name,Value) cross validates with additional options specified by one or more Name,Value pair arguments. You can specify several name-value pair arguments in any order as Name1,Value1,…,NameN,ValueN.

Input Arguments

tree

A regression tree produced by fitrtree.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

'Subtrees'

Vector of pruning levels, with 0 representing the full, unpruned tree. tree must include a pruning sequence as created either by fitctree with 'prune' set to 'on', or by the prune method. The returned E, SE, and Nleaf are vectors of the same length as Subtrees; BestLevel is a scalar. If you set Subtrees to 'all', cvloss uses the entire pruning sequence.

Default: 0

'TreeSize'

One of the following strings:

  • 'se'cvloss uses the smallest tree whose cost is within one standard error of the minimum cost.

  • 'min'cvloss uses the minimal cost tree.

Default: 'se'

'kfold'

Number of cross-validation samples, a positive integer.

Default: 10

Output Arguments

E

The cross-validation mean squared error (loss). A vector or scalar depending on the setting of the Subtrees name-value pair.

SE

The standard error of E. A vector or scalar depending on the setting of the Subtrees name-value pair.

Nleaf

Number of leaf nodes in tree. Leaf nodes are terminal nodes, which give responses, not splits. A vector or scalar depending on the setting of the Subtrees name-value pair.

BestLevel

By default, a scalar representing the largest pruning level that achieves a value of E within SE of the minimum error. If you set TreeSize to 'min', BestLevel is the smallest value in Subtrees.

Examples

Compute the cross-validation error for the default classification tree for the carsmall data:

load carsmall
X = [Displacement Horsepower Weight];
tree = fitrtree(X,MPG);
[E,SE,Nleaf,BestLevel] = cvloss(tree)

E =
   30.7558

SE =
    6.0651

Nleaf =
    19

BestLevel =
     0

Find the best level by using the Subtrees name-value pair:

[~,~,~,BestLevel] = cvloss(tree,'Subtrees','all')

BestLevel =
    15

Alternatives

You can construct a cross-validated tree model with crossval, and call kfoldLoss instead of cvloss. The alternative can save time if you are going to examine the cross-validated tree more than once.

However, unlike cvloss, kfoldLoss does not return SE, Nleaf, or BestLevel.

Was this topic helpful?