Learn R Programming

grf (version 2.3.2)

get_leaf_node: Find the leaf node for a test sample.

Description

Given a GRF tree object, compute the leaf node a test sample falls into. The nodes in a GRF tree are numbered breadth first, and the returned numbers will be the leaf integer according to this ordering. To get kernel weights based on leaf membership, see the function get_forest_weights.

Usage

get_leaf_node(tree, newdata, node.id = TRUE)

Value

A vector of integers indicating the leaf number for each sample in the given tree.

Arguments

tree

A GRF tree object (retrieved by `get_tree`).

newdata

Points at which leaf predictions should be made.

node.id

Boolean indicating whether to return the node.id for each query sample (default), or if FALSE, a list of node numbers with the samples contained.

Examples

Run this code
# \donttest{
p <- 10
n <- 100
X <- matrix(2 * runif(n * p) - 1, n, p)
Y <- (X[, 1] > 0) + 2 * rnorm(n)
r.forest <- regression_forest(X, Y, num.tree = 50)

n.test <- 5
X.test <- matrix(2 * runif(n.test * p) - 1, n.test, p)
tree <- get_tree(r.forest, 1)
# Get a vector of node numbers for each sample.
get_leaf_node(tree, X.test)
# Get a list of samples per node.
get_leaf_node(tree, X.test, node.id = FALSE)
# }

Run the code above in your browser using DataLab