Removes all nodes that did not improve the loss by more than cp times the initial loss. Either by themselves or by one of their successors. Note that the tree is pruned in place. If you intend to keep the original tree, make a copy of it before pruning.

# S3 method for class 'SDTree'
prune(object, cp, ...)

Arguments

object

an SDTree object

cp

Complexity parameter, the higher the value the more nodes are pruned.

...

Further arguments passed to or from other methods.

Value

A pruned SDTree object

See also

Author

Markus Ulmer

Examples

set.seed(1)
X <- matrix(rnorm(10 * 20), nrow = 10)
Y <- rnorm(10)
tree <- SDTree(x = X, y = Y)
pruned_tree <- prune(copy(tree), 0.2)
tree
#>   levelName      value         s  j      label decision n_samples
#> 1     1      0.5271484 0.4918723  2 X2 <= 0.49                 10
#> 2      ¦--1  1.4656657        NA NA        1.5      yes         5
#> 3      °--2 -0.4113688        NA NA       -0.4       no         5
pruned_tree
#>   levelName      value         s  j      label decision n_samples
#> 1     1      0.5271484 0.4918723  2 X2 <= 0.49                 10
#> 2      ¦--1  1.4656657        NA NA        1.5      yes         5
#> 3      °--2 -0.4113688        NA NA       -0.4       no         5