node_membership {aggTrees} | R Documentation |
Node Membership
Description
Constructs a binary variable that encodes whether each observation falls into a particular node of an
rpart
object.
Usage
node_membership(tree, X, node)
Arguments
tree |
An |
X |
Covariate matrix (no intercept). |
node |
Number of node. |
Value
Logical vector denoting whether each observation in X
falls into node
.
Author(s)
Riccardo Di Francesco
See Also
subtree
leaf_membership
get_leaves
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))
## Extract number of leaves.
is_in_third_node <- node_membership(tree, X, 3)
head(is_in_third_node)
[Package aggTrees version 2.0.2 Index]