-
Notifications
You must be signed in to change notification settings - Fork 2
/
splsrda.jl
99 lines (88 loc) · 2.7 KB
/
splsrda.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
"""
splsrda(X, y; kwargs...)
splsrda(X, y, weights::Weight; kwargs...)
Sparse PLSR-DA.
* `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.
* `msparse` : Method used for the sparse thresholding.
Possible values are: `:soft`, `:mix`,
`:hard`. See thereafter.
* `delta` : Only used if `msparse = :soft`. Range for the
thresholding on the loadings (after they are standardized
to their maximal absolute value). Must ∈ [0, 1].
Higher is `delta`, stronger is the thresholding.
* `nvar` : Only used if `msparse = :mix` or `msparse = :hard`.
Nb. variables (`X`-columns) selected for each principal
component (PC). Can be a single integer (i.e. same nb.
of variables for each PC), or a vector of length `nlv`.
* `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 `plsrda` (PLSR-DA) except that
a sparse PLSR (function `splskern`), instead of a
PLSR (function `plskern`), is run on the Y-dummy table.
See function `plsrda` and `splskern` for details.
## 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
msparse = :mix ; nvar = 10
mod = model(splsrda; nlv, msparse, nvar)
fit!(mod, Xtrain, ytrain)
pnames(mod)
pnames(mod.fm)
fm = mod.fm ;
fm.lev
fm.ni
@head fm.fm.T
@head transf(mod, Xtrain)
@head transf(mod, Xtest)
@head transf(mod, Xtest; nlv = 3)
coef(fm.fm)
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(fm.fm, Xtrain)
```
"""
function splsrda(X, y; kwargs...)
par = recovkwargs(Par, kwargs)
Q = eltype(X[1, 1])
weights = mweightcla(Q, y; prior = par.prior)
splsrda(X, y, weights; kwargs...)
end
function splsrda(X, y, weights::Weight; kwargs...)
res = dummy(y)
ni = tab(y).vals
fm = splskern(X, res.Y, weights; kwargs...)
Plsrda(fm, res.lev, ni)
end