group_obs: Automatic grouping (partitioning) of locations

View source: R/nearest_neighbor_functions.R

group_obsR Documentation

Automatic grouping (partitioning) of locations

Description

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.

Usage

group_obs(NNarray, exponent = 2)

Arguments

NNarray

Matrix of nearest neighbor indices, usually the result of find_ordered_nn.

exponent

Within the algorithm, two groups are merged if the number of unique neighbors raised to the exponent power is less than the sum of the unique numbers raised to the exponent power from the two groups.

Value

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 ith entry tells us the location in all_inds of the last index of the ith 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 ith entry tells us the index of the ith response, as ordered in all_inds.

  • local_resp_inds: The ith entry tells us the location within the block of the response index.

  • last_resp_of_block: The ith entry tells us the location within local_resp_inds and global_resp_inds of the last index of the ith block. last_resp_of_block is to global_resp_inds and local_resp_inds as last_ind_of_block is to all_inds.

Examples

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


joeguinness/GpGp documentation built on Feb. 22, 2024, 9:43 a.m.