gw_get_weight: Geographic weighting function

View source: R/gw_core_functions.R

gw_get_weightR Documentation

Geographic weighting function

Description

Returns a function for calculating weights for each set of nearby observations for a given bandwidth, as identified by the function returned from 'gw_get_nearby'. The returned function is expected to be used within a 'function factory' approach. The returned function requires the following to have been defined: a vector of distances ('dists') such as are returned by indexing a distance matrix to get nearby observations (as determined by the function returned by 'gw_get_nearby') for a specific observation location, and an adaptive or fixed bandwidth, 'bw'.

Usage

gw_get_weight(kernel, adaptive)

Arguments

kernel

The type of distance weighting to be used: one of "bisquare", "gaussian", "exponential", "tricube" or "boxcar".

adaptive

A logical value ('TRUE' or 'FALSE') to indicate whether an adaptive or fixed bandwidth distance is being used.

Value

A function that returns a vector weights given an adaptive or fixed bandwidth, 'bw' and a vector of distances weighted by the specified 'kernel' parameter.

Examples

# load some packages and data
library(tmap)
data(georgia)
# define a distance matrix, a location and an adaptive bandwidth
dist_mat = as.matrix(dist(st_coordinates(st_centroid(georgia)), upper = T, diag = T))
obs_index = 70
bw = 30
# create the nearby function - see the help for `gw_get_nearby`
nearby_func = gw_get_nearby(adaptive = TRUE)
# apply to get an index of locations and get a vector of distances
index = nearby_func(obs_index, dist_mat, bw)
dists = dist_mat[obs_index,index]
# create the weighting function and weight the nearby locations
weight_func = gw_get_weight(kernel = "bisquare", adaptive = TRUE)
# apply the weight function to the distances
w = weight_func(bw, dists)
# map the result
g2 = georgia[index,]
g2$weight = w
tm_shape(georgia)+tm_borders()+
tm_shape(g2)+tm_fill("weight", palette = "Reds")+
tm_shape(georgia[obs_index,])+tm_borders(lwd = 2) 

gwverse/gw documentation built on May 4, 2022, 1:46 a.m.