gw_regr: Undertakes a GWR

View source: R/gw_regr_functions.R

gw_regrR Documentation

Undertakes a GWR

Description

Returns a function to undertake GWR once the optimal bandwidth has been defined.

Usage

gw_regr(formula, sf_data, adaptive, kernel, bw)

Arguments

formula

A formula to be used in the regression, with terms contained in the variables names of the input spatial data.

sf_data

A point or polygon spatial dataset in sf format, containing the attributes to modelled.

kernel

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

bw

The bandwidth (fixed or adaptive), either manually specified or preferably as determined through an evaluation of bandwidths using gw_single_bw_gwr.

adative

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

Value

A matrix of coefficient estimates, with each row representing the corresponding location in the input data, and each column the coefficient estimates in the order Intercept, Variable 1, Variable 2, etc as specified in the formula.

Examples

library(sf)
library(tmap)
#load data and define a model formula
data(georgia)
formula = as.formula(MedInc ~ PctBach + PctEld)
# define the bandwidth function
gwr_bw_func = gw_single_bw_gwr(georgia,adaptive=TRUE,kernel="bisquare",eval="AIC")
# define a vector of adaptive bandwidths
bwsa = 10:nrow(georgia)
# apply the bandwidth function to all bandwidths
res = sapply(bwsa, function(x) gwr_bw_func(x, formula))
# return the best result
bw = bwsa[which.min(res)]
# define the final GWR model and run it
gw_regr_final = gw_regr (formula, georgia, adaptive = TRUE, kernel = "bisquare", bw = bw)
coef_mat = gw_regr_final(formula)
# examine the result
head(coef_mat)
summary(coef_mat)
# rename and then map the coefficient estimates
colnames(coef_mat) = c("Intercept", paste0(all.vars(formula)[-1], "CE"))
georgia = cbind(georgia, coef_mat)
tm_shape(georgia)+tm_polygons(c("Intercept","PctBachCE","PctEldCE")) +
tm_layout(legend.position = c("right","top"), frame = F)

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