gw_get_lm_eval: Defines an evaluation function to be used in determining the...

View source: R/gw_regr_functions.R

gw_get_lm_evalR Documentation

Defines an evaluation function to be used in determining the GWR bandwidth

Description

This returns an evaluation function for use in specifying GWR bandwidths for a given regression model formula. The options are "AIC" for a corrected AIC evaluation, or "CV" for a leave one out cross validation. It uses a matrix approach to assess the regression specified in the formula.

Usage

gw_get_lm_eval(eval)

Arguments

eval

Either "AIC" or "CV".

Value

The evaluation function.

Examples

library(sf)
data(georgia)
formula = as.formula(MedInc ~ PctBach + PctEld)
# define a distance matrix, a location and an adaptive bandwidth
dist_mat = as.matrix(dist(st_coordinates(st_centroid(georgia)), upper = T, diag = T))
# create the nearby function - see the help for `gw_get_nearby` in the gw package
nearby_func = gw_get_nearby(adaptive = TRUE)
# create the weighting function - see the help for `gw_get_weight` in the gw package
weight_func = gw_get_weight(kernel = "bisquare", adaptive = TRUE)
# adaptive r fixed bandwidth
adaptive = TRUE
# specify a bandwidth
bw = 50
# create an index of observations
indexMat  = matrix(1:nrow(georgia), ncol = 1) 	
# determine nearby locations for all observation (list for adaptive, matrix for fixed bandwidth) 
nearbyMat = apply(indexMat, 1, function(x) 
  nearby_func(x, dist_mat, bw))
# apply the weight function to the nearby locations 
weightMat = apply(indexMat, 1, function(x) 
  gw_do_weight(x, bw, nearbyMat, dist_mat, weight_func))
# define and run the evaluation function
eval_func = gw_get_lm_eval(eval = "AIC")
as.vector(eval_func(st_drop_geometry(georgia) , formula, indexMat, weightMat))

gwverse/gwregr documentation built on May 12, 2022, 8:41 p.m.