aggregate_tree: Aggregate Case Counts from Leaves to All Nodes in a...

View source: R/aggregate_tree.R

aggregate_treeR Documentation

Aggregate Case Counts from Leaves to All Nodes in a Hierarchical Tree

Description

Given case counts at the leaf level (as parallel vectors) and a tree, aggregates case counts upward from child nodes to parent nodes, producing a complete case matrix indexed by all nodes (rows) and regions (columns).

Usage

aggregate_tree(
  cases,
  region_id,
  node_id,
  tree = NULL,
  tree_node_id = NULL,
  tree_parent_id = NULL
)

Arguments

cases

Numeric vector of length n: case counts.

region_id

Vector of region identifiers, length n.

node_id

Vector of leaf identifiers, length n.

tree

A data.frame with columns node_id and parent_id. As an alternative, pass tree_node_id and tree_parent_id.

tree_node_id, tree_parent_id

Optional. Parallel vectors as an alternative to tree.

Details

This function is exposed for inspection and pedagogical use; the scan functions call it internally on the matrix they build from your input vectors.

Value

A matrix of dimensions m \times k (nodes x regions), with rows ordered by tree$node_id and columns by region.

References

Cancado, A. L. F., Oliveira, G. S., Quadros, A. V. C., & Duczmal, L. (2025). A tree-spatial scan statistic. Environmental and Ecological Statistics, 32, 953-978. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1007/s10651-025-00670-w")}

Examples

tree <- data.frame(
  node_id   = c(1, 2, 3, 4, 5, 6, 7),
  parent_id = c(NA, 1, 1, 2, 2, 3, 3)
)
# Leaves are 4, 5, 6, 7
aggregate_tree(
  cases     = c(10, 5, 3, 8,  2, 7, 4, 1,  6, 3, 9, 2),
  region_id = rep(1:3, each = 4),
  node_id   = rep(c(4, 5, 6, 7), times = 3),
  tree      = tree
)

treeSS documentation built on May 16, 2026, 1:08 a.m.