subtree {aggTrees} | R Documentation |

## Subtree

### Description

Extracts a subtree with a user-specified number of leaves from an `rpart`

object.

### Usage

```
subtree(tree, leaves = NULL, cv = FALSE)
```

### Arguments

`tree` |
An |

`leaves` |
Number of leaves of the desired subtree. |

`cv` |
If |

### Value

The subtree, as an `rpart`

object.

### Author(s)

Riccardo Di Francesco

### See Also

`get_leaves`

`node_membership`

`leaf_membership`

### Examples

```
## Generate data.
set.seed(1986)
n <- 3000
k <- 3
X <- matrix(rnorm(n * k), ncol = k)
colnames(X) <- paste0("x", seq_len(k))
y <- exp(X[, 1]) + 2 * X[, 2] * X[, 2] > 0 + rnorm(n)
## Construct tree.
library(rpart)
tree <- rpart(y ~ ., data = data.frame(y, X), cp = 0)
## Extract subtree.
sub_tree <- subtree(tree, leaves = 4)
sub_tree_cv <- subtree(tree, cv = TRUE)
```

[Package

*aggTrees*version 2.0.2 Index]