-
Notifications
You must be signed in to change notification settings - Fork 2
/
utility.jl
1222 lines (1064 loc) · 25.3 KB
/
utility.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
aggstat(X, y; fun = mean)
aggstat(X::DataFrame; vars, groups, fun = mean)
Compute column-wise statistics by class in a dataset.
* `X` : Data (n, p).
* `y` : A categorical variable (n) (class membership).
* `fun` : Function to compute (default = mean).
Specific for dataframes:
* `vars` : Vector of the ames of the variables to summarize.
* `groups` : Vector of the names of the categorical variables to consider
for computations by class.
Variables defined in `vars` and `groups` must be columns of `X`.
Return a matrix or, if only argument `X::DataFrame` is used, a dataframe.
## Examples
```julia
using DataFrames, Statistics
n, p = 20, 5
X = rand(n, p)
df = DataFrame(X, :auto)
y = rand(1:3, n)
res = aggstat(X, y; fun = sum)
res.X
aggstat(df, y; fun = sum).X
n, p = 20, 5
X = rand(n, p)
df = DataFrame(X, string.("v", 1:p))
df.gr1 = rand(1:2, n)
df.gr2 = rand(["a", "b", "c"], n)
df
aggstat(df; vars = [:v1, :v2], groups = [:gr1, :gr2], fun = var)
```
"""
function aggstat(X, y; fun = mean)
X = ensure_mat(X)
y = vec(y)
q = nco(X)
lev = mlev(y)
nlev = length(lev)
zX = similar(X, nlev, q)
@inbounds for i in 1:nlev, j = 1:q
s = y .== lev[i]
zX[i, j] = fun(X[s, j])
end
(X = zX, lev)
end
function aggstat(X::DataFrame; vars, groups, fun = mean)
gdf = groupby(X, groups)
res = combine(gdf, vars .=> fun, renamecols = false)
sort!(res, groups)
end
"""
aggsum(x::Vector, y::Vector)
Compute sub-total sums by class of a categorical variable.
* `x` : A quantitative variable to sum (n)
* `y` : A categorical variable (n) (class membership).
Return a vector.
## Examples
```julia
x = rand(1000)
y = vcat(rand(["a" ; "c"], 900), repeat(["b"], 100))
aggsum(x, y)
```
"""
function aggsum(x::Vector, y::Vector)
lev = mlev(y)
v = similar(x, length(lev))
@inbounds for i in eachindex(lev)
s = y .== lev[i]
v[i] = sum(vrow(x, s))
end
v
end
"""
corm(X, weights::Weight)
corm(X, Y, weights::Weight)
Compute a weighted correlation matrix.
* `X` : Data (n, p).
* `Y` : Data (n, q).
* `weights` : Weights (n) of the observations.
Object of type `Weight` (e.g. generated by
function `mweight`).
Uncorrected correlation matrix
* of `X`-columns : ==> (p, p) matrix
* or between `X`-columns and `Y`-columns : ==> (p, q) matrix.
## Examples
```julia
n, p = 5, 6
X = rand(n, p)
Y = rand(n, 3)
w = mweight(rand(n))
corm(X, w)
corm(X, Y, w)
```
"""
function corm(X, weights::Weight)
zX = copy(ensure_mat(X))
xmeans = colmean(zX, weights)
xstds = colstd(zX, weights)
fcenter!(zX, xmeans)
fscale!(zX, xstds)
z = Diagonal(sqrt.(weights.w)) * zX
z' * z
end
function corm(X, Y, weights::Weight)
zX = copy(ensure_mat(X))
zY = copy(ensure_mat(Y))
xmeans = colmean(zX, weights)
ymeans = colmean(zY, weights)
xstds = colstd(zX, weights)
ystds = colstd(zY, weights)
fcenter!(zX, xmeans)
fcenter!(zY, ymeans)
fscale!(zX, xstds)
fscale!(zY, ystds)
zX' * Diagonal(weights.w) * zY
end
"""
cosm(X)
cosm(X, Y)
Compute a cosinus matrix.
* `X` : Data (n, p).
* `Y` : Data (n, q).
The function computes the cosinus matrix:
* of the columns of `X`: ==> (p, p) matrix
* or between columns of `X` and `Y` : ==> (p, q) matrix.
## Examples
```julia
n, p = 5, 6
X = rand(n, p)
Y = rand(n, 3)
cosm(X)
cosm(X, Y)
```
"""
function cosm(X)
X = ensure_mat(X)
xnorms = colnorm(X)
zX = fscale(X, xnorms)
zX' * zX
end
function cosm(X, Y)
X = ensure_mat(X)
Y = ensure_mat(Y)
xnorms = colnorm(X)
ynorms = colnorm(Y)
zX = fscale(X, xnorms)
zY = fscale(Y, ynorms)
zX' * zY
end
"""
cosv(x, y)
Compute cosinus between two vectors.
* `x` : vector (n).
* `y` : vector (n).
## Examples
```julia
n = 5
x = rand(n)
y = rand(n)
cosv(x, y)
```
"""
cosv(x, y) = dot(x, y) / (norm(x) * norm(y))
"""
covm(X, weights::Weight)
covm(X, Y, weights::Weight)
Compute a weighted covariance matrix.
* `X` : Data (n, p).
* `Y` : Data (n, q).
* `weights` : Weights (n) of the observations.
Object of type `Weight` (e.g. generated by
function `mweight`).
The function computes the uncorrected weighted covariance
matrix:
* of the columns of `X`: ==> (p, p) matrix
* or between columns of `X` and `Y` : ==> (p, q) matrix.
## Examples
```julia
n, p = 5, 6
X = rand(n, p)
Y = rand(n, 3)
w = mweight(rand(n))
covm(X, w)
covm(X, Y, w)
```
"""
function covm(X, weights::Weight)
zX = copy(ensure_mat(X))
fcenter!(zX, colmean(zX, weights))
zX = Diagonal(sqrt.(weights.w)) * zX
zX' * zX
end
function covm(X, Y, weights::Weight)
zX = copy(ensure_mat(X))
zY = copy(ensure_mat(Y))
fcenter!(zX, colmean(zX, weights))
fcenter!(zY, colmean(zY, weights))
zX' * Diagonal(weights.w) * zY
end
"""
dummy(y, T = Float64)
Compute dummy table from a categorical variable.
* `y` : A categorical variable.
* `T` : Type of the output dummy table `Y`.
## Examples
```julia
y = ["d", "a", "b", "c", "b", "c"]
#y = rand(1:3, 7)
res = dummy(y)
pnames(res)
res.Y
```
"""
function dummy(y, T = Float64)
n = length(y)
lev = mlev(y)
nlev = length(lev)
Y = BitArray(undef, n, nlev) # Type = BitMatrix
@inbounds for i in eachindex(lev)
Y[:, i] = y .== lev[i]
end
Y = convert.(T, Y)
(Y = Y, lev)
end
## Not exported (slower)
function dummy2(y)
lev = mlev(y)
nlev = length(lev)
res = list(BitVector, nlev)
@inbounds for i in eachindex(lev)
res[i] = y .== lev[i]
end
Y = reduce(hcat, res)
(Y = Y, lev)
end
"""
dupl(X; digits = 3)
Find duplicated rows in a dataset.
* `X` : A dataset.
* `digits` : Nb. digits used to round `X`
before checking.
## Examples
```julia
X = rand(5, 3)
Z = vcat(X, X[1:3, :], X[1:1, :])
dupl(X)
dupl(Z)
M = hcat(X, fill(missing, 5))
Z = vcat(M, M[1:3, :])
dupl(M)
dupl(Z)
```
"""
function dupl(X; digits = 3)
X = ensure_mat(X)
# round, etc. does not
# accept missing values
X[ismissing.(X)] .= -1e5
# End
X = round.(X, digits = digits)
n = nro(X)
rownum1 = []
rownum2 = []
@inbounds for i = 1:n
@inbounds for j = (i + 1):n
res = isequal(vrow(X, i), vrow(X, j))
if res
push!(rownum1, i)
push!(rownum2, j)
end
end
end
u = findall(rownum1 .!= rownum2)
res = DataFrame((rownum1 = rownum1[u],
rownum2 = rownum2[u]))
Int.(res)
end
"""
ensure_df(X)
Reshape `X` to a dataframe if necessary.
"""
ensure_df(X::DataFrame) = X
ensure_df(X::AbstractVector) = DataFrame([X], :auto)
ensure_df(X::AbstractMatrix) = DataFrame(X, :auto)
"""
ensure_mat(X)
Reshape `X` to a matrix if necessary.
"""
ensure_mat(X::AbstractMatrix) = X
## Tentative to work with CUDA
## Old was: ensure_mat(X::AbstractVector) = Matrix(reshape(X, :, 1))
ensure_mat(X::AbstractVector) = reshape(X, :, 1)
## End
ensure_mat(X::Number) = reshape([X], 1, 1)
ensure_mat(X::LinearAlgebra.Adjoint) = Matrix(X)
ensure_mat(X::DataFrame) = Matrix(X)
"""
findindex(x, lev)
Replace a vector containg levels by the indexes of a set of levels.
* `x` : Vector (n) of levels to replace.
* `lev` : Vector (nlev) containing the levels.
*Warning*: The levels in `x` must be contained in `lev`.
## Examples
```julia
lev = ["EHH" ; "FFS" ; "ANF" ; "CLZ" ; "CNG" ; "FRG" ; "MPW" ; "PEE" ; "SFG" ; "TTS"]
x = ["EHH" ; "TTS" ; "FRG"]
findindex(x, lev)
```
"""
function findindex(x, lev)
n = length(x)
lev = mlev(lev)
xindex = Vector{Int}(undef, n)
@inbounds for i = 1:n
xindex[i] = findall(lev .== x[i])[1]
end
xindex
end
"""
findmax_cla(x)
findmax_cla(x, weights::Weight)
Find the most occurent level in `x`.
* `x` : A categorical variable.
* `weights` : Weights (n) of the observations.
Object of type `Weight` (e.g. generated by
function `mweight`).
If ex-aequos, the function returns the first.
## Examples
```julia
x = rand(1:3, 10)
tab(x)
findmax_cla(x)
```
"""
function findmax_cla(x)
n = length(x)
res = aggstat(ones(n), x; fun = sum)
res.lev[argmax(res.X)] # if equal, argmax takes the first
end
function findmax_cla(x, weights::Weight)
res = aggstat(weights.w, x; fun = sum)
res.lev[argmax(res.X)]
end
"""
frob(X)
frob(X, weights::Weight)
Frobenius norm of a matrix.
* `X` : A matrix (n, p).
* `weights` : Weights (n) of the observations.
Object of type `Weight` (e.g. generated by
function `mweight`).
The Frobenius norm of `X` is:
* sqrt(tr(X' * X)).
The Frobenius weighted norm is:
* sqrt(tr(X' * D * X)), where D is the diagonal matrix of vector `w`.
"""
frob(X) = LinearAlgebra.norm(X)
frob(X, weights::Weight) = sqrt(sum(weights.w' * (X.^2)))
"""
head(X)
Display the first rows of a dataset.
## Examples
```julia
X = rand(100, 5)
head(X)
@head X
```
"""
function head(X)
n = nro(X)
m = min(3, n)
if isa(X, AbstractVector)
display(X[1:m])
else
display(X[1:m, :])
end
if n > 3
println("... ", size(X))
end
println(" ")
end
macro head(X)
esc( :( head($X) ))
end
"""
iqr(x)
Compute the interquartile interval (IQR).
## Examples
```julia
x = rand(100)
iqr(x)
```
"""
## Not exported
iqr(x) = quantile(x, .75) - quantile(x, .25)
"""
list(n::Integer)
Create a Vector{Any}(nothing, n).
`isnothing(object, i)` can be used to check if cell i is empty.
## Examples
```julia
list(5)
```
"""
list(n::Integer) = Vector{Any}(nothing, n)
"""
list(Q, n::Integer)
Create a Vector{Q}(undef, n).
`isassigned(object, i)` can be used to check if cell i is empty.
## Examples
```julia
list(Float64, 5)
list(Array{Float64}, 5)
list(Matrix{Int}, 5)
```
"""
list(Q, n::Integer) = Vector{Q}(undef, n)
"""
mad(x)
Compute the median absolute deviation (MAD),
adjusted by a factor (1.4826) for asymptotically normal consistency.
## Examples
```julia
x = rand(100)
mad(x)
```
"""
## Not exported
mad(x) = 1.4826 * median(abs.(x .- median(x)))
"""
miss(X)
Find rows with missing data in a dataset.
* `X` : A dataset.
## Examples
```julia
X = rand(5, 4)
zX = hcat(rand(2, 3), fill(missing, 2))
Z = vcat(X, zX)
miss(X)
miss(Z)
```
"""
function miss(X)
X = ensure_mat(X)
z = vec(sum(ismissing.(X); dims = 2))
u = findall(z .> 0)
DataFrame((rownum = u, nmissing = z[u]))
end
"""
mlev(x)
Return the sorted levels of a vector or a dataset.
## Examples
```julia
x = rand(["a";"b";"c"], 20)
lev = mlev(x)
nlev = length(lev)
X = reshape(x, 5, 4)
mlev(X)
df = DataFrame(g1 = rand(1:2, n),
g2 = rand(["a"; "c"], n))
mlev(df)
```
"""
mlev(x) = sort(unique(x))
"""
mweight(x::Vector)
Return an object of type `Weight` containing vector
`w = x / sum(x)` (if ad'hoc building, `w` must sum to 1).
## Examples
```julia
x = rand(10)
w = mweight(x)
sum(w.w)
```
"""
mweight(x::Vector) = Weight(x / sum(x))
#mweight(w::Vector{Q}) where {Q <: AbstractFloat} = mweight!(copy(w))
#mweight!(w::Vector{Q}) where {Q <: AbstractFloat} = w ./= sum(w)
#mweight(w::Union{Vector{Float32}, Vector{Float64}}) = mweight!(copy(w))
#mweight!(w::Union{Vector{Float32}, Vector{Float64}}) = w ./= sum(w)
"""
mweightcla(x::Vector; prior::Union{Symbol, Vector} = :unif)
mweightcla(Q::DataType, x::Vector; prior::Union{Symbol, Vector} = :unif)
Compute observation weights for a categorical variable,
given specified sub-total weights for the classes.
* `x` : A categorical variable (n) (class membership).
* `Q` : A data type (e.g. `Float32`).
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)`).
Return an object of type `Weight` (see function `mweight`) containing
a vector `w` (n) that sums to 1.
## Examples
```julia
x = vcat(rand(["a" ; "c"], 900), repeat(["b"], 100))
tab(x)
weights = mweightcla(x)
#weights = mweightcla(x; prior = :prop)
#weights = mweightcla(x; prior = [.1, .7, .2])
aggstat(weights.w, x; fun = sum).X
```
"""
function mweightcla(x::Vector; prior::Union{Symbol, Vector} = :unif)
n = length(x)
res = tab(x)
lev = res.keys
nlev = length(lev)
if isequal(prior, :unif)
priors = ones(nlev) / nlev
elseif isequal(prior, :prop)
priors = res.vals / n
else
priors = mweight(prior).w # could be '= prior', but mweight not costly
end
w = zeros(n)
@inbounds for i in eachindex(lev)
s = x .== lev[i]
w[s] .= priors[i] / res.vals[i]
end
mweight(w)
end
function mweightcla(Q::DataType, x::Vector; prior::Union{Symbol, Vector} = :unif)
mweight(convert.(Q, mweightcla(x; prior).w))
end
"""
nco(X)
Return the nb. columns of `X`.
"""
nco(X) = size(X, 2)
"""
normw(x, weights::Weight)
Compute the weighted norm of a vector.
* `x` : A vector (n).
* `weights` : Weights (n) of the observations.
Must be of type `Weight` (see e.g. function `mweight`).
The weighted norm of vector `x` is computed by:
* sqrt(x' * D * x), where D is the diagonal matrix of vector `weights.w`.
"""
normw(x, weights::Weight) = sqrt(sum(x .* weights.w .* x))
"""
nro(X)
Return the nb. rows of `X`.
"""
nro(X) = size(X, 1)
"""
out(x)
Return if elements of a vector are strictly outside of a given range.
* `x` : Univariate data.
* `y` : Univariate data on which is computed the range (min, max).
Return a BitVector.
## Examples
```julia
x = [-200.; -100; -1; 0; 1; 200]
out(x, [-1; .2; 1])
out(x, (-1, 1))
```
"""
out(x, y) = (x .< minimum(y)) .| (x .> maximum(y))
"""
plist(x)
Print each element of a list.
"""
function plist(x)
nam = pnames(x)
for i in eachindex(nam)
println("--- ", nam[i])
println("")
println(x[i])
println("")
end
end
"""
pmod(foo)
Shortcut for function `parentmodule`.
"""
pmod(foo) = parentmodule(foo)
"""
pnames(x)
Return the names of the elements of `x`.
"""
pnames(x) = propertynames(x)
"""
psize(x)
Print the type and size of `x`.
"""
function psize(x)
println(typeof(x))
println(size(x))
end
"""
pval(d::Distribution, q)
pval(x::Array, q)
pval(e_cdf::ECDF, q)
Compute p-value(s) for a distribution, an ECDF or vector.
* `d` : A distribution computed from `Distribution.jl`.
* `x` : Univariate data.
* `e_cdf` : An ECDF computed from `StatsBase.jl`.
* `q` : Value(s) for which to compute the p-value(s).
Compute or estimate the p-value of quantile `q`,
ie. P(Q > `q`) where Q is the random variable.
## Examples
```julia
using Distributions, StatsBase
d = Distributions.Normal(0, 1)
q = 1.96
#q = [1.64; 1.96]
Distributions.cdf(d, q) # cumulative density function (CDF)
Distributions.ccdf(d, q) # complementary CDF (CCDF)
pval(d, q) # Distributions.ccdf
x = rand(5)
e_cdf = StatsBase.ecdf(x)
e_cdf(x) # empirical CDF computed at each point of x (ECDF)
p_val = 1 .- e_cdf(x) # complementary ECDF at each point of x
q = .3
#q = [.3; .5; 10]
pval(e_cdf, q) # 1 .- e_cdf(q)
pval(x, q)
```
"""
pval(d::Distribution, q) = Distributions.ccdf(d, q)
pval(e_cdf::ECDF, q) = 1 .- e_cdf(q)
pval(x::AbstractVector, q) = pval(StatsBase.ecdf(x), q)
"""
recodcat2int(x; start = 1)
Recode a categorical variable to a integer variable.
* `x` : Variable to recode.
* `start` : Integer that will be set to the first category.
The integers returned by the function correspond to the
sorted levels (categories) of `x`.
## Examples
```julia
x = ["b", "a", "b"]
[x recodcat2int(x)]
recodcat2int(x; start = 0)
recodcat2int([25, 1, 25])
```
"""
function recodcat2int(x; start::Int = 1)
z = dummy(x).Y
nlev = nco(z)
u = z .* collect(start:(start + nlev - 1))'
u = sum(u; dims = 2)
Int.(vec(u))
end
"""
recodnum2int(x, q)
Recode a continuous variable to integer classes.
* `x` : Variable to recode.
* `q` : Values separating the classes.
## Examples
```julia
using Statistics
x = [collect(1:10); 8.1 ; 3.1]
q = [3; 8]
zx = recodnum2int(x, q)
[x zx]
probs = [.33; .66]
q = quantile(x, probs)
zx = recodnum2int(x, q)
[x zx]
```
"""
function recodnum2int(x, q)
zx = similar(x)
q = sort(q)
@inbounds for i in eachindex(x)
k = 1
@inbounds for j in eachindex(q)
x[i] > q[j] ? k = k + 1 : nothing
end
zx[i] = k
end
Int.(zx)
end
"""
recovkwargs(ParamStruct, kwargs)
"""
function recovkwargs(ParamStruct::DataType, kwargs)
if length(Dict(kwargs)) == 0
par = ParamStruct()
else
par = [ParamStruct(; Dict(kws)...) for kws
in zip([[k => v] for (k, v) in kwargs]...)][1]
end
end
"""
replacebylev(x, lev)
Replace the elements of a vector by levels of corresponding order.
* `x` : Vector (n) of values to replace.
* `lev` : Vector (nlev) containing the levels.
*Warning*: `x` and `lev` must contain the same number (nlev) of levels.
The ith sorted level in `x` is replaced by the ith sorted level of `lev`.
## Examples
```julia
x = [10; 4; 3; 3; 4; 4]
lev = ["B"; "C"; "AA"]
sort(lev)
[x replacebylev(x, lev)]
zx = string.(x)
[zx replacebylev(zx, lev)]
lev = [3; 0; -1]
[x replacebylev(x, lev)]
```
"""
function replacebylev(x, lev)
n = length(x)
lev = sort(lev)
nlev = length(lev)
@assert nlev == length(lev) "x and lev must contain the same number of levels."
xlev = mlev(x)
z = similar(lev, n)
@inbounds for i in eachindex(lev)
s = findall(x .== xlev[i])
z[s] .= lev[i]
end
z
end
"""
replacebylev2(x::Union{Int, Array{Int}}, lev::Array)
Replace the elements of an index-vector by levels.
* `x` : Vector (n) of values to replace.
* `lev` : Vector (nlev) containing the levels.
*Warning*: Let us note nlev the number of levels in `lev`.
Vector `x` must contain integer values between 1 and nlev.
Each element `x`[i] (i = 1, ..., n) is replaced by sort(`lev`)[`x`[i]].
## Examples
```julia
x = [2; 1; 2; 2]
lev = ["B"; "C"; "AA"]
sort(lev)
[x replacebylev2(x, lev)]
replacebylev2([2], lev)
replacebylev2(2, lev)
x = [2; 1; 2]
lev = [3; 0; -1]
replacebylev2(x, lev)
```
"""
function replacebylev2(x::Union{Int, Array{Int}}, lev::Array)
n = length(x)
isa(x, Int) ? x = [x] : x = vec(x)
lev = vec(sort(lev))
v = similar(lev, n)
@inbounds for i in eachindex(x)
v[i] = lev[x[i]]
end
v
end
"""
replacedict(x, dict)
Replace the elements of a vector by levels defined in a dictionary.
* `x` : Vector (n) of values to replace.
* `dict` : A dictionary of the correpondances betwwen the old and new values.
## Examples
```julia
dict = Dict("a" => 1000, "b" => 1, "c" => 2)
x = ["c"; "c"; "a"; "a"; "a"]
replacedict(x, dict)
x = ["c"; "c"; "a"; "a"; "a"; "e"]
replacedict(x, dict)
```
"""
function replacedict(x, dict)
replace(x, dict...)
end
"""
rmcol(X, s)
Remove the columns of a matrix or the components of a vector
having indexes `s`.
* `X` : Matrix or vector.
* `s` : Vector of the indexes.
## Examples
```julia
X = rand(5, 3)
rmcol(X, [1, 3])
```
"""
function rmcol(X::Union{AbstractMatrix, DataFrame},
s::Union{Vector, BitVector, UnitRange, Number})
isa(s, BitVector) ? s = findall(s .== 1) : nothing
X[:, setdiff(1:end, Int.(s))]
end
function rmcol(X::Vector,
s::Union{Vector, BitVector, UnitRange, Number})
isa(s, BitVector) ? s = findall(s .== 1) : nothing
X[setdiff(1:end, Int.(s))]
end
"""
rmrow(X, s)
Remove the rows of a matrix or the components of a vector
having indexes `s`.
* `X` : Matrix or vector.
* `s` : Vector of the indexes.
## Examples
```julia
X = rand(5, 2)
rmrow(X, [1, 4])
```
"""
function rmrow(X::Union{AbstractMatrix, DataFrame},
s::Union{Vector, BitVector, UnitRange, Number})
isa(s, BitVector) ? s = findall(s .== 1) : nothing
X[setdiff(1:end, Int.(s)), :]
end
function rmrow(X::Vector,
s::Union{Vector, BitVector, UnitRange, Number})
isa(s, BitVector) ? s = findall(s .== 1) : nothing
X[setdiff(1:end, Int.(s))]
end
"""
soft(x::Real, delta)
Soft thresholding function.
* `x` : Value to transform.
* `delta` : Range for the thresholding.
The returned value is:
* sign(x) * max(0, abs(x) - delta)
where delta >= 0.
## Examples
```julia
using CairoMakie
delta = .2
soft(3, delta)
x = LinRange(-2, 2, 100)
y = soft.(x, delta)
lines(x, y)
```
"""
function soft(x, delta)
@assert delta >= 0 "delta must be >= 0."
sign(x) * max(0, abs(x) - delta)
end
"""
softmax(x::AbstractVector)
softmax(X::Union{Matrix, DataFrame})
Softmax function.
* `x` : A vector to transform.
* `X` : A matrix whose rows are transformed.
Let v be a vector:
* 'softmax'(v) = exp.(v) / sum(exp.(v))
## Examples
```julia
x = 1:3
softmax(x)
X = rand(5, 3)
softmax(X)
```
"""
function softmax(x::AbstractVector)
expx = exp.(x)
expx / sum(expx)
end
function softmax(X::Union{Matrix, DataFrame})
X = ensure_mat(X)
P = similar(X)
n = nro(P)
@inbounds for i = 1:n
P[i, :] .= softmax(vrow(X, i))
end
P
end
"""
sourcedir(path)
Include all the files contained in a directory.
"""
function sourcedir(path)
z = readdir(path) ## List of files in path
for i in eachindex(z)