Description Usage Arguments Author(s) Examples
Fit a GW-GLM model using Nearest Effective Neighbors for bandwidth selection.
1 |
x |
|
y |
|
coords |
|
D |
|
s |
|
verbose |
|
family |
|
prior.weights |
|
gweight |
|
bw |
|
beta1 |
|
beta2 |
|
type |
|
tol |
|
longlat |
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 76 77 | ##---- 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 (x, y, coords, D, s = NULL, verbose, family, prior.weights,
gweight, bw, beta1, beta2, type = "pearson", tol = 1e-25,
longlat = FALSE)
{
coords.unique = unique(coords)
model = list()
s.optimal = vector()
gwglmnet.object = list()
cv.error = list()
for (i in 1:dim(coords.unique)[1]) {
colocated = which(coords[, 1] == coords.unique[i, 1] &
coords[, 2] == coords.unique[i, 2])
dist = D[i, ]
bandwidth = optimize(gwglmnet.ssr, lower = beta1, upper = beta2,
maximum = FALSE, tol = bw/10, x = x, y = y, colocated = colocated,
s = s, gweight = gweight, verbose = verbose, dist = dist,
prior.weights = prior.weights, family = family, target = bw,
type = type)$minimum
cat(paste("For i=", i, ", bw=", bandwidth, ".\n", sep = ""))
weight.matrix = gweight(D, bandwidth)
loow = weight.matrix[i, -colocated]
prior.loow = prior.weights[-colocated]
reps = length(colocated)
w <- prior.loow * loow
if (sum(loow) == 0) {
return(list(cv.error = Inf))
}
reps = length(colocated)
xx = as.matrix(x[-colocated, ])
yy = as.matrix(y[-colocated])
if (family == "binomial" && (abs(sum(yy * w) - sum(w)) <
1e-04 || sum(yy * w) < 1e-04)) {
cat(paste("Abort. i=", i, ", weighted sum=", sum(yy *
w), ", sum of weights=", sum(w), "\n", sep = ""))
model[[i]] = NULL
cv.error[[i]] = 0
s.optimal = c(s.optimal, max(s))
}
else if (family == "binomial") {
model[[i]] = glmnet(x = xx, y = cbind(1 - yy, yy),
weights = w, family = family, lambda = s)
predictions = predict(model[[i]], newx = matrix(x[colocated,
], nrow = reps, ncol = dim(xx)[2]), s = s, type = "response")
cv.error[[i]] = colSums(abs(matrix(predictions -
matrix(y[colocated], nrow = reps, ncol = length(s)),
nrow = reps, ncol = length(s))))
s.optimal = c(s.optimal, s[which.min(cv.error[[i]])])
}
else {
model[[i]] = glmnet(x = xx, y = yy, weights = w,
family = family, lambda = s)
predictions = predict(model[[i]], newx = matrix(x[colocated,
], nrow = reps, ncol = dim(xx)[2]), s = s, type = "response")
cv.error[[i]] = colSums(abs(matrix(predictions -
matrix(y[colocated], nrow = reps, ncol = length(s)),
nrow = reps, ncol = length(s))))
s.optimal = c(s.optimal, s[which.min(cv.error[[i]])])
}
if (verbose) {
cat(paste(i, "\n", sep = ""))
}
}
gwglmnet.object[["coef.scale"]] = NULL
gwglmnet.object[["model"]] = model
gwglmnet.object[["s"]] = s.optimal
gwglmnet.object[["mode"]] = mode
gwglmnet.object[["coords"]] = coords.unique
gwglmnet.object[["cv.error"]] = cv.error
gwglmnet.object[["s.range"]] = s
class(gwglmnet.object) = "gwglmnet.object"
return(gwglmnet.object)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.