Repository navigation
Expand file tree
/
Copy pathcompare-methods.r
More file actions
148 lines (126 loc) · 4.71 KB
/
Copy pathcompare-methods.r
File metadata and controls
148 lines (126 loc) · 4.71 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
### Definition of regression methods
### This program is part of RoLDSIS
###
### Copyright (C) 2020 Rafael Laboissière
### Copyright (C) 2020 Adrielle de Carvalho Santana
### Copyright (C) 2020 Hani Camille Yehia
###
### This program is free software: you can redistribute it and/or modify it
### under the terms of the GNU General Public License as published by the
### Free Software Foundation, either version 3 of the License, or (at your
### option) any later version.
###
### This program is distributed in the hope that it will be useful, but
### WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
### General Public License for more details.
###
### You should have received a copy of the GNU General Public License along
### with this program. If not, see <http://www.gnu.org/licenses/>.
### * Load the local libraries
source ("paths.r")
source ("cross-validation.r")
source ("roldsis.r")
### * Load the system libraries
load.pkgs (c ("glmnet", "spls"))
### * Auxiliary functions for glmnet-base LASSO and Ridge Regression
glmnet.predict.fct <- function (fm, newx) {
n <- length (fm$lambda)
p <- predict (fm, newx)
return (p [, n])
}
glmnet.coefficients.fct <- function (fm) {
n <- length (fm$lambda)
coef <- coefficients (fm)
return (unname (coef [2 : dim (coef) [1], n]))
}
### * Methods
methods <- list (
RoLDSIS = list (
regression = function (x, y, params, k)
return (roldsis (x, y)),
predict = predict.roldsis,
coefficients = coefficients.roldsis,
init.params = NULL,
lb.params = NULL,
ub.params = NULL,
k.max.fct = function (n) NULL
),
LASSO = list (
regression = function (x, y, params, k)
return (glmnet (x, y, alpha = 1, lambda = params,
standardize = FALSE)),
predict = glmnet.predict.fct,
coefficients = glmnet.coefficients.fct,
init.params = 0,
lb.params = 0,
ub.params = Inf,
k.max.fct = function (n) 1
),
Ridge = list (
regression = function (x, y, params, k)
return (glmnet (x, y, alpha = 0, lambda = params,
standardize = FALSE)),
predict = glmnet.predict.fct,
coefficients = glmnet.coefficients.fct,
init.params = 0,
lb.params = 0,
ub.params = Inf,
k.max.fct = function (n) 1
),
SPLS = list (
regression = function (x, y, params, k) {
fm <- spls (x, y, eta = params, K = k)
cat (sprintf ("\rk = %d eta = %f err = %f",
k, params,
sum ((y - predict (fm)) ^ 2)))
return (fm)
},
predict = predict,
coefficients = coefficients,
init.params = 0.5,
lb.params = 1e-5,
ub.params = 1 - 1e-5,
k.max.fct = function (n) {(n - 1) * 5 - 1}
)
## spcr = list (
## regression = function (x, y, params, k) {
## fm <- spcr (x, y, k,
## lambda.B = params [1],
## lambda.gamma = params [2],
## w = params [3],
## xi = params [4])
## cat (sprintf ("\rk = %d lambda.B = %f lambda.gamma = %f w = %f xi = %f err = %f",
## k, params [1], params [2], params [3], params [4],
## sum ((y - fm$gamma0 - x %*% fm$loadings.B %*% fm$gamma) ^ 2)))
## flush (stdout ())
## return (fm)
## },
## predict = function (fm, newx)
## return (fm$gamma0 + newx %*% fm$loadings.B %*% fm$gamma),
## coefficients = function (fm)
## return (fm$loadings.B %*% fm$gamma),
## init.params = c (0, 0, 0.1, 0.01),
## lb.params = c (0, 0, 0, 0),
## ub.params = c (Inf, Inf, 1, 1),
## k.max.fct = function (n) {(n - 1) * 5 - 1}
## )
)
compare.methods <- function (x, y, nb.folds) {
retval <- list ()
for (m in names (methods)) {
cat (sprintf ("\nRunning method %s\n", m))
flush (stdout ())
meth <- methods [[m]]
retval [[m]] <- cross.validation (x, y,
nb.folds,
meth$regression,
meth$predict,
meth$coefficients,
meth$init.params,
meth$lb.params,
meth$ub.params,
meth$k.max.fct)
}
return (retval)
}