Description Usage Arguments Author(s) Examples
Fit a gw-glm model using the LASSO for variable selection
1 | gwglmnet.fit(x, y, coords, weight.matrix, s, verbose, family, prior.weights)
|
x |
|
y |
|
coords |
|
weight.matrix |
|
s |
|
verbose |
|
family |
|
prior.weights |
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 | ##---- 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, weight.matrix, s, verbose, family, prior.weights)
{
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])
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 {
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]])])
}
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.