% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/aggregate_tree.R
\name{aggregate_tree}
\alias{aggregate_tree}
\title{Aggregate Case Counts from Leaves to All Nodes in a Hierarchical Tree}
\usage{
aggregate_tree(
  data,
  cases,
  region_id,
  node_id,
  tree = NULL,
  tree_node_id = NULL,
  tree_parent_id = NULL
)
}
\arguments{
\item{data}{A \code{data.frame} in long format with one row per
(region, leaf) observation. The \code{cases}, \code{region_id} and
\code{node_id} arguments name columns of this data.frame.}

\item{cases}{Column of \code{data} giving case counts. Given as an
unquoted column name (a string or expression also works).}

\item{region_id}{Column of \code{data} giving the region identifier.}

\item{node_id}{Column of \code{data} giving the leaf identifier.}

\item{tree}{A \code{data.frame} with columns \code{node_id} and
\code{parent_id}. As an alternative, pass \code{tree_node_id} and
\code{tree_parent_id}.}

\item{tree_node_id, tree_parent_id}{Optional. Parallel vectors as an
alternative to \code{tree}.}
}
\value{
A matrix of dimensions \eqn{m \times k} (nodes x regions), with
  rows ordered by \code{tree$node_id} and columns by region.
}
\description{
Given case counts at the leaf level (supplied as columns of a long-format
\code{data} data.frame) 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).
}
\details{
This function is exposed for inspection and pedagogical use; the scan
functions call it internally on the matrix they build from your input
data.
}
\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; long-format data.frame
dat <- data.frame(
  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)
)
aggregate_tree(dat, cases = cases, region_id = region_id,
               node_id = node_id, tree = tree)
}
\references{
Cancado, A. L. F., Oliveira, G. S., Quadros, A. V. C., & Duczmal, L.
(2025). A tree-spatial scan statistic.
\emph{Environmental and Ecological Statistics}, 32, 953-978.
\doi{10.1007/s10651-025-00670-w}
}
