/
lars2.R
68 lines (64 loc) · 3.23 KB
/
lars2.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
56
57
58
59
60
61
62
63
64
65
66
67
68
modelInfo <- list(label = "Least Angle Regression",
library = "lars",
type = "Regression",
parameters = data.frame(parameter = 'step',
class = "numeric",
label = '#Steps'),
grid = function(x, y, len = NULL, search = "grid") {
if(search == "grid") {
out <- data.frame(step = caret::var_seq(p = ncol(x),
classification = is.factor(y),
len = len))
} else {
out <- data.frame(step = sample(1:ncol(x), size = len, replace = TRUE))
}
out
},
loop = function(grid) {
grid <- grid[order(grid$step, 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, ...)
lars::lars(as.matrix(x), y, ...),
predict = function(modelFit, newdata, submodels = NULL) {
out <- predict(modelFit,
as.matrix(newdata),
type = "fit",
mode = "step",
s = modelFit$tuneValue$step)$fit
if(!is.null(submodels))
{
tmp <- vector(mode = "list", length = nrow(submodels) + 1)
tmp[[1]] <- out
for(j in seq(along = submodels$step))
{
tmp[[j+1]] <- predict(modelFit,
as.matrix(newdata),
type = "fit",
mode = "step",
s = submodels$step[j])$fit
}
out <- tmp
}
out
},
predictors = function(x, s = NULL, ...) {
if(is.null(s))
{
if(!is.null(x$tuneValue))
{
s <- x$tuneValue$.fraction
} else stop("must supply a vaue of s")
out <- predict(x, s = s,
type = "coefficients",
mode = "fraction")$coefficients
} else {
out <- predict(x, s = s, ...)$coefficients
}
names(out)[out != 0]
},
tags = c("Linear Regression", "Implicit Feature Selection", "L1 Regularization"),
prob = NULL,
sort = function(x) x[order(x[,1]),])