-
Notifications
You must be signed in to change notification settings - Fork 2
/
dkplslda.jl
102 lines (90 loc) · 2.64 KB
/
dkplslda.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
"""
dkplslda(X, y; kwargs...)
dkplslda(X, y, weights::Weight; kwargs...)
DKPLS-LDA.
* `X` : X-data (n, p).
* `y` : Univariate class membership (n).
* `weights` : Weights (n) of the observations.
Must be of type `Weight` (see e.g. function `mweight`).
Keyword arguments:
* `nlv` : Nb. latent variables (LVs) to compute.
Must be >= 1.
* `kern` : Type of kernel used to compute the Gram matrices.
Possible values are: `:krbf`, `:kpol`. See respective
functions `krbf` and `kpol` for their keyword arguments.
* `prior` : Type of prior probabilities for class
membership. Possible values are: `:unif` (uniform),
`:prop` (proportional), or a vector (of length equal to
the number of classes) giving the prior weight for each class
(the vector must be sorted in the same order as `mlev(x)`).
* `scal` : Boolean. If `true`, each column of `X`
is scaled by its uncorrected standard deviation.
Same as function `plslda` (PLS-LDA) except that
a direct kernel PLSR (function `dkplsr`), instead of a
PLSR (function `plskern`), is run on the Y-dummy table.
## Examples
```julia
using JchemoData, JLD2
path_jdat = dirname(dirname(pathof(JchemoData)))
db = joinpath(path_jdat, "data/forages2.jld2")
@load db dat
pnames(dat)
X = dat.X
Y = dat.Y
n = nro(X)
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 = n, ntrain, ntest)
tab(ytrain)
tab(ytest)
nlv = 15
gamma = .1
mod = model(dkplslda; nlv, gamma)
#mod = model(dkplslda; nlv, gamma, prior = :prop)
#mod = model(dkplsqda; nlv, gamma, alpha = .5)
#mod = model(dkplskdeda; nlv, gamma, a_kde = .5)
fit!(mod, Xtrain, ytrain)
pnames(mod)
pnames(mod.fm)
fm = mod.fm ;
fm.lev
fm.ni
fmpls = fm.fm.fmpls ;
@head fmpls.T
@head transf(mod, Xtrain)
@head transf(mod, Xtest)
@head transf(mod, Xtest; nlv = 3)
coef(fmpls)
res = predict(mod, Xtest) ;
pnames(res)
@head res.posterior
@head res.pred
errp(res.pred, ytest)
conf(res.pred, ytest).cnt
predict(mod, Xtest; nlv = 1:2).pred
```
"""
function dkplslda(X, y; kwargs...)
par = recovkwargs(Par, kwargs)
Q = eltype(X[1, 1])
weights = mweightcla(Q, y; prior = par.prior)
dkplslda(X, y, weights; kwargs...)
end
function dkplslda(X, y, weights::Weight; kwargs...)
par = recovkwargs(Par, kwargs)
@assert par.nlv >= 1 "Argument 'nlv' must be in >= 1"
res = dummy(y)
ni = tab(y).vals
fmpls = dkplsr(X, res.Y, weights; kwargs...)
fmda = list(Lda, par.nlv)
@inbounds for i = 1:par.nlv
fmda[i] = lda(fmpls.T[:, 1:i], y, weights; kwargs...)
end
fm = (fmpls = fmpls, fmda = fmda)
Plslda(fm, res.lev, ni)
end