leaf_membership {aggTrees} | R Documentation |
Constructs a variable that encodes in which leaf of an rpart
object the units in a given data frame fall.
leaf_membership(tree, X)
tree |
An |
X |
Covariate matrix (no intercept). |
A factor whose levels denote in which leaf each unit falls. Leaves are ordered in increasing order of their predictions (from most negative to most positive).
Riccardo Di Francesco
subtree
node_membership
get_leaves
## 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))
## Extract number of leaves.
leaves_factor <- leaf_membership(tree, X)
head(leaves_factor)