-
Notifications
You must be signed in to change notification settings - Fork 2
/
gridcv.jl
266 lines (247 loc) · 8.26 KB
/
gridcv.jl
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""
gridcv(mod, X, Y; segm, score, pars = nothing, nlv = nothing, lb = nothing,
verbose = false)
Cross-validation (CV) of a model over a grid of parameters.
* `mod` : Model to evaluate.
* `X` : Training X-data (n, p).
* `Y` : Training Y-data (n, q).
Keyword arguments:
* `segm` : Segments of observations used for
the CV (output of functions [`segmts`](@ref),
[`segmkf`](@ref), etc.).
* `score` : Function computing the prediction
score (e.g. `rmsep`).
* `pars` : tuple of named vectors of same length defining
the parameter combinations (e.g. output of function `mpar`).
* `verbose` : If `true`, fitting information are printed.
* `nlv` : Value, or vector of values, of the nb. of latent
variables (LVs).
* `lb` : Value, or vector of values, of the ridge
regularization parameter "lambda".
The function is used for grid-search: it computed a prediction score
(= error rate) for model `mod` over the combinations of parameters
defined in `pars`.
For models based on LV or ridge regularization, using arguments `nlv`
and `lb` allow faster computations than including these parameters in
argument `pars. See the examples.
The function returns two outputs:
* `res` : mean results
* `res_p` : results per replication.
## Examples
```julia
######## Regression
using JLD2, CairoMakie, JchemoData
mypath = dirname(dirname(pathof(JchemoData)))
db = joinpath(mypath, "data", "cassav.jld2")
@load db dat
pnames(dat)
X = dat.X
y = dat.Y.tbc
year = dat.Y.year
tab(year)
mod = model(savgol; npoint = 21, deriv = 2, degree = 2)
fit!(mod, X)
Xp = transf(mod, X)
s = year .<= 2012
Xtrain = Xp[s, :]
ytrain = y[s]
Xtest = rmrow(Xp, s)
ytest = rmrow(y, s)
ntrain = nro(Xtrain)
ntest = nro(Xtest)
ntot = ntrain + ntest
(ntot = ntot, ntrain, ntest)
## Replicated K-fold CV
K = 3 ; rep = 10
segm = segmkf(ntrain, K; rep)
## Replicated test-set validation
#m = Int(round(ntrain / 3)) ; rep = 30
#segm = segmts(ntrain, m; rep)
####-- Plsr
mod = model(plskern)
nlv = 0:30
rescv = gridcv(mod, Xtrain, ytrain; segm, score = rmsep, nlv) ;
pnames(rescv)
res = rescv.res
plotgrid(res.nlv, res.y1; step = 2, xlabel = "Nb. LVs", ylabel = "RMSEP").f
u = findall(res.y1 .== minimum(res.y1))[1]
res[u, :]
mod = model(plskern; nlv = res.nlv[u])
fit!(mod, Xtrain, ytrain)
pred = predict(mod, Xtest).pred
@show rmsep(pred, ytest)
plotxy(vec(pred), ytest; color = (:red, .5), bisect = true, xlabel = "Prediction",
ylabel = "Observed").f
## Adding pars
pars = mpar(scal = [false; true])
rescv = gridcv(mod, Xtrain, ytrain; segm, score = rmsep, pars, nlv) ;
res = rescv.res
typ = res.scal
plotgrid(res.nlv, res.y1, typ; step = 2, xlabel = "Nb. LVs", ylabel = "RMSEP").f
u = findall(res.y1 .== minimum(res.y1))[1]
res[u, :]
mod = model(plskern; nlv = res.nlv[u], scal = res.scal[u])
fit!(mod, Xtrain, ytrain)
pred = predict(mod, Xtest).pred
@show rmsep(pred, ytest)
plotxy(vec(pred), ytest; color = (:red, .5), bisect = true, xlabel = "Prediction",
ylabel = "Observed").f
####-- Rr
lb = (10).^(-8:.1:3)
mod = model(rr)
rescv = gridcv(mod, Xtrain, ytrain; segm, score = rmsep, lb) ;
res = rescv.res
loglb = log.(10, res.lb)
plotgrid(loglb, res.y1; step = 2, xlabel = "log(lambda)", ylabel = "RMSEP").f
u = findall(res.y1 .== minimum(res.y1))[1]
res[u, :]
mod = model(rr; lb = res.lb[u])
fit!(mod, Xtrain, ytrain)
pred = predict(mod, Xtest).pred
@show rmsep(pred, ytest)
plotxy(vec(pred), ytest; color = (:red, .5), bisect = true, xlabel = "Prediction",
ylabel = "Observed").f
## Adding pars
pars = mpar(scal = [false; true])
rescv = gridcv(mod, Xtrain, ytrain; segm, score = rmsep, pars, lb) ;
res = rescv.res
loglb = log.(10, res.lb)
typ = string.(res.scal)
plotgrid(loglb, res.y1, typ; step = 2, xlabel = "log(lambda)", ylabel = "RMSEP").f
u = findall(res.y1 .== minimum(res.y1))[1]
res[u, :]
mod = model(rr; lb = res.lb[u], scal = res.scal[u])
fit!(mod, Xtrain, ytrain)
pred = predict(mod, Xtest).pred
@show rmsep(pred, ytest)
plotxy(vec(pred), ytest; color = (:red, .5), bisect = true, xlabel = "Prediction",
ylabel = "Observed").f
####-- Kplsr
mod = model(kplsr)
nlv = 0:30
gamma = (10).^(-5:1.:5)
pars = mpar(gamma = gamma)
rescv = gridcv(mod, Xtrain, ytrain; segm, score = rmsep, pars, nlv) ;
res = rescv.res
loggamma = round.(log.(10, res.gamma), digits = 1)
plotgrid(res.nlv, res.y1, loggamma; step = 2, xlabel = "Nb. LVs", ylabel = "RMSEP",
leg_title = "Log(gamma)").f
u = findall(res.y1 .== minimum(res.y1))[1]
res[u, :]
mod = model(kplsr; nlv = res.nlv[u], gamma = res.gamma[u])
fit!(mod, Xtrain, ytrain)
pred = predict(mod, Xtest).pred
@show rmsep(pred, ytest)
plotxy(vec(pred), ytest; color = (:red, .5), bisect = true, xlabel = "Prediction",
ylabel = "Observed").f
####-- Knnr
nlvdis = [15, 25] ; metric = [:mah]
h = [1, 2.5, 5]
k = [1; 5; 10; 20; 50 ; 100]
pars = mpar(nlvdis = nlvdis, metric = metric, h = h, k = k)
length(pars[1])
mod = model(knnr)
rescv = gridcv(mod, Xtrain, ytrain; segm, score = rmsep, pars, verbose = true) ;
res = rescv.res
u = findall(res.y1 .== minimum(res.y1))[1]
res[u, :]
mod = model(knnr; nlvdis = res.nlvdis[u], metric = res.metric[u], h = res.h[u],
k = res.k[u])
fit!(mod, Xtrain, ytrain)
pred = predict(mod, Xtest).pred
@show rmsep(pred, ytest)
plotxy(vec(pred), ytest; color = (:red, .5), bisect = true, xlabel = "Prediction",
ylabel = "Observed").f
####-- Lwplsr
nlvdis = 15 ; metric = [:mah]
h = [1, 2.5, 5] ; k = [50, 100]
pars = mpar(nlvdis = nlvdis, metric = metric, h = h, k = k)
length(pars[1])
nlv = 0:20
mod = model(lwplsr)
rescv = gridcv(mod, Xtrain, ytrain; segm, score = rmsep, pars, nlv, verbose = true) ;
res = rescv.res
group = string.("h=", res.h, " k=", res.k)
plotgrid(res.nlv, res.y1, group; xlabel = "Nb. LVs", ylabel = "RMSEP").f
u = findall(res.y1 .== minimum(res.y1))[1]
res[u, :]
mod = model(lwplsr; nlvdis = res.nlvdis[u], metric = res.metric[u],
h = res.h[u], k = res.k[u], nlv = res.nlv[u])
fit!(mod, Xtrain, ytrain)
pred = predict(mod, Xtest).pred
@show rmsep(pred, ytest)
plotxy(vec(pred), ytest; color = (:red, .5), bisect = true, xlabel = "Prediction",
ylabel = "Observed").f
####-- LwplsrAvg
nlvdis = 15 ; metric = [:mah]
h = [1, 2.5, 5] ; k = [50, 100]
nlv = [0:15, 0:20, 5:20]
pars = mpar(nlvdis = nlvdis, metric = metric, h = h, k = k, nlv = nlv)
length(pars[1])
mod = model(lwplsravg)
rescv = gridcv(mod, Xtrain, ytrain; segm, score = rmsep, pars, verbose = true) ;
res = rescv.res
u = findall(res.y1 .== minimum(res.y1))[1]
res[u, :]
mod = model(lwplsravg; nlvdis = res.nlvdis[u], metric = res.metric[u], h = res.h[u],
k = res.k[u], nlv = res.nlv[u])
fit!(mod, Xtrain, ytrain)
pred = predict(mod, Xtest).pred
@show rmsep(pred, ytest)
plotxy(vec(pred), ytest; color = (:red, .5), bisect = true, xlabel = "Prediction",
ylabel = "Observed").f
######## Discrimination
## The principle is the same as for regression
using JLD2, CairoMakie, JchemoData
path_jdat = dirname(dirname(pathof(JchemoData)))
db = joinpath(path_jdat, "data/forages2.jld2")
@load db dat
pnames(dat)
X = dat.X
Y = dat.Y
tab(Y.typ)
s = Bool.(Y.test)
Xtrain = rmrow(X, s)
ytrain = rmrow(Y.typ, s)
Xtest = X[s, :]
ytest = Y.typ[s]
ntrain = nro(Xtrain)
ntest = nro(Xtest)
ntot = ntrain + ntest
(ntot = ntot, ntrain, ntest)
## Replicated K-fold CV
K = 3 ; rep = 10
segm = segmkf(ntrain, K; rep)
## Replicated test-set validation
#m = Int(round(ntrain / 3)) ; rep = 30
#segm = segmts(ntrain, m; rep)
####-- Plslda
mod = model(plslda)
nlv = 1:30
prior = [:unif; :prop]
pars = mpar(prior = prior)
rescv = gridcv(mod, Xtrain, ytrain; segm, score = errp, pars, nlv)
res = rescv.res
typ = res.prior
plotgrid(res.nlv, res.y1, typ; step = 2, xlabel = "Nb. LVs", ylabel = "ERR").f
u = findall(res.y1 .== minimum(res.y1))[1]
res[u, :]
mod = model(plslda; nlv = res.nlv[u], prior = res.prior[u])
fit!(mod, Xtrain, ytrain)
pred = predict(mod, Xtest).pred
@show errp(pred, ytest)
conf(pred, ytest).pct
```
"""
function gridcv(mod, X, Y; segm, score, pars = nothing, nlv = nothing, lb = nothing,
verbose = false)
fun = mod.fun
if isnothing(nlv) && isnothing(lb)
res = gridcv_br(X, Y; segm, fun, score, pars, verbose)
elseif !isnothing(nlv)
res = gridcv_lv(X, Y; segm, fun, score, pars, nlv, verbose)
elseif !isnothing(lb)
res = gridcv_lb(X, Y; segm, fun, score, pars, lb, verbose)
end
res
end