View source: R/nearest_neighbor_functions.R
group_obs | R Documentation |
Take in an array of nearest neighbors, and automatically partition the array into groups that share neighbors. This is helpful to speed the computations and improve their accuracy. The function returns a list, with each list element containing one or several rows of NNarray. The algorithm attempts to find groupings such that observations within a group share many common neighbors.
group_obs(NNarray, exponent = 2)
NNarray |
Matrix of nearest neighbor indices, usually the result of |
exponent |
Within the algorithm, two groups are merged if the number of unique
neighbors raised to the |
A list with elements defining the grouping. The list entries are:
all_inds
: vector of all indices of all blocks.
last_ind_of_block
: The i
th entry tells us the
location in all_inds
of the last index of the i
th block. Thus the length
of last_ind_of_block
is the number of blocks, and last_ind_of_block
can
be used to chop all_inds
up into blocks.
global_resp_inds
: The i
th entry tells us the
index of the i
th response, as ordered in all_inds
.
local_resp_inds
: The i
th entry tells us the location within
the block of the response index.
last_resp_of_block
: The i
th entry tells us the
location within local_resp_inds
and global_resp_inds
of the last
index of the i
th block. last_resp_of_block
is to
global_resp_inds
and local_resp_inds
as last_ind_of_block
is to all_inds
.
locs <- matrix( runif(200), 100, 2 ) # generate random locations
ord <- order_maxmin(locs) # calculate an ordering
locsord <- locs[ord,] # reorder locations
m <- 10
NNarray <- find_ordered_nn(locsord,m) # m nearest neighbor indices
NNlist2 <- group_obs(NNarray) # join blocks if joining reduces squares
NNlist3 <- group_obs(NNarray,3) # join blocks if joining reduces cubes
object.size(NNarray)
object.size(NNlist2)
object.size(NNlist3)
mean( NNlist2[["local_resp_inds"]] - 1 ) # average number of neighbors (exponent 2)
mean( NNlist3[["local_resp_inds"]] - 1 ) # average number of neighbors (exponent 3)
all_inds <- NNlist2$all_inds
last_ind_of_block <- NNlist2$last_ind_of_block
inds_of_block_2 <- all_inds[ (last_ind_of_block[1] + 1):last_ind_of_block[2] ]
local_resp_inds <- NNlist2$local_resp_inds
global_resp_inds <- NNlist2$global_resp_inds
last_resp_of_block <- NNlist2$last_resp_of_block
local_resp_of_block_2 <-
local_resp_inds[(last_resp_of_block[1]+1):last_resp_of_block[2]]
global_resp_of_block_2 <-
global_resp_inds[(last_resp_of_block[1]+1):last_resp_of_block[2]]
inds_of_block_2[local_resp_of_block_2]
# these last two should be the same
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.