-
Notifications
You must be signed in to change notification settings - Fork 2
/
plskdeda.jl
97 lines (87 loc) · 2.46 KB
/
plskdeda.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
"""
plskdeda(X, y; kwargs...)
plskdeda(X, y, weights::Weight; kwargs...)
KDE-DA on PLS latent variables (PLS-KDEDA).
* `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.
* `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)`).
* Keyword arguments of function `dmkern` (bandwidth
definition) can also be specified here.
* `scal` : Boolean. If `true`, each column of `X`
is scaled by its uncorrected standard deviation.
The principle is the same as functions `plslda` and
`plsqda` except that class densities are estimated from `dmkern`
instead of `dmnorm`.
## 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
mod = model(plskdeda; nlv)
#mod = model(plskdeda; nlv, 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
summary(fmpls, Xtrain)
```
"""
function plskdeda(X, y; kwargs...)
par = recovkwargs(Par, kwargs)
Q = eltype(X[1, 1])
weights = mweightcla(Q, y; prior = par.prior)
plskdeda(X, y, weights; kwargs...)
end
function plskdeda(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 = plskern(X, res.Y, weights; kwargs...)
fmda = list(Kdeda, par.nlv)
@inbounds for i = 1:par.nlv
fmda[i] = kdeda(vcol(fmpls.T, 1:i), y; kwargs...)
end
fm = (fmpls = fmpls, fmda = fmda)
Plslda(fm, res.lev, ni)
end