-
Notifications
You must be signed in to change notification settings - Fork 633
/
blasso.R
55 lines (54 loc) · 3.07 KB
/
blasso.R
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
modelInfo <- list(label = "The Bayesian lasso",
library = "monomvn",
type = "Regression",
parameters = data.frame(parameter = 'sparsity',
class = "numeric",
label = 'Sparsity Threshold'),
grid = function(x, y, len = NULL, search = "grid") {
if(len == 1) return(data.frame(sparsity = .5))
if(search == "grid") {
out <- expand.grid(sparsity = seq(.3, .7, length = len))
} else {
out <- data.frame(sparsity = runif(len, min = 0, max = 1))
}
out
},
loop = function(grid) {
grid <- grid[order(grid$sparsity, decreasing = TRUE),, drop = FALSE]
loop <- grid[1,,drop = FALSE]
submodels <- list(grid[-1,,drop = FALSE])
list(loop = loop, submodels = submodels)
},
fit = function(x, y, wts, param, lev, last, classProbs, ...) {
mod <- monomvn::blasso(as.matrix(x), y, ...)
mod$.percent <- apply(mod$beta, 2, function(x) mean(x != 0))
mod$.sparsity <- param$sparsity
mod$.betas <- colMeans(mod$beta)
mod
},
predict = function(modelFit, newdata, submodels = NULL) {
betas <- modelFit$.betas
betas[modelFit$.percent <= modelFit$.sparsity] <- 0
if(!is.matrix(newdata)) newdata <- as.matrix(newdata)
out <- (newdata %*% betas)[,1]
if(modelFit$icept) out <- out + mean(modelFit$mu)
if(!is.null(submodels)) {
tmp <- vector(mode = "list", length = nrow(submodels))
for(i in 1:nrow(submodels)) {
betas <- modelFit$.betas
betas[modelFit$.percent <= submodels$sparsity[i]] <- 0
tmp[[i]] <- (newdata %*% betas)[,1]
if(modelFit$icept) tmp[[i]] <- tmp[[i]] + mean(modelFit$mu)
}
out <- c(list(out), tmp)
}
out
},
predictors = function(x, s = NULL, ...) {
x$xNames[x$.percent <= x$.sparsity]
},
notes = "This model creates predictions using the mean of the posterior distributions but sets some parameters specifically to zero based on the tuning parameter `sparsity`. For example, when `sparsity = .5`, only coefficients where at least half the posterior estimates are nonzero are used.",
tags = c("Linear Regression", "Bayesian Model",
"Implicit Feature Selection", "L1 Regularization"),
prob = NULL,
sort = function(x) x[order(-x$sparsity),])