quad.tree
function performs a recursive partitioning of a
numeric matrix, returning row and column index vectors and a list of
medians which may be used to sort the matrix. The quad tree object
is subsequently used in a nearest neighbor search of the matrix (see
find.neighbor
).
quad.tree(x, bucket.size=1)
1
provides
a safe value.
"quad.tree"
containing the following components:
x
.
A quad tree is a partitioning of the rows in a matrix
which can subsequently be used to efficiently find the rows in the
matrix closest (using a variety of metrics) to any given point. Quad
trees (also called k-d trees) are thought to be efficient for finding
nearest neighbors when the number of columns in the matrix is less
than or equal to 10.
The partitioning algorithm proceeds as follows:
1. Set lower = 1 and upper =
nrow
(the index of the
first and last row in the matrix
x
). In the
following, only consider the rows of
x
from lower
to upper.
2. Compute the range of each column of
x
over the range of
observations from lower to upper. Set
icol
to the
column number with the maximum range.
3. Find the median for column
icol
, and order the rows in the data
matrix
x
over the range lower to upper such that the median
evenly splits the rows.
4. If upper - lower =
bucket.size
, return.
5. Go to step 2 with lower unchanged and upper set to
(upper+lower)/2 (the left child of the tree).
6. Go to step 2 with lower set to (upper+lower)/2 + 1,
and upper unchanged (the right child of the tree).
Friedman, J., Bentley, J. L., and Finkel, R. A. (1977). An algorithm for finding best matches in logarithmic expected time. ACM Transaction on Mathematical Software 3, 209-226.
x <- matrix(runif(500),50,10) quad <- quad.tree(x) y <- cbind(sids$easting,sids$northing) sids.quad <- quad.tree(y) sids.quad <- quad.tree(y, bucket.size=5)