Description Usage Arguments Author(s) Examples
Bandwidth selection in a GW-GLM model (bandwidth in terms of nearest neighbors or distance).
1 |
formula |
|
data |
|
coords |
|
adapt |
|
nearest.neighbors |
|
gweight |
|
s |
|
method |
|
verbose |
|
longlat |
|
family |
|
weights |
|
tol |
Wesley Brooks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (formula, data = list(), coords, adapt = FALSE, nearest.neighbors = FALSE,
gweight = gwr.Gauss, s, method = "cv", verbose = FALSE, longlat = FALSE,
family, weights, tol = .Machine$double.eps^0.25)
{
if (!is.logical(adapt))
stop("adapt must be logical")
if (is(data, "Spatial")) {
if (!missing(coords))
warning("data is Spatial* object, ignoring coords argument")
coords <- coordinates(data)
if ((is.null(longlat) || !is.logical(longlat)) && !is.na(is.projected(data)) &&
!is.projected(data)) {
longlat <- TRUE
}
else longlat <- FALSE
data <- as(data, "data.frame")
}
if (is.null(longlat) || !is.logical(longlat))
longlat <- FALSE
if (missing(coords))
stop("Observation coordinates have to be given")
mf <- match.call(expand.dots = FALSE)
m <- match(c("formula", "data", "weights"), names(mf), 0)
mf <- mf[c(1, m)]
mf$drop.unused.levels <- TRUE
mf[[1]] <- as.name("model.frame")
mf <- eval(mf, parent.frame())
mt <- attr(mf, "terms")
dp.n <- length(model.extract(mf, "response"))
weights <- as.vector(model.extract(mf, "weights"))
if (!is.null(weights) && !is.numeric(weights))
stop("'weights' must be a numeric vector")
if (is.null(weights))
weights <- rep(as.numeric(1), dp.n)
if (any(is.na(weights)))
stop("NAs in weights")
if (any(weights < 0))
stop("negative weights")
y <- model.extract(mf, "response")
x <- model.matrix(mt, mf)
n = dim(coords)[1]
if (longlat) {
D = as.matrix(earth.dist(coords), n, n)
}
else {
Xmat = matrix(rep(coords[, 1], times = n), n, n)
Ymat = matrix(rep(coords[, 2], times = n), n, n)
D = sqrt((Xmat - t(Xmat))^2 + (Ymat - t(Ymat))^2)
}
if (nearest.neighbors) {
beta1 <- 0
beta2 <- 1
}
else {
bbox <- cbind(range(coords[, 1]), range(coords[, 2]))
difmin <- spDistsN1(bbox, bbox[2, ], longlat)[1]
if (any(!is.finite(difmin)))
difmin[which(!is.finite(difmin))] <- 0
beta1 <- difmin/1000
beta2 <- 2 * difmin
}
opt <- optimize(gwglmnet.cv.f, lower = beta1, upper = beta2,
maximum = FALSE, tol = tol, formula = formula, coords = coords,
s = s, gweight = gweight, verbose = verbose, longlat = longlat,
data = data, D = D, weights = weights, adapt = adapt,
nn = nearest.neighbors, family = family)
bdwt <- opt$minimum
res <- bdwt
res
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.