View source: R/gw_regr_functions.R
gw_single_bw_gwr | R Documentation |
This returns a function that evaluates a geographically weighted regression under a given bandwidth. It uses functions from the core gwverse package (gw) to specify local observation and selection and weighting functions (get_neraby_func, gw_get_weight) and requires an evaluation function to be specified (see get_lm_eval_fun in this package). The returned function can be used evaluate different bandwidths, for a given regression model formula.
gw_single_bw_gwr(sf_data, adaptive, kernel, eval)
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". |
eval |
An evaluation method for the local model, either "AIC" for a corrected AIC evaluation, or "CV" for a leave one out corss validation. |
adative |
A logical value TRUE or FALSE to indicate whether an adaptive or fixed bandwidth distance is being used. |
The evaluation measure, to be minimised in bandwidth evaluation.
library(sf) # load data and define a model formula data(georgia) formula = as.formula(MedInc ~ PctBach + PctEld) ## 1. A single ADAPTIVE bandwidth # create the function gwr_bw_func = gw_single_bw_gwr(georgia,adaptive=TRUE,kernel="bisquare",eval="AIC") # apply it gwr_bw_func(bw = 100, formula) ## 2. Determining an optimal bandwidth with `optimse` optimise(gwr_bw_func,c(10,nrow(georgia)),formula=formula,maximum=FALSE) ## 3. Determining an adaptive bandwidth with a linear search # slower but guarantees local minima are not returned bwsa = 10:nrow(georgia) # apply the function to all adaptive bandwidths res = sapply(bwsa, function(x) gwr_bw_func(x, formula)) # find the minimum bwsa[which.min(res)] ## 4. Determining an optimal FIXED bandwidth gwr_bw_func = gw_single_bw_gwr(georgia,adaptive=FALSE,kernel="tricube",eval="CV") gwr_bw_func(bw =130000, formula) dist_mat = as.matrix(dist(st_coordinates(st_centroid(georgia)), upper = T, diag = T)) bwsf = seq(50000, (ceiling(max(dist_mat)/5000) * 5000), 5000) # 4a) by optimising optimise(gwr_bw_func,bwsf,formula=formula,maximum=FALSE) # 4b) with a linear search res = sapply(bwsf, function(x) gwr_bw_func(x, formula)) bwsf[which.min(res)]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.