makeWeights: Calculate weights matrices.

View source: R/makeWeights.R

makeWeightsR Documentation

Calculate weights matrices.

Description

Given a a description of relationships, calculate a spatial weights matrix with various ways of handling row-sums

Usage

makeWeights(
  x,
  ID = NULL,
  bw = NULL,
  mode = "adaptive",
  weighting = "membership",
  FUN = NULL,
  offset = 0,
  inf.val = NA,
  minval = 0,
  def.neigh = 0,
  row.stand = FALSE,
  clear.mem = FALSE
)

Arguments

x

A vector representing group membership, or a matrix/distance object representing pairwise distances. The distances need not be symmetrical.

ID

A vector given the unique observation ID names. Default is ID = 1:length(x). Ignored if x is a matrix or distance object.

bw

A number representing the bandwidth within neighbors are considered. If mode = 'adaptive', bw is the number of nearest neighbors. If mode = 'fixed', bw is the radius of the window in the map units. Ignored if x is a vector, required if weighting = 'membership', and assumed to be bw = Inf otherwise.

mode

One of 'adaptive', which considers a bw number of nearest neighbors; or 'fixed', which considers a fixed bandwidth window of radius bw. Ignored if x is a vector.

weighting

One of 'membership', which considers binary membership such that neighbors are weighted 1 and non-neighbors 0; 'distance' which weighs neighbors according to FUN with the raw distance matrix providing the distance; or 'rank' which uses the rank-distance (i.e. 1 for nearest neighbor, 2 for second nearest...) as the distance variable. Ignored if x is a vector.

FUN

The distance function. Default is NULL for 'membership', and function(x) 1/(offset + x) otherwise. Ignored if x is a vector.

offset

What value is added to the denominator to prevent singularities from arising (e.g. whenever the value is 1/0)? Larger values imply smaller distance-decay. Default is offset = 0. Ignored if x is a vector.

inf.val

When singularities arise, (i.e. whenever the value is 1/0), by what value are they replaced? Default is the FUN of the lowest non-minval value. Ignored if x is a vector.

minval

When distances are raw, what is the minimum allowable distance? Default is 0. Ignored if x. Use this if you don't want to offset values otherwise.

def.neigh

Numeric. At what distance (in the map units) are observations definitely neighbors? All distances are subtracted by this value, and all resulting distances less than zero are reassigned to minval.

row.stand

Logical or 'fuzzy'. If TRUE, rows are standardized such that they sum to one. If 'fuzzy', rows are standardized as a proportion of the largest value. If x is a vector, row.stand must be logical. Default is row.stand = FALSE

clear.mem

Logical. Should gc be run in the middle of the calculation? Default is clear.mem == FALSE but set as TRUE if memory limits are a concern. Ignored if x is a vector.

Details

The description of relationships can either be a vector describing to which group an observation belongs, or a distance matrix and associated decay function.

Value

A matrix of dimensions length(x) x length(x).

Examples


# Example 1: Calculate group-based weights
# Generate dummy group names
groups <- 1:4

# Create 10 dummy values assigned to those groups
x <- sample(groups, 10, replace = TRUE)

# Create group membership weights matrix
weights <- makeWeights(x)


# Example 2: Calculate distance-based weights 
# Generate dummy observations
x <- runif(10, 0, 100)

# Get distance matrix
dists <- dist(x)

# Get fuzzy weights considering 5 nearest neighbors based on 
# inverse square distance
weights <- makeWeights(dists, bw = 5, 
                       mode = 'adaptive', weighting = 'distance',
                       FUN = function(x) 1/x^2, minval = 0.1,
                       row.stand = 'fuzzy')

andresgmejiar/lbmech documentation built on Feb. 2, 2025, 12:37 a.m.