tree.matrix {rare} | R Documentation |
Generate matrix A encoding ancestor-descendant relationships in an hclust tree
Description
The function generates the binary matrix A
defined in Yan
and Bien (2018). The matrix encodes ancestor-descendant relationships between leaves
and tree nodes in an hclust
tree.
Usage
tree.matrix(hc)
Arguments
hc |
An |
Value
Returns a nvars
-by-nnodes
binary matrix A
where nvars
is the number of leaves (we associate covariate with leaf),
and nnodes
is the number of tree nodes (including both leaves and interior nodes).
For an hclust
tree, nnodes
= 2*nvars-1
. A[i,j]
is 1 if the i
th leaf
is a descendant of the j
th node in the tree, and 0 otherwise. By default, we
let the first nvars
columns correspond to leaves and the remaining
nvars-1
columns correspond to interior nodes.
A
is in sparse matrix format (inherit from class
sparseMatrix
as in package Matrix
).
References
Yan, X. and Bien, J. (2018) Rare Feature Selection in High Dimensions, https://arxiv.org/abs/1803.06675.
See Also
find.leaves
for finding descendant leaves of a node.
Examples
## Not run:
# For a perfect binary tree of depth 2 below
#
# 3
# /\
# 1 2
# /\ /\
# -1 -2 -3 -4
#
# A can expressed as the following:
A_true <- cbind(diag(4),
as.matrix(c(1, 1, 0, 0)),
as.matrix(c(0, 0, 1, 1)),
as.matrix(c(1, 1, 1, 1)))
# Now use tree.matrix to generate A
tree0 <- list()
tree0$merge <- matrix(c(-1, -2, -3, -4, 1, 2),
ncol = 2, byrow = TRUE)
tree0$labels <- c("leaf1", "leaf2", "leaf3", "leaf4")
A <- tree.matrix(tree0)
all(A_true == as.matrix(A))
# Another example
hc <- hclust(dist(USArrests), "ave")
A <- tree.matrix(hc)
## End(Not run)